Skip to content

Commit 4ad9bc4

Browse files
committed
1.支持index模式取MAP节点,用于支持特殊符号节点名,例如req['content-type'],中括号中仅支持字符串格式,例如req[content-type]仍然按数组格式进行解析。
2.提供表达式变量分析工具,ELUtils.getVariants,将表达式传入后分析出该表达式的变量列表 3.修复根节点为数组时解析失败的问题,现在this[xxx]可以正确被识别为数组 4.优化了表达式方法调用选取重载方法的方式 5.修复了多维度数组的赋值和取值问题
1 parent 9101498 commit 4ad9bc4

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/main/java/org/fanjr/simplify/el/ELExecutor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,11 @@ private static NodeInvoker resolveNode(char[] chars, int start, int end) {
743743
throw new ElException("解析错误!错误的节点:" + nodeName);
744744
} else {
745745
int lastArrayIndex = findLastCharToken(chars, '[', start, end - 1, false);
746-
NodeInvoker parent = resolveNode(chars, start, lastArrayIndex);
746+
NodeInvoker parent = null;
747+
if (start != lastArrayIndex) {
748+
// 没有上一级,可能是多维数组
749+
parent = resolveNode(chars, start, lastArrayIndex);
750+
}
747751
ELInvoker indexEl = resolve(chars, lastArrayIndex + 1, end - 1);
748752
if (indexEl instanceof StringInvoker) {
749753
return IndexMapNodeInvoker.newInstance(nodeName, parent, (String) indexEl.invoke(null));

src/main/java/org/fanjr/simplify/el/invoker/node/IndexMapNodeInvoker.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public static IndexMapNodeInvoker newInstance(String nodeName, NodeInvoker super
2727
return new IndexMapNodeInvoker(nodeName, superNode, indexNodeName);
2828
}
2929

30+
@Override
31+
public void setParentNodeInvoker(NodeInvoker parentNodeInvoker) {
32+
if (this.parentNodeInvoker == null) {
33+
this.parentNodeInvoker = parentNodeInvoker;
34+
} else {
35+
this.parentNodeInvoker.setParentNodeInvoker(parentNodeInvoker);
36+
}
37+
}
38+
3039
@Override
3140
Object getValueByParent(Object ctx, NodeHolder parentNode) {
3241
String nodeName = this.indexNodeName;

src/main/java/org/fanjr/simplify/el/invoker/node/IndexNodeInvoker.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import java.lang.reflect.Array;
1010
import java.util.List;
11-
import java.util.stream.Stream;
1211

1312
/**
1413
* @author fanjr@vip.qq.com
@@ -30,7 +29,11 @@ public static IndexNodeInvoker newInstance(String nodeName, NodeInvoker superNod
3029

3130
@Override
3231
public void setParentNodeInvoker(NodeInvoker parentNodeInvoker) {
33-
this.parentNodeInvoker.setParentNodeInvoker(parentNodeInvoker);
32+
if (this.parentNodeInvoker == null) {
33+
this.parentNodeInvoker = parentNodeInvoker;
34+
} else {
35+
this.parentNodeInvoker.setParentNodeInvoker(parentNodeInvoker);
36+
}
3437
}
3538

3639
private int getIndex(Object ctx) {

src/test/java/unit/ElTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ public void baseTest() {
107107
Assertions.assertEquals("t1", ELExecutor.eval("a.b.c.d=='1234567890'?'t1':'t2'", context, String.class));
108108
Assertions.assertEquals("t1", ELExecutor.eval("a.b.c.d=='1234567890'?\"t1\":\"t2\"", context, String.class));
109109
Assertions.assertEquals("true", ELExecutor.eval("a.b.c.boo=true", context, String.class));
110+
Assertions.assertEquals("true", ELExecutor.eval("a.b.c.boo", context, String.class));
111+
Assertions.assertEquals("true", ELExecutor.eval("a.b.c['boo']", context, String.class));
112+
Assertions.assertEquals("true", ELExecutor.eval("a.b.['c']['boo']", context, String.class));
113+
Assertions.assertEquals("true", ELExecutor.eval("a.['b']['c'].boo", context, String.class));
110114
Assertions.assertEquals("true", ELExecutor.eval("a.b.c.arr[3][1]=true", context, String.class));
115+
Assertions.assertEquals("true", ELExecutor.eval("a.b.c.arr[3][1]", context, String.class));
116+
Assertions.assertEquals("true", ELExecutor.eval("a.b.c.['arr'][3][1]", context, String.class));
111117
Assertions.assertEquals("1", ELExecutor.eval("a.index=1", context, String.class));
112118
Assertions.assertEquals("{\"d\":\"", ELExecutor.eval("a.b.c.toString().substring(\"0\",6)", context, String.class));
113119
Assertions.assertEquals("{\"c\":{\"d\":\"1234567890\",\"boo\":true,\"arr\":[null,null,null,[null,true]]}}", ELExecutor.eval("a.b", context, String.class));
@@ -124,6 +130,7 @@ public void baseTest() {
124130
Assertions.assertEquals("6", ELExecutor.eval("a.index = (a.index + 5)", context, String.class));
125131
Assertions.assertEquals("11", ELExecutor.eval("+ a.index += 5", context, String.class));
126132
Assertions.assertEquals("11", ELExecutor.eval("a.index2[a.index] = a.index", context, String.class));
133+
Assertions.assertEquals("true", ELExecutor.eval("a.index2[a.index] == a.index", context, String.class));
127134
Assertions.assertEquals("6", ELExecutor.eval("a.index = (a.index - 5)", context, String.class));
128135
Assertions.assertEquals("1", ELExecutor.eval("a.index -= 5", context, String.class));
129136
Assertions.assertEquals("1", ELExecutor.eval("a.index2[this.a.index] = a.index", context, String.class));

0 commit comments

Comments
 (0)