diff --git a/sdk/common/src/main/java/io/dataease/utils/MappingUtils.java b/sdk/common/src/main/java/io/dataease/utils/MappingUtils.java new file mode 100644 index 0000000000..28bb398232 --- /dev/null +++ b/sdk/common/src/main/java/io/dataease/utils/MappingUtils.java @@ -0,0 +1,41 @@ +package io.dataease.utils; + +import java.util.HashMap; +import java.util.Map; + +public class MappingUtils { + + public static Map mapNestedUserData(Map userMap, Map mappingMap) { + + Map resultMap = new HashMap<>(); + mappingMap.forEach((targetKey, sourcePath) -> { + Object value = getNestedValue(userMap, sourcePath); + if (value != null) { + resultMap.put(targetKey, value.toString()); + } + }); + + return resultMap; + } + + private static Object getNestedValue(Map sourceMap, String path) { + String[] keys = path.split("\\."); + Object current = sourceMap; + + for (String key : keys) { + if (!(current instanceof Map)) { + return null; + } + @SuppressWarnings("unchecked") + Map currentMap = (Map) current; + current = currentMap.get(key); + + if (current == null) { + return null; + } + } + + return current; + } + +}