diff --git a/fizz-core/src/main/java/we/fizz/function/ListFunc.java b/fizz-core/src/main/java/we/fizz/function/ListFunc.java index cd7aa07..d1d0f0b 100644 --- a/fizz-core/src/main/java/we/fizz/function/ListFunc.java +++ b/fizz-core/src/main/java/we/fizz/function/ListFunc.java @@ -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> index = new HashMap<>(); for (Map 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 m : src) { - if (m.get(joinField) == null) { + Object srcJoinFieldVal = m.get(joinFields[1]); + if (srcJoinFieldVal == null || !index.containsKey(srcJoinFieldVal.toString())) { continue; } - Map record = index.get(m.get(joinField).toString()); + Map record = index.get(srcJoinFieldVal.toString()); if (fields == null || fields.length == 0) { record.putAll(m); diff --git a/fizz-core/src/test/java/we/fizz/function/ListFuncTests.java b/fizz-core/src/test/java/we/fizz/function/ListFuncTests.java index e8c38f7..1e898c1 100644 --- a/fizz-core/src/test/java/we/fizz/function/ListFuncTests.java +++ b/fizz-core/src/test/java/we/fizz/function/ListFuncTests.java @@ -166,9 +166,82 @@ class ListFuncTests { assertEquals(5, result.size()); assertEquals("a2", ((Map) result.get(1)).get("a").toString()); assertEquals("d4-abc", ((Map) 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 result2 = (List) FuncExecutor.getInstance().exec(ctxNode2, funcExpression2); + System.out.println(JSON.toJSONString(result2)); + assertEquals("Name1", ((Map) result2.get(0)).get("orgName").toString()); } - + } \ No newline at end of file