From e24116032b38169ca95ffac5eb0813dee27020b7 Mon Sep 17 00:00:00 2001 From: Francis Dong Date: Fri, 29 Oct 2021 15:28:06 +0800 Subject: [PATCH] Support using global resource and func in path parameter by json path mapping #370 --- .../main/java/we/fizz/input/PathMapping.java | 50 +++++++++++-------- .../java/we/fizz/input/PathMappingTests.java | 33 +++++++++++- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/fizz-core/src/main/java/we/fizz/input/PathMapping.java b/fizz-core/src/main/java/we/fizz/input/PathMapping.java index 95d4064..b94af30 100644 --- a/fizz-core/src/main/java/we/fizz/input/PathMapping.java +++ b/fizz-core/src/main/java/we/fizz/input/PathMapping.java @@ -217,8 +217,8 @@ public class PathMapping { defaultValue = path.substring(path.indexOf("|") + 1); } ONode val = null; - if (path.startsWith(GLOBAL_RESOURCE_PREFIX)) { - val = select(GlobalResourceService.resNode, p); + if (p.startsWith(GLOBAL_RESOURCE_PREFIX)) { + val = select(GlobalResourceService.resNode, p.substring(GLOBAL_RESOURCE_PREFIX.length())); } else { val = select(ctxNode, handlePath(p)); } @@ -333,25 +333,35 @@ public class PathMapping { * @return */ public static Object getValueByPath(ONode ctxNode, String path) { - if (StringUtils.isBlank(path)) { - return null; +// if (StringUtils.isBlank(path)) { +// return null; +// } +// String p = path; +// String defaultValue = null; +// if (path.indexOf("|") != -1) { +// p = path.substring(0, path.indexOf("|")); +// defaultValue = path.substring(path.indexOf("|") + 1); +// } +// ONode val = null; +// if (p.startsWith(GLOBAL_RESOURCE_PREFIX)) { +// val = select(GlobalResourceService.resNode, p.substring(GLOBAL_RESOURCE_PREFIX.length())); +// } else { +// val = select(ctxNode, handlePath(p)); +// } +// if (val != null && !val.isNull()) { +// return val.toData(); +// } +// return defaultValue; + Object val = getRefValue(ctxNode, null, path); + if (val != null && val instanceof ONode) { + ONode oval = (ONode)val; + if (!oval.isNull()) { + return oval.toData(); + } else { + return val; + } } - String p = path; - String defaultValue = null; - if (path.indexOf("|") != -1) { - p = path.substring(0, path.indexOf("|")); - defaultValue = path.substring(path.indexOf("|") + 1); - } - ONode val = null; - if (path.startsWith(GLOBAL_RESOURCE_PREFIX)) { - val = select(GlobalResourceService.resNode, p); - } else { - val = select(ctxNode, handlePath(p)); - } - if (val != null && !val.isNull()) { - return val.toData(); - } - return defaultValue; + return val; } public static Map getScriptRules(Map rules) { diff --git a/fizz-core/src/test/java/we/fizz/input/PathMappingTests.java b/fizz-core/src/test/java/we/fizz/input/PathMappingTests.java index 4936d2b..1ca3303 100644 --- a/fizz-core/src/test/java/we/fizz/input/PathMappingTests.java +++ b/fizz-core/src/test/java/we/fizz/input/PathMappingTests.java @@ -16,6 +16,7 @@ import org.noear.snack.ONode; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import we.global_resource.GlobalResourceService; class PathMappingTests { @@ -188,8 +189,36 @@ class PathMappingTests { assertEquals("0", (String)abcVal1); Object abcVal2 = PathMapping.getValueByPath(ctxNode, "data.arr[-1]"); assertEquals("4", (String)abcVal2); - System.out.println(abcVal1); - System.out.println(abcVal2); + } + + @Test + void testGlobalResource() { + ONode resNode = ONode.load(new HashMap()); + + Map m = new HashMap<>(); + m.put("a", "1"); + m.put("b", "1"); + + List list = new ArrayList<>(); + list.add("0"); + list.add("1"); + list.add("2"); + list.add("3"); + list.add("4"); + + + PathMapping.setByPath(resNode, "data.m", m, true); + + PathMapping.setByPath(resNode, "data.arr", list, true); + + GlobalResourceService.resNode = resNode; + + ONode emptyCtx = ONode.load(new HashMap()); + + Object abcVal1 = PathMapping.getValueByPath(emptyCtx, "g.data.arr[0]"); + assertEquals("0", (String)abcVal1); + Object abcVal2 = PathMapping.getValueByPath(emptyCtx, "g.data.arr[-1]"); + assertEquals("4", (String)abcVal2); } } \ No newline at end of file