Skip to content

Commit

Permalink
task: moves the crd annotations into generator annotations
Browse files Browse the repository at this point in the history
closes #5429
  • Loading branch information
shawkins committed Oct 25, 2023
1 parent 61406ed commit 824c159
Show file tree
Hide file tree
Showing 22 changed files with 678 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fix #5501: [crd-generator] Fix fallback value of `Default` annotation in presence of multiple accessors

#### Improvements
* Fix #5429: moved crd generator annotations to generator-annotations instead of crd-generator-api. Using generator-annotations introduces no transitive dependencies.

#### Dependency Upgrade

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.fabric8.kubernetes.builder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class BaseFluent<F> {

public static final String VISIT = "visit";
public final VisitableMap _visitables = new VisitableMap();

public static <T> VisitableBuilder<T, ?> builderOf(T item) {
if (item instanceof Editable) {
Object editor = ((Editable) item).edit();
if (editor instanceof VisitableBuilder) {
return (VisitableBuilder<T, ?>) editor;
}
}

try {
return (VisitableBuilder<T, ?>) Class
.forName(item.getClass().getName() + "Builder", true, item.getClass().getClassLoader())
.getConstructor(item.getClass())
.newInstance(item);
} catch (Exception e) {
try {
return (VisitableBuilder<T, ?>) Class.forName(item.getClass().getName() + "Builder").getConstructor(item.getClass())
.newInstance(item);
} catch (Exception e1) {
throw new IllegalStateException("Failed to create builder for: " + item.getClass(), e1);
}
}
}

public static <T> List<T> build(List<? extends Builder<? extends T>> list) {
return list == null ? null : list.stream().map(Builder::build).collect(Collectors.toList());
}

public static <T> Set<T> build(Set<? extends Builder<? extends T>> set) {
return set == null ? null : new LinkedHashSet<T>(set.stream().map(Builder::build).collect(Collectors.toSet()));
}

public static <T> List<T> aggregate(List<? extends T>... lists) {
return new ArrayList(Arrays.stream(lists).filter(Objects::nonNull).collect(Collectors.toList()));
}

public static <T> Set<T> aggregate(Set<? extends T>... sets) {
return new LinkedHashSet(Arrays.stream(sets).filter(Objects::nonNull).collect(Collectors.toSet()));
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + 0;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
return true;
}

public Optional<VisitableMap> getVisitableMap() {
return Optional.of(_visitables);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.fabric8.kubernetes.builder;

import java.lang.FunctionalInterface;
@FunctionalInterface
public interface Builder<T>{


T build();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.fabric8.kubernetes.builder;

import java.util.Map.Entry;
import java.lang.Class;
import java.lang.Object;
import java.util.List;
import java.lang.String;
import java.util.function.Predicate;
public class DelegatingVisitor<T> implements Visitor<T>{
DelegatingVisitor(Class<T> type,Visitor<T> delegate) {
this.type = type;
this.delegate = delegate;
}
private final Class<T> type;
private final Visitor<T> delegate;

public Class<T> getType() {
return type;
}

public void visit(T target) {
delegate.visit(target);
}

public int order() {
return delegate.order();
}

public void visit(List<Entry<String,Object>> path,T target) {
delegate.visit(path, target);
}

public <F>Predicate<List<Entry<String,Object>>> getRequirement() {
return delegate.getRequirement();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.fabric8.kubernetes.builder;

public interface Editable<T>{


T edit();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.fabric8.kubernetes.builder;

public interface Nested<F>{


F and();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.fabric8.kubernetes.builder;

import java.util.Map.Entry;
import java.lang.Class;
import java.lang.Object;
import java.util.List;
import java.lang.String;
import java.lang.reflect.Method;
public class PathAwareTypedVisitor<V,P> extends TypedVisitor<V>{
PathAwareTypedVisitor() {
List<Class> args = Visitors.getTypeArguments(PathAwareTypedVisitor.class, getClass());
if (args == null || args.isEmpty()) {
throw new IllegalStateException("Could not determine type arguments for path aware typed visitor.");
}
this.type = (Class<V>) args.get(0);
this.parentType = (Class<P>) args.get(1);
}
private final Class<V> type;
private final Class<P> parentType;

public void visit(V element) {

}

public void visit(List<Entry<String,Object>> path,V element) {
visit(element);
}

public P getParent(List<Entry<String,Object>> path) {
return path.size() - 1 >= 0 ? (P) path.get(path.size() - 1) : null;
}

public Class<P> getParentType() {
return parentType;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.fabric8.kubernetes.builder;

import java.lang.Class;
public abstract class TypedVisitor<V> implements Visitor<V>{



public Class<V> getType() {
return (Class<V>) Visitors.getTypeArguments(TypedVisitor.class, getClass()).get(0);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.fabric8.kubernetes.builder;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;

public interface Visitable<T> {

default T accept(Visitor<?>... visitors) {
return accept(Collections.emptyList(), visitors);
}

default <V> T accept(Class<V> type, Visitor<V> visitor) {
return accept(Collections.emptyList(), new Visitor<V>() {
@Override
public Class<V> getType() {
return type;
}

@Override
public void visit(List<Entry<String, Object>> path, V element) {
visitor.visit(path, element);
}

@Override
public void visit(V element) {
visitor.visit(element);
}
});
}

default T accept(List<Entry<String, Object>> path, Visitor<?>... visitors) {
return accept(path, "", visitors);
}

default T accept(List<Entry<String, Object>> path, String currentKey, Visitor<?>... visitors) {
List<Visitor> sortedVisitor = new ArrayList<>();
for (Visitor visitor : visitors) {
visitor = VisitorListener.wrap(visitor);
if (!visitor.canVisit(path, this)) {
continue;
}
sortedVisitor.add(visitor);
}
sortedVisitor.sort((l, r) -> ((Visitor) r).order() - ((Visitor) l).order());
for (Visitor visitor : sortedVisitor) {
visitor.visit(path, this);
}

List<Entry<String, Object>> copyOfPath = path != null ? new ArrayList(path) : new ArrayList<>();
copyOfPath.add(new AbstractMap.SimpleEntry<>(currentKey, this));

getVisitableMap().ifPresent(vm -> {
for (Entry<String, ?> entry : vm.entrySet()) {
List<Entry<String, Object>> newPath = Collections.unmodifiableList(copyOfPath);

// Copy visitables to avoid ConcurrentModificationException when Visitors add/remove Visitables
for (Visitable<T> visitable : new ArrayList<>((List<Visitable<T>>) entry.getValue())) {
for (Visitor visitor : visitors) {
if (visitor.getType() != null && visitor.getType().isAssignableFrom(visitable.getClass())) {
visitable.accept(newPath, entry.getKey(), visitor);
}
}

for (Visitor visitor : visitors) {
if (visitor.getType() == null || !visitor.getType().isAssignableFrom(visitable.getClass())) {
visitable.accept(newPath, entry.getKey(), visitor);
}
}
}
}
});

return (T) this;
}

default T getTarget(Visitable<T> visitable) {
return (T) visitable;
}

default Optional<VisitableMap> getVisitableMap() {
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.fabric8.kubernetes.builder;

public interface VisitableBuilder<T, V> extends Builder<T>, Visitable<V> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.fabric8.kubernetes.builder;

import java.util.stream.Collectors;
import java.lang.Iterable;
import java.util.function.Consumer;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.Object;
import java.util.List;
import java.lang.String;
import java.util.Spliterator;
public class VisitableMap extends HashMap<String,List<Visitable<?>>> implements Iterable<Visitable<?>>{



public List<Visitable<?>> get(Object key) {
if (!containsKey(key)) {
put(String.valueOf(key), new ArrayList());
}
return super.get(key);
}

public List<Visitable<?>> aggregate() {
return values().stream().flatMap(l -> l.stream()).collect(Collectors.toList());
}

public Iterator<Visitable<?>> iterator() {
return aggregate().iterator();
}

public void forEach(Consumer<? super Visitable<?>> action) {
aggregate().forEach(action);
}

public Spliterator spliterator() {
return aggregate().spliterator();
}


}
Loading

0 comments on commit 824c159

Please sign in to comment.