Skip to content

Commit

Permalink
testing schema swap
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Apr 20, 2022
1 parent d686ed0 commit f1a4132
Show file tree
Hide file tree
Showing 141 changed files with 229 additions and 142 deletions.
2 changes: 1 addition & 1 deletion crd-generator/api/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<artifactId>crd-generator-parent</artifactId>
<groupId>io.fabric8</groupId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Expand Up @@ -28,8 +28,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Proxy;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.sundr.model.utils.Types.BOOLEAN_REF;

Expand Down Expand Up @@ -135,6 +137,8 @@ private static class InternalSchemaSwap {
final ClassRef originalType;
final String fieldName;

public boolean processed;

public InternalSchemaSwap(ClassRef originalType, String fieldName, ClassRef targetType) {
this.originalType = originalType;
this.fieldName = fieldName;
Expand Down Expand Up @@ -199,13 +203,50 @@ private InternalSchemaSwap extractSchemaSwap(AnnotationRef annotation) {
);
}

private static java.lang.reflect.Method originalTypeMethod = Arrays.stream(SchemaSwap.class.getMethods()).filter(m -> m.getName().equals("originalType")).findAny().get();
private static java.lang.reflect.Method fieldNameMethod = Arrays.stream(SchemaSwap.class.getMethods()).filter(m -> m.getName().equals("fieldName")).findAny().get();
private static java.lang.reflect.Method targetTypeMethod = Arrays.stream(SchemaSwap.class.getMethods()).filter(m -> m.getName().equals("targetType")).findAny().get();

private Stream<InternalSchemaSwap> extractSchemaSwaps(AnnotationRef annotation) {
Map<String, Object> params = annotation.getParameters();
Object[] schemaSwaps = (Object[]) params.get("value");
List<InternalSchemaSwap> results = new ArrayList<>(schemaSwaps.length);
for (int i = 0; i < schemaSwaps.length; i++) {
// results.add(schemaSwapFromParams((Map<String, Object>) schemaSwaps[i]));
if (Proxy.isProxyClass(schemaSwaps[i].getClass())) {
try {
ClassRef originalType = extractClassRef(Proxy.getInvocationHandler(schemaSwaps[i]).invoke(schemaSwaps[i], originalTypeMethod, new Object[]{}));
String fieldName = (String) Proxy.getInvocationHandler(schemaSwaps[i]).invoke(schemaSwaps[i], fieldNameMethod, new Object[]{});
ClassRef targetType = extractClassRef(Proxy.getInvocationHandler(schemaSwaps[i]).invoke(schemaSwaps[i], targetTypeMethod, new Object[]{}));

results.add(
new InternalSchemaSwap(
originalType,
fieldName,
targetType
));
} catch (Throwable t) {
throw new RuntimeException(t);
}
} else if (schemaSwaps[i] instanceof AnnotationRef) {
results.add(extractSchemaSwap((AnnotationRef) schemaSwaps[i]));
} else {
throw new RuntimeException("WTF is going on? " + schemaSwaps[i].getClass());
}
// System.out.println(schemaSwaps[i].getClass());
// results.add(extractSchemaSwap((AnnotationRef) schemaSwaps[i]));
}
return results.stream();
}

private void validateRemainingSchemaSwaps(String error, List<InternalSchemaSwap> schemaSwaps) {
if (!schemaSwaps.isEmpty()) {
if (!schemaSwaps.stream().filter(s -> !s.processed).collect(Collectors.toList()).isEmpty()) {
String umatchedSchemaSwaps = schemaSwaps
.stream()
.filter(s -> !s.processed)
.map(InternalSchemaSwap::toString)
.collect(Collectors.joining(",", "[", "]"));
throw new IllegalArgumentException("SchemaSwap annotation error " + error + ": " + umatchedSchemaSwaps);
// throw new IllegalArgumentException("SchemaSwap annotation error " + error + ": " + umatchedSchemaSwaps);
}
}

Expand All @@ -227,13 +268,24 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, List<Interna
.map(this::extractSchemaSwap)
.collect(Collectors.toList());

newSchemaSwaps.addAll(definition
.getAnnotations()
.stream()
.filter(a -> a.getClassRef().getFullyQualifiedName().equals("io.fabric8.crd.generator.annotation.SchemaSwaps"))
.flatMap(this::extractSchemaSwaps)
.collect(Collectors.toList()));

schemaSwaps.addAll(newSchemaSwaps);

final Set<InternalSchemaSwap> currentSchemaSwaps = schemaSwaps
.stream()
.filter(iss -> iss.getOriginalType().getFullyQualifiedName().equals(definition.getFullyQualifiedName()))
.collect(Collectors.toSet());

if (!currentSchemaSwaps.isEmpty()) {
System.out.println(definition.getFullyQualifiedName() + " processing SchemaSwaps: " + currentSchemaSwaps.size());
}

// index potential accessors by name for faster lookup
final Map<String, Method> accessors = indexPotentialAccessors(definition);

Expand All @@ -244,11 +296,17 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, List<Interna
continue;
}

final PropertyFacade facade = new PropertyFacade(property, accessors, currentSchemaSwaps);
final PropertyFacade facade = new PropertyFacade(property, accessors, schemaSwaps.stream().collect(Collectors.toSet()));
final Property possiblyRenamedProperty = facade.process();
final Set<InternalSchemaSwap> matchedSchemaSwaps = facade.getMatchedSchemaSwaps();
currentSchemaSwaps.removeAll(matchedSchemaSwaps);
schemaSwaps.removeAll(matchedSchemaSwaps);
currentSchemaSwaps
.stream()
.filter(schemaSwap -> matchedSchemaSwaps.contains(schemaSwap))
.forEach(schemaSwap -> schemaSwap.processed = true);
schemaSwaps
.stream()
.filter(schemaSwap -> matchedSchemaSwaps.contains(schemaSwap))
.forEach(schemaSwap -> schemaSwap.processed = true);
name = possiblyRenamedProperty.getName();

if (facade.required) {
Expand Down Expand Up @@ -431,6 +489,8 @@ public Property process() {
.findFirst();

currentSchemaSwap.ifPresent( iss -> {
System.out.println(name + " using SchemaSwap: " + iss.getTargetType().getFullyQualifiedName() + " original type " + iss.getOriginalType().getFullyQualifiedName());

schemaFrom = iss.targetType;
matchedSchemaSwaps.add(iss);
});
Expand Down Expand Up @@ -471,6 +531,10 @@ public Property process() {
}
});

if (schemaFrom != null) {
LOGGER.warn("Swapping type " + original.getTypeRef() + " to " + schemaFrom + " on field " + original.getName());
}

TypeRef typeRef = schemaFrom != null ? schemaFrom : original.getTypeRef();
String finalName = renamedTo != null ? renamedTo : original.getName();

Expand Down Expand Up @@ -615,7 +679,7 @@ private T resolveNestedClass(String name, TypeDef def, Set<String> visited, List
} else {
String visitedName = name + ":" + def.getFullyQualifiedName();
if (!def.getFullyQualifiedName().startsWith("java") && visited.contains(visitedName)) {
throw new IllegalArgumentException("Found a cyclic reference involving the field " + name + " of type " + def.getFullyQualifiedName());
throw new IllegalArgumentException("Found a cyclic reference involving the fields " + visited.stream().collect(Collectors.joining(", ")));
}
visited.add(visitedName);
}
Expand Down
Expand Up @@ -19,6 +19,7 @@

@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE_USE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(SchemaSwaps.class)
public @interface SchemaSwap {
Class<?> originalType();
String fieldName();
Expand Down
@@ -0,0 +1,12 @@
package io.fabric8.crd.generator.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE_USE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SchemaSwaps {
SchemaSwap[] value();
}
Expand Up @@ -19,6 +19,7 @@
import io.fabric8.kubernetes.client.CustomResource;

@SchemaSwap(originalType = ExtractionSpec.class, fieldName = "bar", targetType = FooExtractor.class)
@SchemaSwap(originalType = ExtractionSpec.class, fieldName = "bar2", targetType = FooExtractor.class)
public class Extraction extends CustomResource<ExtractionSpec, Void> {

}
Expand Up @@ -24,4 +24,6 @@ public class ExtractionSpec {

private Foo bar;

private Foo bar2;

}
Expand Up @@ -151,7 +151,7 @@ void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {
assertEquals(2, properties.size());
final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = specSchema.getProperties();
assertEquals(2, spec.size());
assertEquals(3, spec.size());

// check typed SchemaFrom
JSONSchemaProps foo = spec.get("foo");
Expand All @@ -176,6 +176,13 @@ void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {

// you can exclude fields
assertNull(barProps.get("baz"));

// check typed and repeated SchemaSwap
JSONSchemaProps bar2 = spec.get("bar2");
Map<String, JSONSchemaProps> bar2Props = bar.getProperties();
assertNotNull(bar2Props);
assertEquals("integer", bar2Props.get("BAZ").getType());
assertTrue(bar2.getRequired().contains("BAZ"));
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion crd-generator/apt/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<artifactId>crd-generator-parent</artifactId>
<groupId>io.fabric8</groupId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion crd-generator/pom.xml
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client-project</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>crd-generator-parent</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/camel-k/client/pom.xml
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>camel-k-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>camel-k-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/camel-k/mock/pom.xml
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>camel-k-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>


Expand Down
2 changes: 1 addition & 1 deletion extensions/camel-k/model-v1/pom.xml
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>camel-k-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>camel-k-model-v1</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/camel-k/model-v1alpha1/pom.xml
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>camel-k-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>camel-k-model-v1alpha1</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/camel-k/pom.xml
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-extensions</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>camel-k-extension-pom</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/camel-k/tests/pom.xml
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>camel-k-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>camel-k-tests</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/certmanager/client/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>certmanager-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>certmanager-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/certmanager/examples/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>certmanager-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>certmanager-examples</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/certmanager/mock/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>certmanager-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>certmanager-server-mock</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/certmanager/model-v1/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>certmanager-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>certmanager-model-v1</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/certmanager/model-v1alpha2/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>certmanager-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>certmanager-model-v1alpha2</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/certmanager/model-v1alpha3/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>certmanager-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>certmanager-model-v1alpha3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/certmanager/model-v1beta1/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>certmanager-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>certmanager-model-v1beta1</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/certmanager/pom.xml
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-extensions</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>certmanager-extension-pom</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/certmanager/tests/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>certmanager-extension-pom</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>certmanager-tests</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/chaosmesh/client/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>chaosmesh</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>chaosmesh-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/chaosmesh/examples/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>chaosmesh</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>chaosmesh-examples</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/chaosmesh/mock/pom.xml
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>chaosmesh</artifactId>
<version>5.12.2</version>
<version>5.12-SNAPSHOT</version>
</parent>

<artifactId>chaosmesh-server-mock</artifactId>
Expand Down

0 comments on commit f1a4132

Please sign in to comment.