You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "gyfora (via GitHub)" <gi...@apache.org> on 2023/03/29 13:03:49 UTC

[GitHub] [flink-kubernetes-operator] gyfora commented on a diff in pull request #535: [FLINK-30786] Support merge by name for podTemplate array fields

gyfora commented on code in PR #535:
URL: https://github.com/apache/flink-kubernetes-operator/pull/535#discussion_r1151911671


##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/utils/FlinkUtils.java:
##########
@@ -106,6 +99,54 @@ private static void mergeInto(JsonNode toNode, JsonNode fromNode) {
         }
     }
 
+    private static void mergeArray(
+            ArrayNode toChildNode, ArrayNode fromChildNode, boolean mergeArraysByName) {
+        if (namesDefined(toChildNode) && namesDefined(fromChildNode) && mergeArraysByName) {
+            var toGrouped = groupByName(toChildNode);
+            var fromGrouped = groupByName(fromChildNode);
+            fromGrouped.forEach(
+                    (name, fromElement) ->
+                            toGrouped.compute(
+                                    name,
+                                    (n, toElement) -> {
+                                        if (toElement == null) {
+                                            return fromElement;
+                                        }
+                                        mergeInto(toElement, fromElement, mergeArraysByName);
+                                        return toElement;
+                                    }));
+
+            toChildNode.removeAll();
+            toGrouped.forEach((n, e) -> toChildNode.add(e));
+        } else {
+            for (int i = 0; i < fromChildNode.size(); i++) {
+                JsonNode updatedChildNode = fromChildNode.get(i);
+                if (toChildNode.size() <= i) {
+                    // append new node
+                    toChildNode.add(updatedChildNode);
+                }
+                mergeInto(toChildNode.get(i), updatedChildNode, mergeArraysByName);
+            }
+        }
+    }
+
+    private static boolean namesDefined(ArrayNode node) {
+        var it = node.elements();
+        while (it.hasNext()) {
+            var next = it.next();
+            if (!next.has("name")) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static Map<String, ObjectNode> groupByName(ArrayNode node) {
+        var out = new LinkedHashMap<String, ObjectNode>();
+        node.elements().forEachRemaining(e -> out.put((e.get("name").asText()), (ObjectNode) e));

Review Comment:
   yes, we only call this if all elements have a name (and thus they are all ObjectsNodes)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org