Skip to content
This repository has been archived by the owner on Sep 23, 2023. It is now read-only.

Commit

Permalink
Added jMustache as templating engine (#62)
Browse files Browse the repository at this point in the history
* Added jMustache as templating engine
  • Loading branch information
dinuta authored Feb 4, 2023
1 parent 3e1c3aa commit b6e3312
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@
<version>8.0.0.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.samskivert/jmustache -->
<dependency>
<groupId>com.samskivert</groupId>
<artifactId>jmustache</artifactId>
<version>1.15</version>
</dependency>

</dependencies>

<dependencyManagement>
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/com/github/estuaryoss/agent/utils/TemplateGluer.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package com.github.estuaryoss.agent.utils;

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;

import java.util.Map;

public class TemplateGluer {
private static final int RUN_DEPTH = 5;

public static String glue(String template, Map<String, String> swapValuesMap) {
if (swapValuesMap == null || template == null) return template;

for (int i = 0; i < RUN_DEPTH; i++) {
for (Map.Entry<String, String> entry : swapValuesMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();

template = template.replace(String.format("{%s}", key), value);
template = template.replace(String.format("{ %s }", key), value);
}
}
Template jMustacheTemplate = Mustache.compiler().compile(template);

return template;
return jMustacheTemplate.execute(swapValuesMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void whenSettingExternalEnvVarsFromFileAndItsASystemOneThenItDoesntGetOve
strings = {
"FOO1;BARx;BARx",
"FOO3;BAR3;BAR3",
"FOO4;{FOO1}/{FOO3};BARx/BAR3"
"FOO4;{{FOO1}}/{{FOO3}};BARx/BAR3"
}
)
@Order(3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void whenSettingEnvVarWhichHasReferenceToAnotherEnvVar_TheSecondOneIsInte
String envVarName1 = "FOO1";
String envVarName1Value = "/FOO1/BAR1";
String envVarName2 = "FOO2";
String envVarName2Value = "{FOO1}/BAR2";
String envVarName2Value = "{{FOO1}}/BAR2";
Map<String, String> envVarsToBeSet = new LinkedHashMap<>();
envVarsToBeSet.put(envVarName1, envVarName1Value);
envVarsToBeSet.put(envVarName2, envVarName2Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class TemplateGluerTest {
@ParameterizedTest
@ValueSource(
strings = {
"{FOO1}/A;FOO1;B;B/A",
"{{FOO1}}/A;FOO1;B;B/A",
"A;A;A;A",
"A/{FOO1};FOO1;B;A/B",
"A/{{FOO1}};FOO1;B;A/B",
}
)
public void whenSwapping_ThenProfit(String templateInVarNameInVarValueExpectedOut) {
Expand Down

0 comments on commit b6e3312

Please sign in to comment.