Skip to content

Commit

Permalink
Merge branch 'moqui:master' into acetousk
Browse files Browse the repository at this point in the history
  • Loading branch information
acetousk authored Aug 31, 2024
2 parents d959574 + d677e40 commit a7e7df2
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 a7e7df2

Please sign in to comment.