Skip to content

Commit

Permalink
Add $$identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
jfgiraud committed Sep 16, 2022
1 parent 5049384 commit fc35e9f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/temmental.iml
/target/
/.idea/
22 changes: 20 additions & 2 deletions src/main/java/com/github/jfgiraud/temmental/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,37 @@ Object getInModel(Map<String, Object> map, String prefix) throws TemplateExcepti
varName = varName.substring(0, varName.length() - 1);
}
if (map.containsKey(varName) && map.get(varName) != null) {
return map.get(varName);
return isIndirection(prefix) ? getIndirectionValue(map, map.get(varName), varName, true) : map.get(varName);
} else {
throw new TemplateIgnoreRenderingException("Ignore rendering because key '%s' is not present or has null value in the model map at position '%s'.", varName, cursor.getPosition());
}
} else {
if (!map.containsKey(varName) || map.get(varName) == null) {
throw new TemplateException("Key '%s' is not present or has null value in the model map at position '%s'.", varName, cursor.getPosition());
} else {
return map.get(varName);
return isIndirection(prefix) ? getIndirectionValue(map, map.get(varName), varName, false) : map.get(varName);
}
}
}

private Object getIndirectionValue(Map<String, Object> map, Object o, String varName, boolean optional) {
if (! (o instanceof String)) {
throw new TemplateException("Value for '$%s' is not a String", varName, cursor);
}
if (!map.containsKey(o) || map.get(o) == null) {
if (!optional) {
throw new TemplateException("Key '%s' is not present or has null value in the model map at position '%s'.", o, cursor.getPosition());
} else {
throw new TemplateIgnoreRenderingException("Ignore rendering because key '%s' is not present or has null value in the model map at position '%s'.", o, cursor.getPosition());
}
}
return map.get(o);
}

private boolean isIndirection(String prefix) {
return "$$".equals(prefix);
}

boolean isRequired() {
String identifier = getIdentifier();
return identifier != null && (identifier.startsWith("'") || !identifier.endsWith("?"));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/jfgiraud/temmental/Identifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public boolean equals(Object o) {
Object writeObject(Map<String, Object> functions, Map<String, Object> model, TemplateMessages messages) throws TemplateException {
if (identifier.startsWith("'")) {
return identifier.substring(1);
} /*else if (identifier.startsWith("$$")) {
} else if (identifier.startsWith("$$")) {
return getInModel(model, "$$");
} */ else if (identifier.startsWith("$")) {
} else if (identifier.startsWith("$")) {
return getInModel(model, "$");
} else if (identifier.startsWith("@$")) {
return getInModel(model, "@$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class TestTemplateTest {
@Before
public void setUp() throws Exception {
transforms = new HashMap<String, Object>();
transforms.put("concat", String.class.getDeclaredMethod("concat", String.class));
properties = new Properties();
properties.put("hello", "Bonjour");
TemplateRecorder.setRecording(true);
Expand Down Expand Up @@ -159,6 +160,13 @@ public void testBackSlashInTextContext() throws IOException, TemplateException {

@Test
public void testCommandForIndirection() throws IOException, TemplateException {
StringTemplate template = new StringTemplate("~$it#for<'branch>~#~$branch~#~\"elem_\":'concat<$branch>#set<'l>~~$l~#~$$l~~#set~\n~#for~", transforms, properties, Locale.ENGLISH);
model = createModel("it", Arrays.asList("b", "b1", "b2"), "elem_b", "VALUE_B", "elem_b1", "VALUE_B1", "elem_b2", "VALUE_B2");
assertEquals("#b#elem_b#VALUE_B\n#b1#elem_b1#VALUE_B1\n#b2#elem_b2#VALUE_B2\n", template.format(model));
}

@Test
public void testCommandForVariable() throws IOException, TemplateException {
StringTemplate template = new StringTemplate("~$elem~~$l#for<$elem>~<~$before~>~#for~~$elem~", transforms, properties, Locale.ENGLISH);
List<Integer> elements = Arrays.asList(1, 2, 3);
model = createModel("l", elements, "elem", "before");
Expand Down

0 comments on commit fc35e9f

Please sign in to comment.