All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.kubernetes.api.model.GenericKubernetesResource Maven / Gradle / Ivy

/**
 * 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.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.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.sundr.builder.annotations.Buildable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

@JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
  "apiVersion",
  "kind",
  "metadata",
  "items",
})
@Getter
@Setter
@ToString
@EqualsAndHashCode
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public class GenericKubernetesResource implements HasMetadata {

  private static final ObjectMapper MAPPER = new ObjectMapper();

  @JsonProperty("apiVersion")
  private String apiVersion;
  @JsonProperty("kind")
  private String kind;
  @JsonProperty("metadata")
  private ObjectMeta metadata;
  @JsonIgnore
  private Map additionalProperties = new LinkedHashMap<>();

  @JsonAnyGetter
  public Map getAdditionalProperties() {
    return this.additionalProperties;
  }

  public void setAdditionalProperties(Map additionalProperties) {
    this.additionalProperties = additionalProperties;
  }

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

  @JsonIgnore
  public JsonNode getAdditionalPropertiesNode() {
    return MAPPER.convertValue(getAdditionalProperties(), JsonNode.class);
  }

  /**
   * Allows the retrieval of field values from this Resource for the provided path segments.
   *
   * 

If the path segment is of type {@link Integer}, then we assume that it is an array index to retrieve * the value of an entry in the array. * *

If the path segment is of type {@link String}, then we assume that it is a field name to retrieve the value * from the resource. * *

In any other case, the path segment is ignored and considered invalid. The method returns null. * *

Considering the following JSON object: * *

{@code
   * {
   *   "field": {
   *     "value": 42
   *     "list": [
   *       {entry: 1}, {entry: 2}, {entry: 3}
   *     ],
   *     "1": "one"
   *   }
   * }
   * }
* *

The following invocations will produce the documented results: *

    *
  • {@code get("field", "value")} will result in {@code 42}
  • *
  • {@code get("field", "1")} will result in {@code "one"}
  • *
  • {@code get("field", 1)} will result in {@code null}
  • *
  • {@code get("field", "list", 1, "entry")} will result in {@code 2}
  • *
  • {@code get("field", "list", 99, "entry")} will result in {@code null}
  • *
  • {@code get("field", "list", "1", "entry")} will result in {@code null}
  • *
  • {@code get("field", "list", 1, false)} will result in {@code null}
  • *
* * @param path of the field to retrieve. * @param type of the returned object. * @return the value of the traversed path or null if the field does not exist. */ @SuppressWarnings("unchecked") public T get(Object... path) { Object current = getAdditionalProperties(); for (Object segment : path) { if (segment instanceof Integer && current instanceof Collection && ((Collection)current).size() > (int)segment) { current = ((Collection) current).toArray()[(int)segment]; } else if (segment instanceof String && current instanceof Map) { current = ((Map) current).get(segment.toString()); } else { return null; } } return (T) current; } }