Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor (kubernetes-model-core) : Refactor IntOrString class to avoid invalid field combinations #3806

Merged
merged 1 commit into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#### Improvements
* Fix #3811: Reintroduce `Replaceable` interface in `NonNamespaceOperation`
* Remove `setIntVal`, `setStrVal`, `setKind` setters from `IntOrString` class to avoid invalid combinations

#### Dependency Upgrade
* Fix #3788: Point CamelK Extension model to latest released version v1.8.0
Expand Down
33 changes: 33 additions & 0 deletions doc/MIGRATION-v6.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# Migration from 5.x to 6.x

## Contents:
- [IntOrString changes](#intorstring-changes)


## IntOrString changes

We've removed setter methods `setIntVal`, `setKind`, `setStrVal` from the class. You'd need to rely on constructors or builders for creating `IntOrString` object. Here are some examples:

- Creating an Integer based object:
```java
// Creating using Constructor
IntOrString i1 = new IntOrString(3000);
// Creating using Builder
IntOrString i2 = new IntOrStringBuilder().withValue(89).build();
```
- Creating a String based object:
```java
// Creating using Constructor
IntOrString i1 = new IntOrString("3000");
// Creating using Builder
IntOrString i2 = new IntOrStringBuilder().withValue("89").build();
```
- You can rely on existing `getIntVal`, `getStrVal` methods to get Integer or String values respectively:
```java
// Get Integer values:
IntOrString i1 = new IntOrString(3000);
Integer intValue = i1.getIntVal();

// Get String values:
IntOrString i2 = new IntOrString("3000");
String strValue = i2.getStrVal();
```
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void onClose(WatcherException e) {
client.services().inNamespace("thisisatest").create(new ServiceBuilder()
.withNewMetadata().withName("testservice").endMetadata()
.withNewSpec()
.addNewPort().withPort(80).withNewTargetPort().withIntVal(80).endTargetPort().endPort()
.addNewPort().withPort(80).withNewTargetPort(80).endPort()
.endSpec()
.build()));
log("Updated service", client.services().inNamespace("thisisatest").withName("testservice").edit(s -> new ServiceBuilder(s).editMetadata().addToLabels("test", "label").endMetadata().build()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@
*/
package io.fabric8.kubernetes.api.model;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
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.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
Expand All @@ -40,171 +34,103 @@

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonDeserialize(using = IntOrString.Deserializer.class)
@JsonSerialize(using = IntOrString.Serializer.class)
@JsonPropertyOrder({
"IntVal",
"Kind",
"StrVal"
})
@ToString
@EqualsAndHashCode
@Setter
@Accessors(prefix = {
"_",
""
})
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage=true, builderPackage = "io.fabric8.kubernetes.api.builder")
public class IntOrString implements Serializable {

@JsonProperty("IntVal")
private Integer IntVal;
@JsonProperty("Kind")
private Integer Kind;
@JsonProperty("StrVal")
private String StrVal;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<>();

public IntOrString() {
}

//Builders are generated for the first non-empty constructor found.
public IntOrString(Integer intVal, Integer kind, String strVal, Map<String, Object> additionalProperties) {
IntVal = intVal;
Kind = kind;
StrVal = strVal;
this.additionalProperties = additionalProperties;
}
private Object value;

public IntOrString(Integer intVal) {
this(intVal, 0, null, new HashMap<>());
}
public IntOrString() { }

public IntOrString(String strVal) {
this(null, 1, strVal, new HashMap<>());
//Builders are generated for the first non-empty constructor found.
@Buildable(editableEnabled = false, generateBuilderPackage=true, builderPackage = "io.fabric8.kubernetes.api.builder")
public IntOrString(Object value) {
if (value instanceof Integer || value instanceof String) {
this.value = value;
} else {
throw new IllegalArgumentException("Either integer or string value needs to be provided");
}


/**
*
* @return
* The IntVal
*/
@JsonProperty("IntVal")
public Integer getIntVal() {
return IntVal;
}

/**
* Get Raw enclosed object.
*
* @return Object value
*/
public Object getValue() {
return value;
}

/**
* Get Integer value
*
* @return Integer value if set
*/
public Integer getIntVal() {
if (value instanceof Integer) {
return (Integer) value;
}

/**
*
* @param IntVal
* The IntVal
*/
@JsonProperty("IntVal")
public void setIntVal(Integer IntVal) {
this.IntVal = IntVal;
}

/**
*
* @return
* The Kind
*/
@JsonProperty("Kind")
public Integer getKind() {
return Kind;
}

/**
*
* @param Kind
* The Kind
*/
@JsonProperty("Kind")
public void setKind(Integer Kind) {
this.Kind = Kind;
}

/**
*
* @return
* The StrVal
*/
@JsonProperty("StrVal")
public String getStrVal() {
return StrVal;
return null;
}

/**
* Get String value
*
* @return string value if set
*/
public String getStrVal() {
if (value instanceof String) {
return (String) value;
}

/**
*
* @param StrVal
* The StrVal
*/
@JsonProperty("StrVal")
public void setStrVal(String StrVal) {
this.StrVal = StrVal;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

public static class Serializer extends JsonSerializer<IntOrString> {

@Override
public void serialize(IntOrString value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
if (value != null) {
if (value.getKind() == null) {
Integer intValue = value.getIntVal();
if (intValue != null) {
jgen.writeNumber(intValue);
} else {
String stringValue = value.getStrVal();
if (stringValue != null) {
jgen.writeString(stringValue);
} else {
jgen.writeNull();
}
}
} else if (value.getKind() == 0) {
jgen.writeNumber(value.getIntVal());
} else if (value.getKind() == 1) {
jgen.writeString(value.getStrVal());
} else {
jgen.writeNull();
}
} else {
jgen.writeNull();
}
return null;
}

public static class Serializer extends JsonSerializer<IntOrString> {

@Override
public void serialize(IntOrString value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (value != null) {
Integer intValue = value.getIntVal();
if (intValue != null) {
jgen.writeNumber(intValue);
} else {
String stringValue = value.getStrVal();
if (stringValue != null) {
jgen.writeString(stringValue);
} else {
jgen.writeNull();
}
}

} else {
jgen.writeNull();
}
}

public static class Deserializer extends JsonDeserializer<IntOrString> {

@Override
public IntOrString deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
IntOrString intOrString;
if (node.isInt()) {
intOrString = new IntOrString(node.asInt());
} else {
intOrString = new IntOrString(node.asText());
}
return intOrString;
}

}

public static class Deserializer extends JsonDeserializer<IntOrString> {

@Override
public IntOrString deserialize(JsonParser jsonParser, DeserializationContext ctxt)
throws IOException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
IntOrString intOrString;
if (node.isInt()) {
intOrString = new IntOrString(node.asInt());
} else {
intOrString = new IntOrString(node.asText());
}
return intOrString;
}

}
}
Loading