Skip to content

Commit

Permalink
Add flattenNestedMapWithKeys method for stripe conversion to applicat…
Browse files Browse the repository at this point in the history
…ion/x-www-form-urlencoded
  • Loading branch information
acetousk authored and dixitdeepak committed Dec 2, 2024
1 parent a4acb92 commit 3bd5d16
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions framework/src/main/java/org/moqui/util/CollectionUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,39 @@ public static Map flattenNestedMap(Map theMap) {
return outMap;
}

public static Map<String, String> flattenNestedMapWithKeys(Map<String, Object> theMap) {
return flattenNestedMapWithKeys(theMap, "");
}

@SuppressWarnings("unchecked")
private static Map<String, String> flattenNestedMapWithKeys(Map<String, Object> theMap, String parentKey) {
Map<String, String> output = new LinkedHashMap<>();

if (theMap == null) return output;

for (Map.Entry<String, Object> 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<String, Object>) value, newKey));
} else if (value instanceof Collection) {
int index = 0;
for (Object colValue : (Collection<?>) value) {
if (colValue instanceof Map) {
output.putAll(flattenNestedMapWithKeys((Map<String, Object>) 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<Object, Object> baseMap, Map<Object, Object> overrideMap, boolean overrideEmpty) {
if (baseMap == null || overrideMap == null) return;
Expand Down

0 comments on commit 3bd5d16

Please sign in to comment.