Skip to content

Commit

Permalink
fix: adds filtering and switches to iterative traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Nov 14, 2023
1 parent 4c403fd commit 6961996
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Bugs

* Fix #5580: [java-generator] Correctly handle defaults for IntOrString types
* Fix #5584: Fix CRD generation when EnumMap is used

#### Improvements
* Fix #5429: moved crd generator annotations to generator-annotations instead of crd-generator-api. Using generator-annotations introduces no transitive dependencies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

import io.fabric8.crd.generator.annotation.PrinterColumn;

import java.util.ArrayList;

public class AdditionalPrinterColumnDetector extends AnnotatedMultiPropertyPathDetector {

public AdditionalPrinterColumnDetector() {
this(DOT);
}

public AdditionalPrinterColumnDetector(String prefix) {
super(prefix, PrinterColumn.class.getSimpleName(), new ArrayList<>());
super(prefix, PrinterColumn.class.getSimpleName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.stream.Collectors;

import static io.fabric8.crd.generator.AbstractJsonSchema.ANNOTATION_JSON_IGNORE;
Expand All @@ -40,21 +41,19 @@ public class AnnotatedMultiPropertyPathDetector extends TypedVisitor<TypeDefBuil
private final String annotationName;
private final List<Property> parents;
private final Map<String, Property> properties;
private final Stack<Runnable> toRun;

public AnnotatedMultiPropertyPathDetector(String prefix, String annotationName) {
this(prefix, annotationName, new ArrayList<>());
}

public AnnotatedMultiPropertyPathDetector(String prefix, String annotationName, List<Property> parents) {
this(prefix, annotationName, parents, new HashMap<>());
this(prefix, annotationName, new ArrayList<>(), new HashMap<>(), new Stack<>());
}

public AnnotatedMultiPropertyPathDetector(String prefix, String annotationName, List<Property> parents,
Map<String, Property> properties) {
Map<String, Property> properties, Stack<Runnable> toRun) {
this.prefix = prefix;
this.annotationName = annotationName;
this.parents = parents;
this.properties = properties;
this.toRun = toRun;
}

private boolean excludePropertyProcessing(Property p) {
Expand Down Expand Up @@ -84,15 +83,20 @@ public void visit(TypeDefBuilder builder) {
if (!parents.contains(p) && !excludePropertyProcessing(p)) {
ClassRef classRef = (ClassRef) p.getTypeRef();
TypeDef propertyType = Types.typeDefFrom(classRef);
if (!propertyType.isEnum()) {
if (!propertyType.isEnum() && !classRef.getPackageName().startsWith("java.")) {
List<Property> newParents = new ArrayList<>(parents);
newParents.add(p);
new TypeDefBuilder(propertyType)
.accept(new AnnotatedMultiPropertyPathDetector(prefix, annotationName, newParents, properties))
.build();
toRun.add(() -> new TypeDefBuilder(propertyType)
.accept(new AnnotatedMultiPropertyPathDetector(prefix, annotationName, newParents, properties, toRun)));
}
}
});

if (parents.isEmpty()) {
while (!toRun.isEmpty()) {
toRun.pop().run();
}
}
}

public Set<String> getPaths() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

Expand All @@ -39,22 +40,20 @@ public class AnnotatedPropertyPathDetector extends TypedVisitor<TypeDefBuilder>
private final String prefix;
private final String annotationName;
private final List<Property> parents;
private final AtomicReference<Optional<String>> reference;
private final AtomicReference<String> reference;
private final Stack<Runnable> toRun;

public AnnotatedPropertyPathDetector(String prefix, String annotationName) {
this(prefix, annotationName, new ArrayList<>());
}

public AnnotatedPropertyPathDetector(String prefix, String annotationName, List<Property> parents) {
this(prefix, annotationName, parents, new AtomicReference<>(Optional.empty()));
this(prefix, annotationName, new ArrayList<>(), new AtomicReference<>(), new Stack<>());
}

public AnnotatedPropertyPathDetector(String prefix, String annotationName, List<Property> parents,
AtomicReference<Optional<String>> reference) {
AtomicReference<String> reference, Stack<Runnable> toRun) {
this.prefix = prefix;
this.annotationName = annotationName;
this.parents = parents;
this.reference = reference;
this.toRun = toRun;
}

private static boolean excludePropertyProcessing(Property p) {
Expand All @@ -70,52 +69,48 @@ private static boolean excludePropertyProcessing(Property p) {
public void visit(TypeDefBuilder builder) {
TypeDef type = builder.build();
final List<Property> properties = type.getProperties();
if (visitProperties(properties)) {
return;
}
visitPropertiesClasses(properties);
visitProperties(properties);
}

private void visitPropertiesClasses(List<Property> properties) {
private void visitProperties(List<Property> properties) {
for (Property p : properties) {
if (parents.contains(p)) {
continue;
}

List<Property> newParents = new ArrayList<>(parents);
boolean match = false;
for (AnnotationRef annotation : p.getAnnotations()) {
match = annotation.getClassRef().getName().equals(annotationName);
if (match) {
newParents.add(p);
reference.set(newParents.stream().map(Property::getName).collect(Collectors.joining(DOT, prefix, "")));
return;
}
}

if (!(p.getTypeRef() instanceof ClassRef)) {
continue;
}
if (!parents.contains(p) && !excludePropertyProcessing(p)) {
ClassRef classRef = (ClassRef) p.getTypeRef();
TypeDef propertyType = Types.typeDefFrom(classRef);
if (!propertyType.isEnum()) {
List<Property> newParents = new ArrayList<>(parents);
if (!propertyType.isEnum() && !classRef.getPackageName().startsWith("java.")) {
newParents.add(p);
new TypeDefBuilder(propertyType)
.accept(new AnnotatedPropertyPathDetector(prefix, annotationName, newParents, reference))
.build();
.accept(new AnnotatedPropertyPathDetector(prefix, annotationName, newParents, reference, toRun));
}
}
}
}

private boolean visitProperties(List<Property> properties) {
for (Property p : properties) {
if (parents.contains(p)) {
continue;
}

List<Property> newParents = new ArrayList<>(parents);
boolean match = false;
for (AnnotationRef annotation : p.getAnnotations()) {
match = annotation.getClassRef().getName().equals(annotationName);
if (match) {
newParents.add(p);
reference.set(Optional.of(newParents.stream().map(Property::getName).collect(Collectors.joining(DOT, prefix, ""))));
return true;
}
if (parents.isEmpty()) {
while (!toRun.isEmpty() && reference.get() == null) {
toRun.pop().run();
}
}
return false;
}

public Optional<String> getPath() {
return reference.get();
return Optional.ofNullable(reference.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

import io.fabric8.kubernetes.model.annotation.LabelSelector;

import java.util.ArrayList;

public class LabelSelectorPathDetector extends AnnotatedPropertyPathDetector {

public LabelSelectorPathDetector() {
this(DOT);
}

public LabelSelectorPathDetector(String prefix) {
super(prefix, LabelSelector.class.getSimpleName(), new ArrayList<>());
super(prefix, LabelSelector.class.getSimpleName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

import io.fabric8.kubernetes.model.annotation.SpecReplicas;

import java.util.ArrayList;

public class SpecReplicasPathDetector extends AnnotatedPropertyPathDetector {

public SpecReplicasPathDetector() {
this(DOT);
}

public SpecReplicasPathDetector(String prefix) {
super(prefix, SpecReplicas.class.getSimpleName(), new ArrayList<>());
super(prefix, SpecReplicas.class.getSimpleName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

import io.fabric8.kubernetes.model.annotation.StatusReplicas;

import java.util.ArrayList;

public class StatusReplicasPathDetector extends AnnotatedPropertyPathDetector {

public StatusReplicasPathDetector(String prefix) {
super(prefix, StatusReplicas.class.getSimpleName(), new ArrayList<>());
super(prefix, StatusReplicas.class.getSimpleName());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

import java.util.EnumMap;

@Group("map.fabric8.io")
@Version("v1alpha1")
public class ContainingMaps extends CustomResource<ContainingMapsSpec, Void> {

public enum Foo { BAR }

private EnumMap<Foo, String> enumToStringMap;

}

0 comments on commit 6961996

Please sign in to comment.