Fix the issue that join function of list will throw exception while source record not exist in dest #372

This commit is contained in:
Francis Dong
2021-11-11 11:33:22 +08:00
committed by dxfeng10
parent 7721582b6b
commit b7e98ca6e3
2 changed files with 90 additions and 8 deletions

View File

@@ -25,6 +25,8 @@ import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import we.fizz.exception.FizzRuntimeException;
/**
* List Functions
*
@@ -127,7 +129,9 @@ public class ListFunc implements IFunc {
*
* @param dest destination list
* @param src source list
* @param joinField join field
* @param joinField join field, pattern: joinFieldOfDest:joinFieldOfSrc,
* :joinFieldOfSrc could be omitted if both join field names
* are the same
* @param fields fields which will be merge to destination list, all fields
* will be merged if it is null
* @return
@@ -137,18 +141,23 @@ public class ListFunc implements IFunc {
if (dest == null || dest.size() == 0 || src == null || src.size() == 0) {
return dest;
}
String[] joinFields = joinField.split(":");
if (joinFields.length == 1) {
joinFields = new String[] {joinField, joinField};
}
Map<String, Map<String, Object>> index = new HashMap<>();
for (Map<String, Object> record : dest) {
if (record.get(joinField) != null) {
index.put(record.get(joinField).toString(), record);
if (record.get(joinFields[0]) != null) {
index.put(record.get(joinFields[0]).toString(), record);
}
}
for (Map<String, Object> m : src) {
if (m.get(joinField) == null) {
Object srcJoinFieldVal = m.get(joinFields[1]);
if (srcJoinFieldVal == null || !index.containsKey(srcJoinFieldVal.toString())) {
continue;
}
Map<String, Object> record = index.get(m.get(joinField).toString());
Map<String, Object> record = index.get(srcJoinFieldVal.toString());
if (fields == null || fields.length == 0) {
record.putAll(m);

View File

@@ -166,9 +166,82 @@ class ListFuncTests {
assertEquals(5, result.size());
assertEquals("a2", ((Map<String, Object>) result.get(1)).get("a").toString());
assertEquals("d4-abc", ((Map<String, Object>) result.get(3)).get("d").toString());
// System.out.println(JSON.toJSONString(result));
String json1 = "[\n"
+ " {\n"
+ " \"itemType\": \"3\",\n"
+ " \"currCd\": \"156\",\n"
+ " \"batchDate\": \"20190331\",\n"
+ " \"itemCd\": \"ORGAP0008\",\n"
+ " \"itemVal\": 0.7189,\n"
+ " \"itemNm\": \"balance rate\",\n"
+ " \"valueStr\": \"0.7189\",\n"
+ " \"orgCd1\": \"230009999\"\n"
+ " },\n"
+ " {\n"
+ " \"itemType\": \"3\",\n"
+ " \"currCd\": \"156\",\n"
+ " \"batchDate\": \"20190331\",\n"
+ " \"itemCd\": \"ORGAP0008\",\n"
+ " \"itemVal\": 0.7040,\n"
+ " \"itemNm\": \"balance rate\",\n"
+ " \"valueStr\": \"0.7040\",\n"
+ " \"orgCd1\": \"231009999\"\n"
+ " }\n"
+ " ]";
String json2 = "[\n"
+ " {\n"
+ " \"orgName\": \"Name1\",\n"
+ " \"orgCd\": \"230009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name2\",\n"
+ " \"orgCd\": \"231009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name3\",\n"
+ " \"orgCd\": \"232009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name3\",\n"
+ " \"orgCd\": \"233009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name4\",\n"
+ " \"orgCd\": \"234009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name5\",\n"
+ " \"orgCd\": \"235009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name6\",\n"
+ " \"orgCd\": \"236009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name7\",\n"
+ " \"orgCd\": \"237009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name8\",\n"
+ " \"orgCd\": \"238009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name9\",\n"
+ " \"orgCd\": \"239009999\"\n"
+ " }\n"
+ " ]";
ONode ctxNode2 = ONode.load(new HashMap());
PathMapping.setByPath(ctxNode2, "test.list1", JSON.parseArray(json1, Map.class), true);
PathMapping.setByPath(ctxNode2, "test.list2", JSON.parseArray(json2, Map.class), true);
String funcExpression2 = "fn.list.join({test.list1},{test.list2},\"orgCd1:orgCd\")";
List<Object> result2 = (List<Object>) FuncExecutor.getInstance().exec(ctxNode2, funcExpression2);
System.out.println(JSON.toJSONString(result2));
assertEquals("Name1", ((Map<String, Object>) result2.get(0)).get("orgName").toString());
}
}