|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.kubernetes.operator.api.spec; |
| 19 | + |
| 20 | +import org.apache.flink.configuration.Configuration; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.databind.JsonNode; |
| 23 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 24 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Iterator; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** Allows parsing configurations as YAML, and adds related utility methods. */ |
| 32 | +public class ConfigObjectNode extends ObjectNode { |
| 33 | + |
| 34 | + public ConfigObjectNode() { |
| 35 | + this(JsonNodeFactory.instance); |
| 36 | + } |
| 37 | + |
| 38 | + public ConfigObjectNode(JsonNodeFactory nc, Map<String, JsonNode> kids) { |
| 39 | + super(nc, kids); |
| 40 | + } |
| 41 | + |
| 42 | + public ConfigObjectNode(JsonNodeFactory nc) { |
| 43 | + super(nc); |
| 44 | + } |
| 45 | + |
| 46 | + public void remove(String... names) { |
| 47 | + remove(Arrays.asList(names)); |
| 48 | + } |
| 49 | + |
| 50 | + public void putAllFrom(Map<String, String> value) { |
| 51 | + value.forEach(this::put); |
| 52 | + } |
| 53 | + |
| 54 | + public void setAllFrom(Map<String, String> value) { |
| 55 | + removeAll(); |
| 56 | + putAllFrom(value); |
| 57 | + } |
| 58 | + |
| 59 | + public Map<String, String> asFlatMap() { |
| 60 | + Map<String, String> flatMap = new HashMap<>(); |
| 61 | + flattenHelper(this, "", flatMap); |
| 62 | + return flatMap; |
| 63 | + } |
| 64 | + |
| 65 | + public Configuration asConfiguration() { |
| 66 | + return Configuration.fromMap(asFlatMap()); |
| 67 | + } |
| 68 | + |
| 69 | + private static void flattenHelper( |
| 70 | + JsonNode node, String parentKey, Map<String, String> flatMap) { |
| 71 | + if (node.isObject()) { |
| 72 | + Iterator<Map.Entry<String, JsonNode>> fields = node.fields(); |
| 73 | + while (fields.hasNext()) { |
| 74 | + Map.Entry<String, JsonNode> field = fields.next(); |
| 75 | + String newKey = |
| 76 | + parentKey.isEmpty() ? field.getKey() : parentKey + "." + field.getKey(); |
| 77 | + flattenHelper(field.getValue(), newKey, flatMap); |
| 78 | + } |
| 79 | + } else if (node.isArray()) { |
| 80 | + for (int i = 0; i < node.size(); i++) { |
| 81 | + String newKey = parentKey + "[" + i + "]"; |
| 82 | + flattenHelper(node.get(i), newKey, flatMap); |
| 83 | + } |
| 84 | + } else { |
| 85 | + // Store values as strings |
| 86 | + flatMap.put(parentKey, node.asText()); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments