Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-3128

Mustache templates should allow iteration over the values in an object without referring to the keys

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: (None)
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: cf-agent
    • Labels:
      None
    • Found in version (details):
      3.12.1

      Description

      The handling of iteration over objects vs. lists in mustache templates is a little bit inconsistent/unpredictable (or at least cannot be predicted from the documentation).

      Here is a test that doesn't work at all:

      bundle agent main {
      
        files:
          any::
            "/tmp/demo.txt"
              create => "true",
              template_method => "inline_mustache",
              edit_template_string => "
      {{#list}}
      Should have three items: {{{.}}}
      {{/list}}
      {{#list_no_ref_to_value}}
      This line should appear three times
      {{/list_no_ref_to_value}}
      {{#obj}}
      Three words for numbers: {{{@}}} means {{{.}}}
      {{/obj}}
      {{#obj_bad_refs}}
      Should show three words: {{{.}}}
      {{/obj_bad_refs}}
      {{#obj_bad_refs2}}
      Should show three numerals: {{{@}}}
      {{/obj_bad_refs2}}
      {{#obj_bad_refs3}}
      This line should be here three times also
      {{/obj_bad_refs3}}
      ",
              template_data => @(mydata);
      
        vars:
          any::
            "mydata"
              data => '{
                "list_no_ref_to_value": [
                  "1",
                  "2",
                  "3",
                ],
                "list": [
                  "1",
                  "2",
                  "3",
                ],
                "obj": {
                  "1": "one",
                  "2": "two",
                  "3": "three",
                },
                "obj_bad_refs": {
                  "1": "one",
                  "2": "two",
                  "3": "three",
                },
                "obj_bad_refs2": {
                  "1": "one",
                  "2": "two",
                  "3": "three",
                },
                "obj_bad_refs3": {
                  "1": "one",
                  "2": "two",
                  "3": "three",
                },
              }';
      
      }
      

      This produces the error:

         error: Error rendering mustache template '(null)'
      

      Whatever that means.

      Removing these lines allows the template to at least render (although they shouldn't need to be removed):

      -{{#obj_bad_refs}}
      -Should show three words: {{{.}}}
      -{{/obj_bad_refs}}
      

      Then it produces these results:

      Should have three items: 1
      Should have three items: 2
      Should have three items: 3
      This line should appear three times
      This line should appear three times
      This line should appear three times
      Three words for numbers: 1 means one
      Three words for numbers: 2 means two
      Three words for numbers: 3 means three
      Should show three numerals: 1
      Should show three numerals: 2
      Should show three numerals: 3
      This line should be here three times also
      

      Summary:

      1. A data container (object) cannot be iterated over solely by value, whereas a list can be. This is a bug. (To include the values from an object in the rendered file, you MUST include the keys.) This is the test that throws the error above and doesn't render at all.
      2. A data container that is iterated over without any reference to the keys, will produce only ONE line in the rendered output. Contrast this with the handling for a list, where iterating over a list without referring to the values (using the "." variable) will still produce as many lines (blocks) as there are elements in the list. This is a bug.

      I haven't tested the handling for CFEngine slists and classic arrays that are passed by top-level key, but I anticipate that these same bugs will hold true. (Also see CFE-3124 for an additional bug about CFEngine classic arrays.)

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mweilgart Mike Weilgart
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:

                Summary Panel