Prechádzať zdrojové kódy

support extracting placeholder from non-normalized expressions, e.g. ${user.address}/user/gateway

Jason Song 4 rokov pred
rodič
commit
0306b71b46

+ 3 - 3
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/PlaceholderHelper.java

@@ -120,12 +120,12 @@ public class PlaceholderHelper {
   }
 
   private boolean isNormalizedPlaceholder(String propertyString) {
-    return propertyString.startsWith(PLACEHOLDER_PREFIX) && propertyString.endsWith(PLACEHOLDER_SUFFIX);
+    return propertyString.startsWith(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX);
   }
 
   private boolean isExpressionWithPlaceholder(String propertyString) {
-    return propertyString.startsWith(EXPRESSION_PREFIX) && propertyString.endsWith(EXPRESSION_SUFFIX)
-        && propertyString.contains(PLACEHOLDER_PREFIX);
+    return propertyString.startsWith(EXPRESSION_PREFIX) && propertyString.contains(EXPRESSION_SUFFIX)
+        && propertyString.contains(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX);
   }
 
   private String normalizeToPlaceholder(String strVal) {

+ 21 - 0
apollo-client/src/test/java/com/ctrip/framework/apollo/spring/property/PlaceholderHelperTest.java

@@ -22,6 +22,11 @@ public class PlaceholderHelperTest {
     check("${some.key:100}", "some.key");
     check("${some.key:${some.other.key}}", "some.key", "some.other.key");
     check("${some.key:${some.other.key:100}}", "some.key", "some.other.key");
+
+    check("${some.key}/xx", "some.key");
+    check("${some.key:100}/xx", "some.key");
+    check("${some.key:${some.other.key}/xx}/yy", "some.key", "some.other.key");
+    check("${some.key:${some.other.key:100}/xx}/yy", "some.key", "some.other.key");
   }
 
   @Test
@@ -30,12 +35,21 @@ public class PlaceholderHelperTest {
     check("${${some.key:other.key}}", "some.key");
     check("${${some.key}:100}", "some.key");
     check("${${some.key}:${another.key}}", "some.key", "another.key");
+
+    check("${${some.key}/xx}/xx", "some.key");
+    check("${${some.key:other.key/xx}/xx}/xx", "some.key");
+    check("${${some.key}/xx:100}/xx", "some.key");
+    check("${${some.key}/xx:${another.key}/xx}xx", "some.key", "another.key");
+
   }
 
   @Test
   public void testExtractComplexNestedPlaceholderKeys() throws Exception {
     check("${${a}1${b}:3.${c:${d:100}}}", "a", "b", "c", "d");
     check("${1${a}2${b}3:4.${c:5${d:100}6}7}", "a", "b", "c", "d");
+
+    check("${${a}1${b}:3.${c:${d:100}}}/xx", "a", "b", "c", "d");
+    check("${1${a}2${b}3:4.${c:5${d:100}6}7}/xx", "a", "b", "c", "d");
   }
 
   @Test
@@ -46,6 +60,13 @@ public class PlaceholderHelperTest {
     check("#{new java.text.SimpleDateFormat('${some.key:${some.other.key:abc}}').parse('${another.key}')}", "some.key", "another.key", "some.other.key");
     check("#{new java.text.SimpleDateFormat('${${some.key}}').parse('${${another.key:other.key}}')}", "some.key", "another.key");
 
+    check("#{new java.text.SimpleDateFormat('${some.key}/xx').parse('${another.key}/xx')}", "some.key", "another.key");
+    check("#{new java.text.SimpleDateFormat('${some.key:abc}/xx').parse('${another.key:100}/xx')}", "some.key", "another.key");
+    check("#{new java.text.SimpleDateFormat('${some.key:${some.other.key}/xx}/xx').parse('${another.key}/xx')}", "some.key", "another.key", "some.other.key");
+    check("#{new java.text.SimpleDateFormat('${some.key:${some.other.key:abc}/xx}/xx').parse('${another.key}/xx')}", "some.key", "another.key", "some.other.key");
+    check("#{new java.text.SimpleDateFormat('${${some.key}/xx}').parse('${${another.key:other.key}/xx}/xx')}", "some.key", "another.key");
+
+
     assertTrue(placeholderHelper.extractPlaceholderKeys("#{systemProperties[some.key] ?: 123}").isEmpty());
     assertTrue(placeholderHelper.extractPlaceholderKeys("#{ T(java.lang.Math).random() * 100.0 }").isEmpty());
   }