From 298d464689005b2fa82adced6936066bd287306d Mon Sep 17 00:00:00 2001 From: Ioannis Canellos Date: Thu, 16 Apr 2020 17:15:29 +0300 Subject: [PATCH] feat: Use own ArrayOrString in v1beta1 --- extensions/tekton/model/pom.xml | 1 + .../pipeline/v1beta1/ArrayOrString.java | 184 ++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 extensions/tekton/model/src/main/java/io/fabric8/tekton/pipeline/v1beta1/ArrayOrString.java diff --git a/extensions/tekton/model/pom.xml b/extensions/tekton/model/pom.xml index 01e64750cb0..3a0b0ad7bcb 100644 --- a/extensions/tekton/model/pom.xml +++ b/extensions/tekton/model/pom.xml @@ -129,6 +129,7 @@ removing the duplicate generated calss + diff --git a/extensions/tekton/model/src/main/java/io/fabric8/tekton/pipeline/v1beta1/ArrayOrString.java b/extensions/tekton/model/src/main/java/io/fabric8/tekton/pipeline/v1beta1/ArrayOrString.java new file mode 100644 index 00000000000..082dac981ad --- /dev/null +++ b/extensions/tekton/model/src/main/java/io/fabric8/tekton/pipeline/v1beta1/ArrayOrString.java @@ -0,0 +1,184 @@ +/** + * 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.tekton.pipeline.v1beta1; + + +import com.fasterxml.jackson.annotation.*; +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.*; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import io.fabric8.kubernetes.api.model.Doneable; +import io.fabric8.kubernetes.api.model.KubernetesResource; +import io.sundr.builder.annotations.Buildable; +import io.sundr.builder.annotations.Inline; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +import java.io.IOException; +import java.util.*; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "arrayVal", + "stringVal", + "type" +}) +@JsonDeserialize(using = ArrayOrString.Deserializer.class) +@JsonSerialize(using = ArrayOrString.Serializer.class) +@ToString +@EqualsAndHashCode +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder", inline = @Inline(type = Doneable.class, prefix = "Doneable", value = "done")) +public class ArrayOrString implements KubernetesResource { + + private static final String TYPE_STRING = "string"; + private static final String TYPE_ARRAY = "array"; + + @JsonProperty("arrayVal") + private List arrayVal = new ArrayList(); + + @JsonProperty("stringVal") + private String stringVal; + + @JsonProperty("type") + private String type; + + @JsonIgnore + private Map additionalProperties = new HashMap(); + + /** + * No args constructor for use in serialization + */ + public ArrayOrString() { + } + + //Builders are generated for the first non-empty constructor found. + public ArrayOrString(List arrayVal, String stringVal, String type, Map additionalProperties) { + this.arrayVal = arrayVal; + this.stringVal = stringVal; + this.type = type; + this.additionalProperties = additionalProperties; + } + + public ArrayOrString(String stringVal) { + this(null, stringVal, TYPE_STRING, new HashMap<>()); + } + + public ArrayOrString(List arrayVal) { + this(arrayVal, null, TYPE_ARRAY, new HashMap<>()); + } + + @JsonProperty("arrayVal") + public List getArrayVal() { + return arrayVal; + } + + @JsonProperty("arrayVal") + public void setArrayVal(List arrayVal) { + this.arrayVal = arrayVal; + } + + @JsonProperty("stringVal") + public String getStringVal() { + return stringVal; + } + + @JsonProperty("stringVal") + public void setStringVal(String stringVal) { + this.stringVal = stringVal; + } + + @JsonProperty("type") + public String getType() { + return type; + } + + @JsonProperty("type") + public void setType(String type) { + this.type = type; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + public static class Serializer extends JsonSerializer { + + @Override + public void serialize(ArrayOrString value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { + if (value != null) { + if (value.getType() == null) { + String stringVal = value.getStringVal(); + if (stringVal != null) { + jgen.writeString(stringVal); + } else { + List arrayVal = value.getArrayVal(); + if (arrayVal != null) { + writeArray(value, jgen); + } else { + jgen.writeNull(); + } + } + } else if (Objects.equals(value.getType(), TYPE_STRING)) { + jgen.writeString(value.stringVal); + } else if (Objects.equals(value.getType(), TYPE_ARRAY)) { + writeArray(value, jgen); + } else { + jgen.writeNull(); + } + } else { + jgen.writeNull(); + } + } + + private void writeArray(ArrayOrString value, JsonGenerator jgen) throws IOException { + jgen.writeStartArray(value.getArrayVal().size()); + for (String n : value.getArrayVal()) { + jgen.writeString(n); + } + jgen.writeEndArray(); + } + + } + + public static class Deserializer extends JsonDeserializer { + + @Override + public ArrayOrString deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException { + ObjectCodec oc = jsonParser.getCodec(); + JsonNode node = oc.readTree(jsonParser); + ArrayOrString arrayOrString; + if (node.isArray()) { + List elements = new ArrayList<>(); + node.elements().forEachRemaining(n -> elements.add(n.asText())); + arrayOrString = new ArrayOrString(elements); + } else { + arrayOrString = new ArrayOrString(node.asText()); + } + return arrayOrString; + } + + } +}