Browse Source

优化表达式解析

zhou-hao 3 years ago
parent
commit
4e8a0f99ec

+ 4 - 1
hsweb-core/src/main/java/org/hswebframework/web/utils/TemplateParser.java

@@ -103,11 +103,14 @@ public class TemplateParser {
         while (next()) {
             if (isPrepare()) {
                 inPrepare = true;
-            } else if (inPrepare&&isPrepareEnd()) {
+            } else if (inPrepare && isPrepareEnd()) {
                 inPrepare = false;
                 setParsed(propertyMapping.apply(new String(expression, 0, expressionPos)).toCharArray());
                 expressionPos = 0;
             } else if (inPrepare) {
+                if (expression.length <= expressionPos) {
+                    expression = Arrays.copyOf(expression, (int)(expression.length * 1.5));
+                }
                 expression[expressionPos++] = symbol;
             } else if (!isPreparing()) {
                 setParsed(symbol);

+ 4 - 2
hsweb-core/src/test/java/org/hswebframework/web/utils/ExpressionUtilsTest.java

@@ -53,7 +53,7 @@ public class ExpressionUtilsTest {
                 "     \"msgtype\": \"markdown\",\n" +
                 "     \"markdown\": {\n" +
                 "         \"title\":\"消息类型:${messageType}\",\n" +
-                "         \"text\": \" - 设备ID: `${deviceId}` \\n - 设备型号: `${headers.productId}`\\n - 设备名称: `${headers.deviceName}`\"" +
+                "         \"text\": \" - 设备ID: `${deviceId}` 值:`${properties.a-r-str}` \\n - 设备型号: `${headers.productId}`\\n - 设备名称: `${headers.deviceName}`\"" +
                 "     \n},\n" +
                 "      \"at\": {\n" +
                 "          \"isAtAll\": false\n" +
@@ -63,7 +63,9 @@ public class ExpressionUtilsTest {
                 "  \"headers\": {\n" +
                 "    \"productId\": \"VIS-Mandrake\",\n" +
                 "    \"deviceName\": \"能见度仪-曼德克-01\"\n" +
-                "  },\n" +
+                "  }, \"properties\": {\n" +
+                "        \"a-r-str\": \"a2\"\n" +
+                "    },\n" +
                 "  \"messageType\": \"OFFLINE\",\n" +
                 "  \"timestamp\": 1592098397277\n" +
                 "}"), "spel");

+ 21 - 0
hsweb-core/src/test/java/org/hswebframework/web/utils/TemplateParserTest.java

@@ -4,6 +4,7 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import java.util.Collections;
+import java.util.function.Function;
 
 import static org.junit.Assert.*;
 
@@ -18,6 +19,17 @@ public class TemplateParserTest {
         Assert.assertEquals(result, "test-test-test");
     }
 
+    @Test
+    public void testLargeExpr() {
+        String expr = "";
+        for (int i = 0; i < 1000; i++) {
+            expr += "expr_" + i;
+        }
+        String result = TemplateParser.parse("${"+expr+"}", Function.identity());
+
+        assertEquals(expr,result);
+
+    }
     @Test
     public void testLarge() {
         String str = "";
@@ -30,4 +42,13 @@ public class TemplateParserTest {
     }
 
 
+    @Test
+    public void testNest() {
+
+
+        String result = TemplateParser.parse("test-${properties.a-r-str}", Collections.singletonMap("properties", Collections.singletonMap("a-r-str","123")));
+
+        Assert.assertEquals(result, "test-123");
+    }
+
 }