diff --git a/fizz-core/src/main/java/com/fizzgate/fizz/function/CommonFunc.java b/fizz-core/src/main/java/com/fizzgate/fizz/function/CommonFunc.java index 9b59ffc..4ce0827 100644 --- a/fizz-core/src/main/java/com/fizzgate/fizz/function/CommonFunc.java +++ b/fizz-core/src/main/java/com/fizzgate/fizz/function/CommonFunc.java @@ -18,7 +18,10 @@ package com.fizzgate.fizz.function; import java.lang.reflect.Array; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; @@ -65,6 +68,8 @@ public class CommonFunc implements IFunc { FuncExecutor.register(NAME_SPACE_PREFIX + "common.and", this); FuncExecutor.register(NAME_SPACE_PREFIX + "common.or", this); FuncExecutor.register(NAME_SPACE_PREFIX + "common.not", this); + FuncExecutor.register(NAME_SPACE_PREFIX + "common.emptyMap", this); + FuncExecutor.register(NAME_SPACE_PREFIX + "common.emptyList", this); } /** @@ -174,4 +179,22 @@ public class CommonFunc implements IFunc { return !(obj == null ? false : obj); } + /** + * Return an empty map + * + * @return + */ + public Map emptyMap() { + return new HashMap<>(); + } + + /** + * Return an empty list + * + * @return + */ + public List> emptyList() { + return new ArrayList<>(); + } + } diff --git a/fizz-core/src/main/java/com/fizzgate/fizz/function/FuncExecutor.java b/fizz-core/src/main/java/com/fizzgate/fizz/function/FuncExecutor.java index 4292196..e2e5fab 100644 --- a/fizz-core/src/main/java/com/fizzgate/fizz/function/FuncExecutor.java +++ b/fizz-core/src/main/java/com/fizzgate/fizz/function/FuncExecutor.java @@ -312,6 +312,26 @@ public class FuncExecutor { argsStrContainer = this.trimArgStr(argsStrContainer, 5, isVarArgs, paramTypes.length, funcExpression); argsStr = argsStrContainer.getArgsStr(); i = argsStrContainer.getIndex(); + } else if (argsStr.matches("^\\[\\]\\s*,.*") || argsStr.matches("^\\[\\]\\s*\\).*")) { // [] + if (isVarArgs && i == paramTypes.length - 1) { + varArgs.add(new ArrayList()); + args[i] = varArgs.toArray(new ArrayList[varArgs.size()]); + } else { + args[i] = new ArrayList(); + } + argsStrContainer = this.trimArgStr(argsStrContainer, 2, isVarArgs, paramTypes.length, funcExpression); + argsStr = argsStrContainer.getArgsStr(); + i = argsStrContainer.getIndex(); + } else if (argsStr.matches("^\\{\\}\\s*,.*") || argsStr.matches("^\\{\\}\\s*\\).*")) { // {} + if (isVarArgs && i == paramTypes.length - 1) { + varArgs.add(new HashMap()); + args[i] = varArgs.toArray(new HashMap[varArgs.size()]); + } else { + args[i] = new HashMap(); + } + argsStrContainer = this.trimArgStr(argsStrContainer, 2, isVarArgs, paramTypes.length, funcExpression); + argsStr = argsStrContainer.getArgsStr(); + i = argsStrContainer.getIndex(); } else if (argsStr.startsWith("{")) { // reference value int pos = argsStr.indexOf("}", 1); if (pos != -1) { diff --git a/fizz-core/src/main/java/com/fizzgate/fizz/function/ListFunc.java b/fizz-core/src/main/java/com/fizzgate/fizz/function/ListFunc.java index 7c41a49..2ceeb67 100644 --- a/fizz-core/src/main/java/com/fizzgate/fizz/function/ListFunc.java +++ b/fizz-core/src/main/java/com/fizzgate/fizz/function/ListFunc.java @@ -62,6 +62,7 @@ public class ListFunc implements IFunc { FuncExecutor.register(NAME_SPACE_PREFIX + "list.join", this); FuncExecutor.register(NAME_SPACE_PREFIX + "list.rename", this); FuncExecutor.register(NAME_SPACE_PREFIX + "list.removeFields", this); + FuncExecutor.register(NAME_SPACE_PREFIX + "list.emptyList", this); } /** @@ -227,5 +228,14 @@ public class ListFunc implements IFunc { } return data; } + + /** + * Return an empty list + * + * @return + */ + public List> emptyList() { + return new ArrayList<>(); + } } diff --git a/fizz-core/src/test/java/com/fizzgate/fizz/function/CommonFuncTests.java b/fizz-core/src/test/java/com/fizzgate/fizz/function/CommonFuncTests.java index a195e6a..339fc35 100644 --- a/fizz-core/src/test/java/com/fizzgate/fizz/function/CommonFuncTests.java +++ b/fizz-core/src/test/java/com/fizzgate/fizz/function/CommonFuncTests.java @@ -16,6 +16,7 @@ */ package com.fizzgate.fizz.function; +import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.ArrayList; @@ -116,6 +117,46 @@ class CommonFuncTests { assertEquals(true, result); } + @Test + void testIif6() { + // test [] + String funcExpression = "fn.common.iif(false, \"abc\", [])"; + List result = (List)FuncExecutor.getInstance().exec(null, funcExpression); + assertEquals(true, result.size() == 0); + } + + @Test + void testIif7() { + // test {} + String funcExpression = "fn.common.iif(false, \"abc\", {})"; + Map result = (Map)FuncExecutor.getInstance().exec(null, funcExpression); + assertEquals(true, result.size() == 0); + } + + @Test + void testIif8() { + // test [] + String funcExpression = "fn.common.iif(false, \"abc\", fn.common.emptyList())"; + List result = (List)FuncExecutor.getInstance().exec(null, funcExpression); + assertEquals(true, result.size() == 0); + } + + @Test + void testIif9() { + // test {} + String funcExpression = "fn.common.iif(false, \"abc\", fn.common.emptyMap())"; + Map result = (Map)FuncExecutor.getInstance().exec(null, funcExpression); + assertEquals(true, result.size() == 0); + } + + @Test + void testIif10() { + // test null + String funcExpression = "fn.common.iif(false, \"abc\", null)"; + Map result = (Map)FuncExecutor.getInstance().exec(null, funcExpression); + assertNull(result); + } + @Test void testEquals() { String funcExpression = "fn.common.equals(\"abc\", true)"; diff --git a/fizz-core/src/test/java/com/fizzgate/fizz/function/ListFuncTests.java b/fizz-core/src/test/java/com/fizzgate/fizz/function/ListFuncTests.java index 81957fa..c2be563 100644 --- a/fizz-core/src/test/java/com/fizzgate/fizz/function/ListFuncTests.java +++ b/fizz-core/src/test/java/com/fizzgate/fizz/function/ListFuncTests.java @@ -106,6 +106,29 @@ class ListFuncTests { assertEquals("a2", ((Map) result.get(1)).get("a").toString()); assertEquals("a4", ((Map) result.get(3)).get("a").toString()); } + + @Test + void testMerge2() { + List subList1 = new ArrayList<>(); + subList1.add(createRecord("a", "a1")); + subList1.add(createRecord("a", "a2")); + subList1.add(createRecord("a", "a3")); + + List subList2 = new ArrayList<>(); + subList2.add(createRecord("a", "a4")); + subList2.add(createRecord("a", "a5")); + subList2.add(createRecord("a", "a6")); + + ONode ctxNode = ONode.load(new HashMap()); + PathMapping.setByPath(ctxNode, "test.data1", subList1, true); + PathMapping.setByPath(ctxNode, "test.data2", subList2, true); + + String funcExpression = "fn.list.merge({test.data1}, [] , {test.data2})"; + List result = (List) FuncExecutor.getInstance().exec(ctxNode, funcExpression); + assertEquals(6, result.size()); + assertEquals("a2", ((Map) result.get(1)).get("a").toString()); + assertEquals("a4", ((Map) result.get(3)).get("a").toString()); + } @Test void testExtract() {