Skip to content

Commit

Permalink
rebase, improvements and more
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Nov 11, 2022
1 parent 3f89a22 commit 3480e87
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,25 @@ public GeneratorResult generateJava() {
}

if (prop.getDefaultValue() != null) {
objField.getVariable(0).setInitializer(generateDefaultInitializerExpression(prop));
Expression primitiveDefault = generatePrimitiveDefaultInitializerExpression(prop);

if (primitiveDefault != null) {
objField.getVariable(0).setInitializer(primitiveDefault);
} else {
String staticFieldName = "_" + fieldName;
FieldDeclaration defaultObjField = clz.addField(fieldType, staticFieldName, Modifier.Keyword.PRIVATE,
Modifier.Keyword.STATIC);

defaultObjField.getVariable(0).setInitializer(
new NameExpr(
"io.fabric8.kubernetes.client.utils.Serialization.unmarshal("
+ "\"" + StringEscapeUtils.escapeJava(Serialization.asJson(prop.getDefaultValue())) + "\""
+ ", "
+ prop.getClassType() + ".class"
+ ")"));

objField.getVariable(0).setInitializer(staticFieldName);
}
}
} catch (Exception cause) {
throw new JavaGeneratorException(
Expand Down Expand Up @@ -277,12 +295,12 @@ public GeneratorResult generateJava() {
}

/**
* This method is responsible for creating an expression that will initialize the default value.
* This method is responsible for creating an expression that will initialize the default value if primitive
*
* @return a {@link Expression} instance that contains a call to the
* {@link Serialization#unmarshal(String, Class)} method.
*/
private Expression generateDefaultInitializerExpression(AbstractJSONSchema2Pojo prop) {
private Expression generatePrimitiveDefaultInitializerExpression(AbstractJSONSchema2Pojo prop) {
if (prop.getDefaultValue().isValueNode()) {
String value = prop.getDefaultValue().toString();
if (prop.getClassType().equals("Long") && prop.getDefaultValue().canConvertToLong()) {
Expand All @@ -294,27 +312,8 @@ private Expression generateDefaultInitializerExpression(AbstractJSONSchema2Pojo
} else {
return new NameExpr(value);
}
} else if (prop.getDefaultValue().isArray()) {
// FIXME: this code doesn't compile as it throws an exception
// return new NameExpr(
// "io.fabric8.kubernetes.client.utils.Serialization.jsonMapper().readValue("
// + "\"" + StringEscapeUtils.escapeJava(Serialization.asJson(prop.getDefaultValue())) + "\""
// + ", "
// + prop.getClassType() + ".class"
// + ") } catch (JsonProcessingException e) { }");
return new NameExpr(
"io.fabric8.kubernetes.client.utils.Serialization.unmarshal("
+ "\"" + StringEscapeUtils.escapeJava(Serialization.asJson(prop.getDefaultValue())) + "\""
+ ", "
+ prop.getClassType() + ".class"
+ ")");
} else {
return new NameExpr(
"io.fabric8.kubernetes.client.utils.Serialization.unmarshal("
+ "\"" + StringEscapeUtils.escapeJava(Serialization.asJson(prop.getDefaultValue())) + "\""
+ ", "
+ prop.getClassType() + ".class"
+ ")");
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ void testObjectDefaultFieldsManagement() {

Optional<ClassOrInterfaceDeclaration> clzT = res.getTopLevelClasses().get(2).getCompilationUnit().getClassByName("T");
assertTrue(clzT.isPresent());
assertEquals(2, clzT.get().getFields().size());
assertEquals(2 + 1 /* intialization static field */, clzT.get().getFields().size());

Optional<FieldDeclaration> o1Field = clzT.get().getFieldByName("o1");
assertTrue(o1Field.isPresent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@com.fasterxml.jackson.annotation.JsonProperty("deploymentStrategy")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("The deployment strategy to use to replace existing pods with new ones.\nFor more information see https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#strategy")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private org.test.v1.akkamicroservicespec.DeploymentStrategy deploymentStrategy = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("{\"type\":\"RollingUpdate\",\"rollingUpdate\":{\"maxSurge\":\"25%\",\"maxUnavailable\":\"25%\"}}", org.test.v1.akkamicroservicespec.DeploymentStrategy.class);
private org.test.v1.akkamicroservicespec.DeploymentStrategy deploymentStrategy = _deploymentStrategy;

public org.test.v1.akkamicroservicespec.DeploymentStrategy getDeploymentStrategy() {
return deploymentStrategy;
Expand All @@ -99,6 +99,8 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
this.deploymentStrategy = deploymentStrategy;
}

private static org.test.v1.akkamicroservicespec.DeploymentStrategy _deploymentStrategy = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("{\"type\":\"RollingUpdate\",\"rollingUpdate\":{\"maxSurge\":\"25%\",\"maxUnavailable\":\"25%\"}}", org.test.v1.akkamicroservicespec.DeploymentStrategy.class);

/**
* List of environment variables to set in the container.
*/
Expand Down Expand Up @@ -153,7 +155,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@com.fasterxml.jackson.annotation.JsonProperty("extraVolumeMounts")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("List of extra volume mounts to be provided to the main container.")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private java.util.List<org.test.v1.akkamicroservicespec.ExtraVolumeMounts> extraVolumeMounts = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("[]", java.util.List.class);
private java.util.List<org.test.v1.akkamicroservicespec.ExtraVolumeMounts> extraVolumeMounts = _extraVolumeMounts;

public java.util.List<org.test.v1.akkamicroservicespec.ExtraVolumeMounts> getExtraVolumeMounts() {
return extraVolumeMounts;
Expand All @@ -163,6 +165,8 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
this.extraVolumeMounts = extraVolumeMounts;
}

private static java.util.List<org.test.v1.akkamicroservicespec.ExtraVolumeMounts> _extraVolumeMounts = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("[]", java.util.List.class);

/**
* Enable ingress for gRPC
*/
Expand Down Expand Up @@ -269,7 +273,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@com.fasterxml.jackson.annotation.JsonProperty("imagePullSecrets")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("List of the image pull secrets to be used. More info: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/#create-a-pod-that-uses-your-secret")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private java.util.List<String> imagePullSecrets = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("[]", java.util.List.class);
private java.util.List<String> imagePullSecrets = _imagePullSecrets;

public java.util.List<String> getImagePullSecrets() {
return imagePullSecrets;
Expand All @@ -279,6 +283,8 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
this.imagePullSecrets = imagePullSecrets;
}

private static java.util.List<String> _imagePullSecrets = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("[]", java.util.List.class);

/**
* Additional arguments to the application JVM. It will be added to the `JAVA_TOOL_OPTIONS` environment variable, which will be used by most JVM implementations.
*/
Expand Down Expand Up @@ -349,7 +355,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@com.fasterxml.jackson.annotation.JsonProperty("livenessProbe")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("The liveness probe, can be configured as any standard probe or disabled by using an empty object. More info: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private org.test.v1.akkamicroservicespec.LivenessProbe livenessProbe = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("{\"httpGet\":{\"port\":\"management\",\"path\":\"/alive\"},\"periodSeconds\":10,\"initialDelaySeconds\":20,\"failureThreshold\":10}", org.test.v1.akkamicroservicespec.LivenessProbe.class);
private org.test.v1.akkamicroservicespec.LivenessProbe livenessProbe = _livenessProbe;

public org.test.v1.akkamicroservicespec.LivenessProbe getLivenessProbe() {
return livenessProbe;
Expand All @@ -359,6 +365,8 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
this.livenessProbe = livenessProbe;
}

private static org.test.v1.akkamicroservicespec.LivenessProbe _livenessProbe = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("{\"httpGet\":{\"port\":\"management\",\"path\":\"/alive\"},\"periodSeconds\":10,\"initialDelaySeconds\":20,\"failureThreshold\":10}", org.test.v1.akkamicroservicespec.LivenessProbe.class);

/**
* Name of Secret with entries that will be used as logback.xml configuration.
*/
Expand Down Expand Up @@ -414,7 +422,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@com.fasterxml.jackson.annotation.JsonProperty("readinessProbe")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("The readiness probe, can be configured as any standard probe or disabled by using an empty object. More info: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private org.test.v1.akkamicroservicespec.ReadinessProbe readinessProbe = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("{\"httpGet\":{\"port\":\"management\",\"path\":\"/ready\"},\"periodSeconds\":10,\"initialDelaySeconds\":20,\"failureThreshold\":10}", org.test.v1.akkamicroservicespec.ReadinessProbe.class);
private org.test.v1.akkamicroservicespec.ReadinessProbe readinessProbe = _readinessProbe;

public org.test.v1.akkamicroservicespec.ReadinessProbe getReadinessProbe() {
return readinessProbe;
Expand All @@ -424,6 +432,8 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
this.readinessProbe = readinessProbe;
}

private static org.test.v1.akkamicroservicespec.ReadinessProbe _readinessProbe = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("{\"httpGet\":{\"port\":\"management\",\"path\":\"/ready\"},\"periodSeconds\":10,\"initialDelaySeconds\":20,\"failureThreshold\":10}", org.test.v1.akkamicroservicespec.ReadinessProbe.class);

/**
* Number of desired pods.
*/
Expand All @@ -448,7 +458,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@com.fasterxml.jackson.annotation.JsonProperty("resources")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("ResourceRequirements describes the compute resource requirements.")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private org.test.v1.akkamicroservicespec.Resources resources = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("{\"limits\":{\"memory\":\"1024Mi\",\"cpu\":\"\"},\"requests\":{\"memory\":\"1024Mi\",\"cpu\":\"1\"}}", org.test.v1.akkamicroservicespec.Resources.class);
private org.test.v1.akkamicroservicespec.Resources resources = _resources;

public org.test.v1.akkamicroservicespec.Resources getResources() {
return resources;
Expand All @@ -458,13 +468,15 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
this.resources = resources;
}

private static org.test.v1.akkamicroservicespec.Resources _resources = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("{\"limits\":{\"memory\":\"1024Mi\",\"cpu\":\"\"},\"requests\":{\"memory\":\"1024Mi\",\"cpu\":\"1\"}}", org.test.v1.akkamicroservicespec.Resources.class);

/**
* Scale by Akka Cluster node role. https://doc.akka.io/docs/akka/current/typed/cluster.html#node-roles
*/
@com.fasterxml.jackson.annotation.JsonProperty("roles")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("Scale by Akka Cluster node role. https://doc.akka.io/docs/akka/current/typed/cluster.html#node-roles")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private java.util.List<org.test.v1.akkamicroservicespec.Roles> roles = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("[]", java.util.List.class);
private java.util.List<org.test.v1.akkamicroservicespec.Roles> roles = _roles;

public java.util.List<org.test.v1.akkamicroservicespec.Roles> getRoles() {
return roles;
Expand All @@ -474,13 +486,15 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
this.roles = roles;
}

private static java.util.List<org.test.v1.akkamicroservicespec.Roles> _roles = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("[]", java.util.List.class);

/**
* List of Secrets with entries that will be mounted as files in the mountPath directory.
*/
@com.fasterxml.jackson.annotation.JsonProperty("secretVolumes")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("List of Secrets with entries that will be mounted as files in the mountPath directory.")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private java.util.List<org.test.v1.akkamicroservicespec.SecretVolumes> secretVolumes = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("[]", java.util.List.class);
private java.util.List<org.test.v1.akkamicroservicespec.SecretVolumes> secretVolumes = _secretVolumes;

public java.util.List<org.test.v1.akkamicroservicespec.SecretVolumes> getSecretVolumes() {
return secretVolumes;
Expand All @@ -490,6 +504,8 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
this.secretVolumes = secretVolumes;
}

private static java.util.List<org.test.v1.akkamicroservicespec.SecretVolumes> _secretVolumes = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("[]", java.util.List.class);

/**
* The service account to be used by the microservice, instead of the one generated one. More info: https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void testDefaultValues() throws Exception {
Float six = cr.getSpec().getSix();
Double seven = cr.getSpec().getSeven();
Double eight = cr.getSpec().getEight();
// List nine = cr.getSpec().getNine(); // FIXME
List<String> nine = cr.getSpec().getNine();
Ten ten = cr.getSpec().getTen();

// Assert
Expand All @@ -54,7 +54,9 @@ void testDefaultValues() throws Exception {
assertEquals(6,1f, six);
assertEquals(7.2d, seven);
assertEquals(8.2d, eight);
// assertEquals([asdf], nine); FIXME to be done
assertEquals(2, nine.size());
assertEquals("nine1", nine.get(0));
assertEquals("nine2", nine.get(1));
assertEquals("tenone", ten.getTenOne());
assertEquals("tentwo", ten.getTenTwo());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ spec:
eight:
type: number
default: 8.2
# nine:
# type: array
# items:
# type: string
# default:
# - nine1
# - nine2
nine:
type: array
items:
type: string
default: ['nine1', 'nine2']
ten:
type: object
properties:
Expand Down

0 comments on commit 3480e87

Please sign in to comment.