Skip to content

Commit f793336

Browse files
authored
Merge pull request #41 from Optum/support-lowercase
Add lowercase support.
2 parents 3024afc + bb32580 commit f793336

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.optum.templ</groupId>
88
<artifactId>templ</artifactId>
9-
<version>1.0.X-SNAPSHOT</version>
9+
<version>1.1.X-SNAPSHOT</version>
1010
<packaging>pom</packaging>
1111

1212
<name>templ</name>

templ-lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>com.optum.templ</groupId>
1111
<artifactId>templ</artifactId>
12-
<version>1.0.X-SNAPSHOT</version>
12+
<version>1.1.X-SNAPSHOT</version>
1313
<relativePath>../pom.xml</relativePath>
1414
</parent>
1515

templ-lib/src/main/java/com/optum/templ/TemplEngine.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class TemplEngine {
1010
private static final String EMPTY_STRING = "";
1111
private static final String TO_UPPER_COMMAND_PREFIX = "^";
12+
private static final String TO_LOWER_COMMAND_PREFIX = "~";
1213

1314
private static final String TEMPLATE_CANARY = "{{";
1415
/**
@@ -27,7 +28,7 @@ public class TemplEngine {
2728
* \ needs to be Regex escaped with a backslash. Java has to escape BOTH backslashes. So it becomes \\\\
2829
*/
2930
private static final String DELIMITERS = "[ ,.;:?&@#/()<>_\\-\\\\|]";
30-
private static final Pattern PREFIX_PATTERN = Pattern.compile("^(" + DELIMITERS + "*)(\\^?)");
31+
private static final Pattern PREFIX_PATTERN = Pattern.compile("^(" + DELIMITERS + "*)([\\^~]?)");
3132
private static final Pattern SUFFIX_PATTERN = Pattern.compile("(" + DELIMITERS + "+)$");
3233

3334
final TemplDataSource dataSource;
@@ -73,14 +74,17 @@ public String processTemplate(String rawValue) throws TemplException {
7374
private String resolveTemplateFragment(String template) throws TemplException {
7475
final String prefix;
7576
final boolean uppercase;
77+
final boolean lowercase;
7678
Matcher prefixMatcher = PREFIX_PATTERN.matcher(template);
7779
if (prefixMatcher.find()) {
7880
prefix = prefixMatcher.group(1);
7981
uppercase = TO_UPPER_COMMAND_PREFIX.equals(prefixMatcher.group(2));
80-
template = template.substring(prefix.length()+(uppercase?1:0));
82+
lowercase = TO_LOWER_COMMAND_PREFIX.equals(prefixMatcher.group(2));
83+
template = template.substring(prefix.length()+((uppercase||lowercase)?1:0));
8184
} else {
8285
prefix = EMPTY_STRING;
8386
uppercase = false;
87+
lowercase = false;
8488
}
8589

8690
final String suffix;
@@ -100,7 +104,17 @@ private String resolveTemplateFragment(String template) throws TemplException {
100104
throw new MissingKeyTemplException(template);
101105
}
102106
} else if (!result.isEmpty()) {
103-
return prefix + (uppercase ? result.toUpperCase() : result) + suffix;
107+
StringBuilder sb = new StringBuilder();
108+
sb.append(prefix);
109+
if (uppercase) {
110+
sb.append(result.toUpperCase().replace("-","_"));
111+
} else if (lowercase) {
112+
sb.append(result.toLowerCase().replace("_","-"));
113+
} else {
114+
sb.append(result);
115+
}
116+
sb.append(suffix);
117+
return sb.toString();
104118
} else {
105119
return EMPTY_STRING;
106120
}

templ-lib/src/test/groovy/com/optum/templ/TemplEngineSpec.groovy

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class TemplEngineSpec extends Specification {
192192
def "Template Substitution Supports Uppercase Results" () {
193193
when:
194194
map.clear()
195-
map.put("ENV", "dev")
195+
map.put("ENV", "dev-a_b")
196196
TemplEngine te = new TemplEngine(ds)
197197
String result = te.processTemplate(template)
198198

@@ -201,13 +201,31 @@ class TemplEngineSpec extends Specification {
201201

202202
where:
203203
template | expected
204-
'A{{^ENV}}Z' | 'ADEVZ'
205-
'A{{.^ENV}}Z' | 'A.DEVZ'
206-
'A{{.__^ENV}}Z' | 'A.__DEVZ'
207-
'A{{^ENV.}}Z' | 'ADEV.Z'
208-
'A{{.^ENV.}}Z' | 'A.DEV.Z'
204+
'A{{^ENV}}Z' | 'ADEV_A_BZ'
205+
'A{{.^ENV}}Z' | 'A.DEV_A_BZ'
206+
'A{{.__^ENV}}Z' | 'A.__DEV_A_BZ'
207+
'A{{^ENV.}}Z' | 'ADEV_A_B.Z'
208+
'A{{.^ENV.}}Z' | 'A.DEV_A_B.Z'
209209
}
210210

211+
def "Template Substitution Supports Lowercase Results" () {
212+
when:
213+
map.clear()
214+
map.put("ENV", "DEV-A_B")
215+
TemplEngine te = new TemplEngine(ds)
216+
String result = te.processTemplate(template)
217+
218+
then:
219+
result == expected
220+
221+
where:
222+
template | expected
223+
'A{{~ENV}}Z' | 'Adev-a-bZ'
224+
'A{{.~ENV}}Z' | 'A.dev-a-bZ'
225+
'A{{.__~ENV}}Z' | 'A.__dev-a-bZ'
226+
'A{{~ENV.}}Z' | 'Adev-a-b.Z'
227+
'A{{.~ENV.}}Z' | 'A.dev-a-b.Z'
228+
}
211229

212230
def "Exception - Template Substitution Missing Value" () {
213231
when:

0 commit comments

Comments
 (0)