From d677e40c14e7a93b10eefed245e582ae3bad6e5d Mon Sep 17 00:00:00 2001 From: acetousk Date: Fri, 23 Aug 2024 16:33:49 -0600 Subject: [PATCH] Add flattenNestedMapWithKeys method for stripe conversion to application/x-www-form-urlencoded --- .../org/moqui/util/CollectionUtilities.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/framework/src/main/java/org/moqui/util/CollectionUtilities.java b/framework/src/main/java/org/moqui/util/CollectionUtilities.java index 3252956a8..ea7fe9459 100644 --- a/framework/src/main/java/org/moqui/util/CollectionUtilities.java +++ b/framework/src/main/java/org/moqui/util/CollectionUtilities.java @@ -460,6 +460,39 @@ public static Map flattenNestedMap(Map theMap) { return outMap; } + public static Map flattenNestedMapWithKeys(Map theMap) { + return flattenNestedMapWithKeys(theMap, ""); + } + + @SuppressWarnings("unchecked") + private static Map flattenNestedMapWithKeys(Map theMap, String parentKey) { + Map output = new LinkedHashMap<>(); + + if (theMap == null) return output; + + for (Map.Entry entry : theMap.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + String newKey = parentKey.isEmpty() ? key : parentKey + "[" + key + "]"; + + if (value instanceof Map) { + output.putAll(flattenNestedMapWithKeys((Map) value, newKey)); + } else if (value instanceof Collection) { + int index = 0; + for (Object colValue : (Collection) value) { + if (colValue instanceof Map) { + output.putAll(flattenNestedMapWithKeys((Map) colValue, newKey + "[" + index + "]")); + } else { + output.put(newKey + "[" + index + "]", colValue.toString()); + } + index++; + } + } else { + output.put(newKey, value.toString()); + } + } + return output; + } @SuppressWarnings("unchecked") public static void mergeNestedMap(Map baseMap, Map overrideMap, boolean overrideEmpty) { if (baseMap == null || overrideMap == null) return;