Support using global resource and func in path parameter by json path mapping #370

This commit is contained in:
Francis Dong
2021-10-29 15:28:06 +08:00
committed by dxfeng10
parent da8522e903
commit e24116032b
2 changed files with 61 additions and 22 deletions

View File

@@ -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<String, Object> getScriptRules(Map<String, Object> rules) {

View File

@@ -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<String, Object> m = new HashMap<>();
m.put("a", "1");
m.put("b", "1");
List<String> 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);
}
}