Support default value in jsonpath mapping #216

This commit is contained in:
Francis Dong
2021-07-08 12:01:27 +08:00
committed by dxfeng10
parent f92eaff792
commit 9541ccfae4
2 changed files with 48 additions and 12 deletions

View File

@@ -135,39 +135,73 @@ public class PathMapping {
String starEntryKey = null; String starEntryKey = null;
for (Entry<String, String> entry : rs.entrySet()) { for (Entry<String, String> entry : rs.entrySet()) {
String path = handlePath(entry.getValue()); String path = entry.getValue();
ONode val = select(ctxNode, path); String p = path;
Object obj = val; String defaultValue = null;
if (val != null && types.containsKey(entry.getKey())) { if (path.indexOf("|") != -1) {
p = path.substring(0, path.indexOf("|"));
defaultValue = path.substring(path.indexOf("|") + 1);
}
ONode val = select(ctxNode, handlePath(p));
Object obj = null;
if (val != null && !val.isNull()) {
obj = val;
} else {
obj = defaultValue;
}
if (obj != null && types.containsKey(entry.getKey())) {
switch (types.get(entry.getKey())) { switch (types.get(entry.getKey())) {
case "Integer": case "Integer":
case "int": { case "int": {
obj = val.val().isNull() ? null : val.val().getInt(); if (obj instanceof ONode) {
obj = ((ONode) obj).val().getInt();
} else {
obj = Integer.valueOf(obj.toString());
}
break; break;
} }
case "Boolean": case "Boolean":
case "boolean": { case "boolean": {
obj = val.val().isNull() ? null : val.val().getBoolean(); if (obj instanceof ONode) {
obj = ((ONode) obj).val().getBoolean();
} else {
obj = Boolean.valueOf(obj.toString());
}
break; break;
} }
case "Float": case "Float":
case "float": { case "float": {
obj = val.val().isNull() ? null : val.val().getFloat(); if (obj instanceof ONode) {
obj = ((ONode) obj).val().getFloat();
} else {
obj = Float.valueOf(obj.toString());
}
break; break;
} }
case "Double": case "Double":
case "double": { case "double": {
obj = val.val().isNull() ? null : val.val().getDouble(); if (obj instanceof ONode) {
obj = ((ONode) obj).val().getDouble();
} else {
obj = Double.valueOf(obj.toString());
}
break; break;
} }
case "String": case "String":
case "string": { case "string": {
obj = val.val().isNull() ? null : val.val().getString(); if (obj instanceof ONode) {
obj = ((ONode) obj).val().getString();
}
break; break;
} }
case "Long": case "Long":
case "long": { case "long": {
obj = val.val().isNull() ? null : val.val().getLong(); if (obj instanceof ONode) {
obj = ((ONode) obj).val().getLong();
} else {
obj = Long.valueOf(obj.toString());
}
break; break;
} }
} }
@@ -175,7 +209,7 @@ public class PathMapping {
if (CommonConstants.WILDCARD_STAR.equals(entry.getKey())) { if (CommonConstants.WILDCARD_STAR.equals(entry.getKey())) {
starValObj = obj; starValObj = obj;
starEntryKey = entry.getKey(); starEntryKey = entry.getKey();
}else { } else {
setByPath(target, entry.getKey(), obj, supportMultiLevels); setByPath(target, entry.getKey(), obj, supportMultiLevels);
} }
} }

View File

@@ -14,6 +14,9 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.noear.snack.ONode; import org.noear.snack.ONode;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
class PathMappingTests { class PathMappingTests {
@Test @Test
@@ -159,7 +162,6 @@ class PathMappingTests {
assertEquals("1", (String)abcVal1); assertEquals("1", (String)abcVal1);
assertEquals("123456", (String)abcVal2); assertEquals("123456", (String)abcVal2);
} }