Skip to content

Commit

Permalink
feat: trying
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel Hawker <samuel.hawker@ibm.com>
  • Loading branch information
samuel-hawker committed May 5, 2020
1 parent 1770b2e commit dddbb2f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
29 changes: 29 additions & 0 deletions crd-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,38 @@
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>builder-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration combine.self="override">>
<compilerArgs>
<arg>-Xlint:unchecked,deprecation</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -160,24 +161,30 @@ private static void argParseErr(String s) {
System.err.println("CrdGenerator: error: " + s);
}

/**
* Add a new error to the list of errors
* If errors is non empty an InvalidCrdException will be thrown
* @param s the string of the error to add
*/
private void err(String s) {
System.err.println("CrdGenerator: error: " + s);
numErrors++;
errors.add("CrdGenerator: error: " + s);
}

private final ObjectMapper mapper;
private final JsonNodeFactory nf;
private final Map<String, String> labels;
private int numErrors;
private List<String> errors;

CrdGenerator(ObjectMapper mapper) {
this(mapper, emptyMap());

}

CrdGenerator(ObjectMapper mapper, Map<String, String> labels) {
this.mapper = mapper;
this.nf = mapper.getNodeFactory();
this.labels = labels;
this.errors = new ArrayList<>();
}

void generate(Class<? extends CustomResource> crdClass, Writer out) throws IOException {
Expand Down Expand Up @@ -215,6 +222,9 @@ void generate(Class<? extends CustomResource> crdClass, Writer out) throws IOExc
node.set("spec", buildSpec(crd.spec(), crdClass));
}
mapper.writeValue(out, node);
if (!errors.isEmpty()) {
throw new InvalidCrdException("There were " + errors.size() + " errors\n" + String.join("\n", errors));
}
}

private ObjectNode buildSpec(Crd.Spec crd, Class<? extends CustomResource> crdClass) {
Expand Down Expand Up @@ -427,14 +437,24 @@ private Collection<Property> unionOfSubclassProperties(Class<?> crdClass) {
}
result.putAll(properties(crdClass));
JsonPropertyOrder order = crdClass.getAnnotation(JsonPropertyOrder.class);
if (order == null && !isExemptClass(crdClass)) {
throw new InvalidCrdException(crdClass.getName() + " missing @JsonPropertyOrder annotation");
if (order == null && requiresJsonPropertyOrderAnnotation(crdClass)) {
err(crdClass.getName() + " missing @JsonPropertyOrder annotation");
}
return sortedProperties(order != null ? order.value() : null, result).values();
}

private boolean isExemptClass(Class<?> crdClass) {
return crdClass == Object.class;
/**
* The CrdGenerator walks all class and sub-class properties, at some point it may hit a java primitive or a
* java Object class, these classes do not have to have {@code JsonPropertyOrder} defined
*
* With these exceptions we expect all classes to have the {@code JsonPropertyOrder} annotation
*
* @param crdClass the class being checked
* @return a boolean of whether the JsonPropertyOrder is required
*/
private boolean requiresJsonPropertyOrderAnnotation(Class<?> crdClass) {
// return crdClass != Object.class;
return true;
}

private ArrayNode buildSchemaRequired(Class<?> crdClass) {
Expand Down Expand Up @@ -649,6 +669,7 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio
CrdGenerator generator = new CrdGenerator(yaml ?
new YAMLMapper().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true).configure(YAMLGenerator.Feature.WRITE_DOC_START_MARKER, false) :
new ObjectMapper(), labels);

for (Map.Entry<String, Class<? extends CustomResource>> entry : classes.entrySet()) {
File file = new File(entry.getKey());
if (file.getParentFile().exists()) {
Expand All @@ -658,16 +679,10 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio
} else if (!file.getParentFile().mkdirs()) {
generator.err(file.getParentFile() + " does not exist and could not be created");
}

try (Writer w = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
generator.generate(entry.getValue(), w);
}
}

if (generator.numErrors > 0) {
System.err.println("There were " + generator.numErrors + " errors");
System.exit(1);
} else {
System.exit(0);
}
}
}
12 changes: 12 additions & 0 deletions crd-generator/src/test/java/io/strimzi/crdgenerator/TestCrds.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package io.strimzi.crdgenerator;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonSubTypes;
Expand All @@ -18,6 +19,8 @@
import io.strimzi.crdgenerator.annotations.Minimum;
import io.strimzi.crdgenerator.annotations.OneOf;
import io.strimzi.crdgenerator.annotations.Pattern;
import io.sundr.builder.annotations.Buildable;
//import io.sundr.builder.annotations.Buildable;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -49,6 +52,11 @@ public class TestCrds {
))
@OneOf({@OneOf.Alternative(@OneOf.Alternative.Property("either")), @OneOf.Alternative(@OneOf.Alternative.Property("or"))})
@JsonPropertyOrder({})
// @Buildable(
// editableEnabled = false,
// builderPackage = "io.fabric8.kubernetes.api.builder"
// )
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class ExampleCrd<T, U extends ValidCrdNumber, V extends U> extends CustomResource {

private String ignored;
Expand Down Expand Up @@ -121,6 +129,10 @@ public static class ExampleCrd<T, U extends ValidCrdNumber, V extends U> extends


@JsonPropertyOrder({})
@Buildable(
editableEnabled = false,
builderPackage = "io.fabric8.kubernetes.api.builder"
)
@Description("Example of complex type.")
public static class ObjectProperty {
private String foo;
Expand Down

0 comments on commit dddbb2f

Please sign in to comment.