Skip to content

Commit

Permalink
support extracting placeholder from non-normalized expressions, e.g. …
Browse files Browse the repository at this point in the history
…${user.address}/user/gateway
  • Loading branch information
nobodyiam committed Jul 12, 2020
1 parent 5e0ab3c commit 0306b71
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ public Set<String> extractPlaceholderKeys(String propertyString) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public void testExtractPlaceholderKeys() throws Exception {
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
Expand All @@ -30,12 +35,21 @@ public void testExtractNestedPlaceholderKeys() throws Exception {
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
Expand All @@ -46,6 +60,13 @@ public void testExtractPlaceholderKeysFromExpression() throws Exception {
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());
}
Expand Down

0 comments on commit 0306b71

Please sign in to comment.