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

As a policy writer I would lke to be able to avoid unnecessary intermediate variable definitions when iterating

    XMLWordPrintable

    Details

    • Type: Story
    • Status: Done
    • Priority: Low
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.11.0
    • Component/s: None
    • Labels:
      None

      Description

      This was discussed in the community advisory board meeting in relation to the proposal by Ted Zlatanov on the help list.

      https://groups.google.com/forum/#!topic/help-cfengine/cdaIS-YWV7A

      As discussed in the advisory board meeting:

      The proposal is to add an attribute that allows you to specify the variable that is used for iteration. Any $(this) in the promise gets iterated. Perhaps $(this.k) and $(this.v) to support arrays.

      The main benefits of this proposal are:

      • Avoids necessity of creating intermediate variables
        • This can make policy harder to read as the intermediate variables can obfuscate the intention. (Is this a string, a list, or an array element?)
        • This can help with avoiding the duplication of data un-necessarily.
        • Classic arrays are more expensive internally than data containers since each key value pair is its own variable.
          • Perhaps use of this pattern will result in more efficient agent runs.
          • This needs to be better clarified by a developer in the ticket.
      bundle agent main
      {
        vars:
          "mylist" slist => { "one", "two", "three" };
      
          "array[$(this)]"
            string => "$(this)",
            with_iterable => "mylist";
      
          "array[$(this)]"
            string => "$(this)",
            with_iterable => '[ "one", "two", "three" ]';
      
          "array[$(this)]"
            string => "$(this)",
            with_iterable => { "one", "two", "three" };
      
          "array[$(this)]"
            string => "$(this)",
            with_iterable => unique(maplist(dirname($(item)), filelist)),
            comment => "to collect all the directories in a list of files. This saves
                        a variable and also keeps the transformation close to the
                        point of usage";
      
      }
      

      Equivlent to:

      bundle agent main
      {
        vars:
          "mylist" slist => { "one", "two", "three" };
      
          "array[$(mylist)]"
            string => "$(mylist)";
      }
      

        Attachments

          Activity

            People

            • Assignee:
              a10003 Eystein Maloy Stenberg
              Reporter:
              a10042 Nick Anderson
            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Summary Panel