How to hande nested structures in mustache

836 views
Skip to first unread message

Average Joe

unread,
Nov 14, 2020, 4:05:10 PM11/14/20
to mustache.java
I have a YAML I am trying to create using the mustache template(s) for a state diagram. It looks like this:

states:
{{#states}}
  "{{name}}":
    properties:
    {{#properties}}
      {{key}} : "{{value}}"
      options:
    {{#options}}
      {{value}}: {{key}}
    {{/options}}
    {{/properties}}
    transitions:
    {{#transitions}}
      {{key}} : {{value}}
    {{/transitions}}
{{/states}}

The structure looks somewhat like this and I am unable to create the correct POJO mapping/method to get it to work. The documentation and the 2 samples on github do not deal with this in detail.

I can print the "states" just fine and even the "properties" and "transitions" just fine from the accompanying POJOs, but I am unable to the "options" to work.

Currently I have a class, with a bunch of code, the relevant parts are copied here:

    Set<Map.Entry<String,String>> properties(State state)  {
        Map mapOfProperties = new HashMap();
        switch (state.getType()) {
            case text:
                mapOfProperties.put("text", state.getMessage());
                break;
            case list:
                mapOfProperties.put("prompt", state.getMessage());
                mapOfProperties.put("variable", "someAction");
               // I was hoping to add something here, such that the options sub tree can be added here. Just not sure what to put? 
              // Something similar to the following, that would put options: one line and 
             // the actual key/value pairs on the next few lines, somewhat similar to 
             // options:
             //.           key1: value1
             //.           key2: value2
             //   mapOfProperties.put("options:", options(state)); 
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + state.getType());
        }
        return mapOfProperties.entrySet();
    }
   
 Code for "options" method:
        Set<Map.Entry<String,String>> options(State state) {
        List<String> childStateList = state.getChildren();
        LinkedHashMap<String, String> mapOfOptions = new LinkedHashMap<>();
        if ((childStateList != null) && !childStateList.isEmpty()) {
            for (String childState: childStateList) {
                mapOfOptions.put(childState, "- value: ");
                mapOfOptions.put(getChildStateMessage(childState), "  label:");
            }
        }
        return mapOfOptions.entrySet();
    }

Sam Pullara

unread,
Nov 14, 2020, 9:54:36 PM11/14/20
to mustac...@googlegroups.com
It looks like it should work on the face of it. If you can make a simple repro that i can run to investigate I'd be happy to help out. Maybe step through with a debugger and see what the value are?

--
You received this message because you are subscribed to the Google Groups "mustache.java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mustachejava/013747d5-cacf-4b58-b94a-ba9035d69029n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages