Quellcode durchsuchen

修复字典错误

zhouhao vor 8 Jahren
Ursprung
Commit
d08ea52eca

+ 18 - 0
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/SimpleDictionaryParser.java

@@ -45,6 +45,24 @@ public class SimpleDictionaryParser<V> implements DictionaryParser<V> {
 
     private Map<String, String> toValueExpressions = new HashMap<>();
 
+    public SimpleDictionaryParser<V> addToTextExpression(String id, String expression) {
+        toTextExpressions.put(id, expression);
+        return this;
+    }
+
+    public SimpleDictionaryParser<V> addToValueExpression(String id, String expression) {
+        toValueExpressions.put(id, expression);
+        return this;
+    }
+
+    public void setToTextExpressions(Map<String, String> toTextExpressions) {
+        this.toTextExpressions = toTextExpressions;
+    }
+
+    public void setToValueExpressions(Map<String, String> toValueExpressions) {
+        this.toValueExpressions = toValueExpressions;
+    }
+
     public SingleDictParser getToTextParser() {
         return toTextParser;
     }

+ 5 - 5
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/test/java/org/hswebframework/web/starter/dictionary/DictionaryParserTests.java

@@ -49,10 +49,7 @@ public class DictionaryParserTests {
                 "{'value':'10102','text':'红富士'}" +
                 ",{'value':'10103','text':'青苹果'}" +
                 //使用表达式进行解析
-                ",{'value':'10105','text':'其他苹果'" +
-                ",'textExpression':'${#value}[${#context[otherApple]}]'" +
-                ",'valueExpression':'${(#context.put(\\'otherApple\\',#pattern.split(\"[ \\\\[ \\\\]]\")[1])==null)?#value:#value}'" +
-                "}" +
+                ",{'id':'10105','value':'10105','text':'其他苹果'}" +
                 "]}" +
                 ",{'value':'102','text':'梨子'}]" +
                 "}" +
@@ -61,7 +58,10 @@ public class DictionaryParserTests {
 
         List<SimpleDictionaryItemEntity> itemEntities = JSON.parseArray(json, SimpleDictionaryItemEntity.class);
         dictionaryEntity.setItems(itemEntities);
-        this.parser = SimpleDictionaryParser.setDict(dictionaryEntity);
+        this.parser = new SimpleDictionaryParser<String>()
+                .addToTextExpression("10105", "${#value}[${#context[otherApple]}]")
+                .addToValueExpression("10105", "${(#context.put('otherApple',#pattern.split(\"[ \\[ \\]]\")[1])==null)?#value:#value}")
+                .setDict(dictionaryEntity);
     }
 
     //支持表达式