Skip to content

Commit

Permalink
fix: adds filtering and switches to iterative traversal
Browse files Browse the repository at this point in the history
closes #5584
  • Loading branch information
shawkins committed Nov 14, 2023
1 parent 4c403fd commit f54242d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 57 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 @@ -22,7 +22,9 @@
import io.sundr.model.TypeDef;
import io.sundr.model.TypeDefBuilder;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -40,21 +42,19 @@ public class AnnotatedMultiPropertyPathDetector extends TypedVisitor<TypeDefBuil
private final String annotationName;
private final List<Property> parents;
private final Map<String, Property> properties;
private final Deque<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 ArrayDeque<>());
}

public AnnotatedMultiPropertyPathDetector(String prefix, String annotationName, List<Property> parents,
Map<String, Property> properties) {
Map<String, Property> properties, Deque<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 +84,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 @@ -23,7 +23,9 @@
import io.sundr.model.TypeDef;
import io.sundr.model.TypeDefBuilder;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -39,22 +41,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 Deque<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 ArrayDeque<>());
}

public AnnotatedPropertyPathDetector(String prefix, String annotationName, List<Property> parents,
AtomicReference<Optional<String>> reference) {
AtomicReference<String> reference, Deque<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,32 +70,10 @@ 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);
}

private void visitPropertiesClasses(List<Property> properties) {
for (Property p : properties) {
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);
newParents.add(p);
new TypeDefBuilder(propertyType)
.accept(new AnnotatedPropertyPathDetector(prefix, annotationName, newParents, reference))
.build();
}
}
}
visitProperties(properties);
}

private boolean visitProperties(List<Property> properties) {
private void visitProperties(List<Property> properties) {
for (Property p : properties) {
if (parents.contains(p)) {
continue;
Expand All @@ -107,15 +85,30 @@ private boolean visitProperties(List<Property> properties) {
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;
reference.set(newParents.stream().map(Property::getName).collect(Collectors.joining(DOT, prefix, "")));
return;
}
}

if (p.getTypeRef() instanceof ClassRef && !excludePropertyProcessing(p)) {
ClassRef classRef = (ClassRef) p.getTypeRef();
TypeDef propertyType = Types.typeDefFrom(classRef);
if (!propertyType.isEnum() && !classRef.getPackageName().startsWith("java.")) {
newParents.add(p);
new TypeDefBuilder(propertyType)
.accept(new AnnotatedPropertyPathDetector(prefix, annotationName, newParents, reference, toRun));
}
}
}
return false;

if (parents.isEmpty()) {
while (!toRun.isEmpty() && reference.get() == null) {
toRun.pop().run();
}
}
}

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,16 @@
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 f54242d

Please sign in to comment.