diff --git a/fizz-core/src/main/java/we/fizz/function/FuncExecutor.java b/fizz-core/src/main/java/we/fizz/function/FuncExecutor.java index 037544e..0fdbcba 100644 --- a/fizz-core/src/main/java/we/fizz/function/FuncExecutor.java +++ b/fizz-core/src/main/java/we/fizz/function/FuncExecutor.java @@ -212,19 +212,43 @@ public class FuncExecutor { // int pos2 = funcExpression.lastIndexOf(")"); String argsStr = funcExpression.substring(pos1 + 1); argsStr = StringUtils.trim(argsStr); + + Object[] args = new Object[paramTypes.length]; + if (paramTypes.length == 0) { + // no argument method + if (hasCloseParenthesis(argsStr, 0)) { + ctx.funcExpression = argsStr.substring(1); + } else { + ctx.funcExpression = argsStr; + } + return args; + } // check if there is any argument if (StringUtils.isBlank(argsStr)) { if (paramTypes == null || paramTypes.length == 0) { - return null; + ctx.funcExpression = argsStr; + return args; } else if (paramTypes.length == 1 && isVarArgs) { // check if variable arguments - return null; + ctx.funcExpression = argsStr; + return args; + } else { + throw new FizzRuntimeException( + String.format("missing argument, Function Expression: %s", funcExpression)); + } + } else if (hasCloseParenthesis(argsStr, 0)) { + if (paramTypes == null || paramTypes.length == 0) { + ctx.funcExpression = argsStr.substring(1); + return args; + } else if (paramTypes.length == 1 && isVarArgs) { + ctx.funcExpression = argsStr.substring(1); + return args; } else { throw new FizzRuntimeException( String.format("missing argument, Function Expression: %s", funcExpression)); } } - Object[] args = new Object[paramTypes.length]; + List varArgs = new ArrayList<>(); for (int i = 0; i < paramTypes.length; i++) { Class clazz = paramTypes[i]; @@ -439,9 +463,9 @@ public class FuncExecutor { if (!Character.isWhitespace(argsStr.charAt(i))) { if (")".equals(String.valueOf(argsStr.charAt(i)))) { return true; - } /*else { + } else { return false; - }*/ + } } } return false; diff --git a/fizz-core/src/test/java/we/fizz/function/FuncExecutorTests.java b/fizz-core/src/test/java/we/fizz/function/FuncExecutorTests.java new file mode 100644 index 0000000..d9ab62b --- /dev/null +++ b/fizz-core/src/test/java/we/fizz/function/FuncExecutorTests.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2021 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package we.fizz.function; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * + * @author Francis Dong + * + */ +class FuncExecutorTests { + + @Test + void contextLoads() { + } + + + @Test + void testNest() { + String funcExpression = "fn.codec.md5(fn.date.add( fn.date.add(\"2021-07-09 22:44:55\", \"yyyy-MM-dd HH:mm:ss\", 1, fn.math.addExact(999,1 ) ), \"yyyy-MM-dd HH:mm:ss\", fn.math.addExact(0,1), 1000 ))"; + Object result = FuncExecutor.getInstance().exec(null, funcExpression); + assertEquals(CodecFunc.getInstance().md5("2021-07-09 22:44:57"), result); + } + + @Test + void testNest2() { + String funcExpression = "fn.string.toUpperCase(fn.codec.sha256(fn.string.concat(\"a\",\"b\",fn.string.toString(fn.date.timestamp()))))"; + Object result = FuncExecutor.getInstance().exec(null, funcExpression); + System.out.println(result); + } + + @Test + void testNest3() { + String funcExpression = "fn.string.toUpperCase(fn.string.concat(\"a\",\"b\", fn.string.concat())))"; + Object result = FuncExecutor.getInstance().exec(null, funcExpression); + assertEquals("AB", result); + } + + @Test + void testNest4() { + String funcExpression = "fn.string.toUpperCase(fn.codec.sha256(fn.string.concat(\"a\",fn.string.concat(),\"b\")))"; + Object result = FuncExecutor.getInstance().exec(null, funcExpression); + System.out.println(result); + } + + @Test + void testNest5() { + String funcExpression = "fn.string.toUpperCase(fn.codec.sha256(fn.string.concat(\"a\",fn.string.toString(fn.date.timestamp()),fn.string.concat(fn.string.concat(fn.string.concat(fn.string.concat())), \"c\"),\"b\")))"; + Object result = FuncExecutor.getInstance().exec(null, funcExpression); + System.out.println(result); + } + + +} \ No newline at end of file diff --git a/fizz-core/src/test/java/we/fizz/function/StringFuncTests.java b/fizz-core/src/test/java/we/fizz/function/StringFuncTests.java index a5a0193..83a97bc 100644 --- a/fizz-core/src/test/java/we/fizz/function/StringFuncTests.java +++ b/fizz-core/src/test/java/we/fizz/function/StringFuncTests.java @@ -97,7 +97,7 @@ class StringFuncTests { @Test void testConcatws() { - String funcExpression = "fn.string.concatws(\",\" , \"2021-07-09 22:44:55\", \"yyyy-MM-dd HH:mm:ss\")"; + String funcExpression = "fn.string.concatws(\",\" , \"2021-07-09 22:44:55\", \"yyyy-MM-dd HH:mm:ss\" )"; Object result = FuncExecutor.getInstance().exec(null, funcExpression); assertEquals("2021-07-09 22:44:55,yyyy-MM-dd HH:mm:ss", result.toString()); } @@ -124,6 +124,7 @@ class StringFuncTests { String funcExpression = "fn.string.substring({data.dateStr}, {data.startIndex})"; // String funcExpression = "fn.string.substring(\"2021-07-09 22:44:55\", 1)"; Object result = FuncExecutor.getInstance().exec(ctxNode, funcExpression); + System.out.println(result); assertEquals("2021-07-09 22:44:55".substring(1), result.toString()); } @@ -183,6 +184,14 @@ class StringFuncTests { assertEquals("234", result); } + + @Test + void testToString4() { + String funcExpression = "fn.string.toString(fn.date.timestamp())"; + String result = (String)FuncExecutor.getInstance().exec(null, funcExpression); + System.out.println(result); + } + @Test void testReplace() { String funcExpression = "fn.string.replace(\"2021-07-09 22:44:55\", \"44:55\", \"00:00\")"; @@ -197,6 +206,13 @@ class StringFuncTests { assertEquals("2021-07-09 22:44:55 44:55".replaceAll("44:55", "00:00"), result); } + @Test + void testReplaceAll2() { + String funcExpression = "fn.string.replaceAll(\"1.2.3\", \"\\.\", \"\")"; + String result = (String)FuncExecutor.getInstance().exec(null, funcExpression); + assertEquals("1.2.3".replaceAll("\\.", ""), result); + } + @Test void testReplaceFirst() { String funcExpression = "fn.string.replaceFirst(\"2021-07-09 22:44:55 44:55\", \"44:55\", \"00:00\")";