Skip to content

Commit

Permalink
making pojos editable and adds a toBuiler method alias
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Sep 11, 2023
1 parent a7e7014 commit b8c6728
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Fix #5368: added support for additional ListOptions fields
* Fix #5377: added a createOr and unlock function to provide a straight-forward replacement for createOrReplace.
* Fix #5388: [crd-generator] Generate deterministic CRDs
* Fix #5135: added per instance methods to create a builder - instead on new PodBuilder(pod), you may use pod.toBuilder()
* Fix #5257: Add ErrorStreamMessage and StatusStreamMessage to ease mocking of pods/exec requests

#### Dependency Upgrade
Expand Down
27 changes: 26 additions & 1 deletion kubernetes-model-generator/kubernetes-model-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,36 @@
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Import-Package>*</Import-Package>
<Export-Package>
io.fabric8.kubernetes.model**
io.fabric8.kubernetes.model**,
io.fabric8.kubernetes.api.builder**
</Export-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<echo>Removing Unneeded Classes</echo>
<delete
file="target/classes/io/fabric8/kubernetes/model/util/Dummy.class"
verbose="true" />
<delete verbose="true">
<fileset dir="target/generated-sources/annotations/io/fabric8/kubernetes/model/util">
<include name="*.java" />
</fileset>
</delete>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.fabric8.kubernetes.model.util;

import io.sundr.builder.annotations.Buildable;

/**
* Only exists to trigger builder package generation
*/
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder")
class Dummy {

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
@ToString
@Setter
@EqualsAndHashCode
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder")
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public class AnyType implements Serializable {

protected Object value;
Expand All @@ -50,7 +50,7 @@ public AnyType() {

@JsonCreator
//Builders are generated for the first non-empty constructor found.
@Buildable(editableEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder")
@Buildable(editableEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public AnyType(Object value) {
this.value = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@Setter
@ToString
@EqualsAndHashCode
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder")
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public class GenericKubernetesResource implements HasMetadata {

private static final ObjectMapper MAPPER = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public IntOrString() {

@JsonCreator
//Builders are generated for the first non-empty constructor found.
@Buildable(editableEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder")
@Buildable(editableEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public IntOrString(Object value) {
setValue(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
@Group("")
@Kind("List")
@JsonDeserialize(using = JsonDeserializer.None.class)
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder")
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public class KubernetesList extends DefaultKubernetesResourceList<HasMetadata> implements KubernetesResource {

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"_",
""
})
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder")
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public class Quantity implements Serializable {
private String amount;
private String format = "";
Expand Down Expand Up @@ -156,7 +156,7 @@ public static BigDecimal getAmountInBytes(Quantity quantity) throws ArithmeticEx
/**
* Constructs a new Quantity from the provided amountInBytes. This amount is converted
* to a value with the unit provided in desiredFormat.
*
*
* @param amountInBytes
* @param desiredFormat
* @see #getNumericalAmount()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@ToString(callSuper = true)
@JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class)
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder")
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public class RawExtension extends AnyType implements KubernetesResource {

public RawExtension() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.kubernetes.jsonschema2pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
Expand All @@ -24,12 +25,18 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.sun.codemodel.JAnnotationArrayMember;
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpressionImpl;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JFormatter;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import io.fabric8.kubernetes.api.builder.Editable;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;
import io.fabric8.kubernetes.model.jackson.JsonUnwrappedDeserializer;
Expand All @@ -55,6 +62,8 @@

public class KubernetesCoreTypeAnnotator extends Jackson2Annotator {

private static final String BUILDER_PACKAGE = "io.fabric8.kubernetes.api.builder";

public static final String BUILDABLE_REFERENCE_VALUE = "value";

protected static final String ANNOTATION_VALUE = "value";
Expand Down Expand Up @@ -97,6 +106,7 @@ public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) {
clazz.annotate(EqualsAndHashCode.class);
clazz.annotate(Setter.class);
clazz.annotate(Accessors.class).paramArray("prefix").param("_").param("");
makeEditable(clazz);
processBuildable(clazz);

final Map<String, JFieldVar> fields = clazz.fields();
Expand Down Expand Up @@ -135,6 +145,19 @@ public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) {
}
}

private void makeEditable(JDefinedClass clazz) {
JClass builderType = clazz.owner().ref(clazz.fullName() + "Builder");
JClass editableType = clazz.owner().ref(Editable.class).narrow(builderType);
clazz._implements(editableType);
JMethod editMethod = clazz.method(JMod.PUBLIC , builderType, "edit");
editMethod.annotate(JsonIgnore.class);
JInvocation newBuilder = JExpr._new(builderType).arg(JExpr._this());
editMethod.body()._return(newBuilder);
JMethod toBuilderMethod = clazz.method(JMod.PUBLIC , builderType, "toBuilder");
toBuilderMethod.annotate(JsonIgnore.class);
toBuilderMethod.body()._return(JExpr.invoke("edit"));
}

private void annotate(JDefinedClass clazz, String apiVersion, String apiGroup) {
clazz.annotate(Version.class).param(ANNOTATION_VALUE, apiVersion);
clazz.annotate(Group.class).param(ANNOTATION_VALUE, apiGroup);
Expand Down Expand Up @@ -199,7 +222,7 @@ protected void processBuildable(JDefinedClass clazz) {
.param("validationEnabled", false)
.param("generateBuilderPackage", generateBuilderPackage())
.param("lazyCollectionInitEnabled", false)
.param("builderPackage", "io.fabric8.kubernetes.api.builder");
.param("builderPackage", BUILDER_PACKAGE);

List<String> types = new ArrayList<>();
addBuildableTypes(clazz, types);
Expand All @@ -217,7 +240,7 @@ protected void processBuildable(JDefinedClass clazz) {
}

protected boolean generateBuilderPackage() {
return true;
return false;
}

protected void addBuildableTypes(JDefinedClass clazz, List<String> types) {
Expand Down

0 comments on commit b8c6728

Please sign in to comment.