Skip to content

Commit

Permalink
add support for dynamicnames
Browse files Browse the repository at this point in the history
  • Loading branch information
spullara committed Jun 27, 2024
1 parent 9442141 commit 702606f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public void partial(TemplateContext tc, final String variable, String indent) {
list.add(new PartialCode(partialTC, df, variable));
}

@Override
public void dynamicPartial(TemplateContext tc, final String variable, String indent) {
TemplateContext partialTC = new TemplateContext("{{", "}}", tc.file(), tc.line(), tc.startOfLine());
list.add(new DynamicPartialCode(partialTC, df, variable));
}

@Override
public void value(TemplateContext tc, final String variable, boolean encoded) {
list.add(new ValueCode(tc, df, variable, encoded));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,12 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
String indent = (onlywhitespace && startOfLine) ? out.toString() : "";
out = write(mv, out, file, currentLine.intValue(), startOfLine);
startOfLine = startOfLine & onlywhitespace;
mv.partial(new TemplateContext(sm, em, file, currentLine.get(), startOfLine), variable, indent);

if (variable.trim().startsWith("*")) {
mv.dynamicPartial(new TemplateContext(sm, em, file, currentLine.get(), startOfLine), variable.replaceAll(" ", ""), indent);
} else {
mv.partial(new TemplateContext(sm, em, file, currentLine.get(), startOfLine), variable, indent);
}
// a new line following a partial is dropped
if (specConformWhitespace && startOfLine) {
br.mark(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface MustacheVisitor {

void partial(TemplateContext templateContext, String variable, String indent);

void dynamicPartial(TemplateContext templateContext, String variable, String indent);

void value(TemplateContext templateContext, String variable, boolean encoded);

void write(TemplateContext templateContext, String text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class DefaultCode implements Code, Cloneable {
protected final boolean returnThis;
protected final Binding binding;
protected final DefaultMustacheFactory df;
protected final boolean dynamic;

@SuppressWarnings({"CloneDoesntCallSuperClone", "CloneDoesntDeclareCloneNotSupportedException"})
public Object clone() {
Expand Down Expand Up @@ -79,7 +80,13 @@ public DefaultCode(TemplateContext tc, DefaultMustacheFactory df, Mustache musta
this.type = type;
this.name = name;
this.tc = tc;
this.binding = oh == null ? null : oh.createBinding(name, tc, this);
if (name != null && name.startsWith("*")) {
this.binding = oh == null ? null : oh.createBinding(name.substring(1), tc, this);
this.dynamic = true;
} else {
this.binding = oh == null ? null : oh.createBinding(name, tc, this);
this.dynamic = false;
}
this.returnThis = ".".equals(name);
}

Expand Down Expand Up @@ -142,6 +149,12 @@ public Object get(List<Object> scopes) {
return length == 0 ? null : scopes.get(length - 1);
}
try {
if (dynamic) {
// We need to create a new binding each time, as the name is dynamic
String dynamicName = (String) binding.get(scopes);
// TODO: cache these bindings
return oh.createBinding(dynamicName, tc, this).get(scopes);
}
return binding.get(scopes);
} catch (MustacheException e) {
e.setContext(tc);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.mustachejava.codes;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.TemplateContext;

import java.io.Writer;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class DynamicPartialCode extends DefaultCode {
public DynamicPartialCode(TemplateContext tc, DefaultMustacheFactory df, String name) {
super(tc, df, null, name, ">*");
}

private final ConcurrentMap<String, PartialCode> partialCodeMap = new ConcurrentHashMap<>();

@Override
public Writer execute(Writer writer, List<Object> scopes) {
String partialName = (String) binding.get(scopes);
if (partialName == null) {
return appendText(writer);
}
PartialCode partialCode = partialCodeMap.computeIfAbsent(partialName, name -> {
PartialCode pc = new PartialCode(tc, df, partialName);
pc.append(appended);
pc.init();
return pc;
});
return partialCode.execute(writer, scopes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public void lambdas() {
public void inheritance() throws IOException {
}

@Override
@Test
@Ignore("not ready yet")
public void dynamicnames() {
}

@Override
protected DefaultMustacheFactory createMustacheFactory(final JsonNode test) {
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/test/java/com/github/mustachejava/SpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public void inheritance() throws IOException {
run(getSpec("~inheritance.yml"));
}

@Test
public void dynamicnames() throws IOException {
run(getSpec("~dynamic-names.yml"));
}

private void run(JsonNode spec) {
int fail = 0;
int success = 0;
Expand Down

0 comments on commit 702606f

Please sign in to comment.