Open Aethon opened 5 years ago
For anyone that finds this, the workaround I am using is to just store template names in the map and use double indirection to invoke them:
... $(map.(\"simple\"))()$ ...
and
map ::= [
"simple": "simple_template"
]
simple_template() ::= "map template = $dump(view)$"
It is slightly more verbose, but not so terrible as workarounds go :)
hi. is this Kotlin? Might matter. E.g., what if an object gets wrapped and no longer a java object? Maybe the registration no longer works?
It is Kotlin. The registrations work, just not after "passing through" a mapped template in a loaded group file.
I will provide a Java repro after the weekend.
Thanks.
ok, yeah, Maybe there's something about parameter handoff or dynamic scoping that is screwing something up here.
Here is the Java 11 repro (using the same template files above):
package test;
import org.stringtemplate.v4.Interpreter;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroupFile;
import org.stringtemplate.v4.misc.ObjectModelAdaptor;
import java.nio.charset.StandardCharsets;
public class ST4BugReplJava {
static class Adaptor extends ObjectModelAdaptor {
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) {
if (propertyName.equals("special_name"))
return "HUZZAH!";
return super.getProperty(interp, self, o, property, propertyName);
}
}
static class View {
public String regularName;
public View(String regularName) {
this.regularName = regularName;
}
}
static public void main(String[] args) {
var group = new STGroupFile(View.class.getResource("/strepl/main.stg"), StandardCharsets.UTF_8.name(), '$', '$');
group.registerModelAdaptor(Object.class, new Adaptor());
var st = group.getInstanceOf("main");
st.add("view", new View("HIP! HIP!"));
System.out.println(st.render());
}
}
Given ST4 4.1 on the JVM and the following code, I would expect that the
View
would be dumped out identically both times. However, the following is produced:It seems that the templates in the map create a new group based on the file they appear in but this new group does not inherit the model adaptor registrations from the original group. Much confusion ensues.
/strepl/main
:/strepl/other
: