Add emptyList/emptyMap functions #477
This commit is contained in:
@@ -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<String, Object> emptyMap() {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an empty list
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Map<String, Object>> emptyList() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,4 +229,13 @@ public class ListFunc implements IFunc {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an empty list
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Map<String, Object>> emptyList() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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)";
|
||||
|
||||
@@ -107,6 +107,29 @@ class ListFuncTests {
|
||||
assertEquals("a4", ((Map<String, Object>) result.get(3)).get("a").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMerge2() {
|
||||
List<Object> subList1 = new ArrayList<>();
|
||||
subList1.add(createRecord("a", "a1"));
|
||||
subList1.add(createRecord("a", "a2"));
|
||||
subList1.add(createRecord("a", "a3"));
|
||||
|
||||
List<Object> 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<Object> result = (List<Object>) FuncExecutor.getInstance().exec(ctxNode, funcExpression);
|
||||
assertEquals(6, result.size());
|
||||
assertEquals("a2", ((Map<String, Object>) result.get(1)).get("a").toString());
|
||||
assertEquals("a4", ((Map<String, Object>) result.get(3)).get("a").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExtract() {
|
||||
List<Object> subList1 = new ArrayList<>();
|
||||
|
||||
Reference in New Issue
Block a user