Skip to content

Instantly share code, notes, and snippets.

@tjormola
Created November 14, 2015 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjormola/4f16602b55daada5a3cd to your computer and use it in GitHub Desktop.
Save tjormola/4f16602b55daada5a3cd to your computer and use it in GitHub Desktop.
body common control
{
bundlesequence => { "datatest" };
}
body agent control
{
ifelapsed => 0;
}
bundle agent datatest
{
vars:
"data_with_list_of_strings" data => parsejson('{"key":["item1","item2"]}');
"data_with_list_of_maps" data => parsejson('{"key":[{"name":"item1","url":"url1"},{"name":"item2","url":"url2"}]}');
methods:
# "handle_list_of_strings" usebundle => handle_list_of_strings(@(data_with_list_of_strings[key]));
"handle_list_of_maps" usebundle => handle_list_of_maps(@(data_with_list_of_maps[key]));
}
bundle agent handle_list_of_strings(list)
{
vars:
"list_str" string => format("%S", list);
methods:
# Bundle is applied for each item in the list
"handle_list_items" usebundle => handle_string_item($(list));
reports:
"$(this.bundle) called with list $(list_str)";
}
bundle agent handle_list_of_maps(list)
{
vars:
"list_str" string => format("%S", list);
"list_indices" slist => getindices("list");
methods:
"handle_list_items" usebundle => handle_map_item(@(list), $(list_indices));
reports:
"$(this.bundle) called with list $(list_str)";
"$(this.bundle) list has item with name $(list[$(list_indices)][name]) and url $(list[$(list_indices)][url])";
}
bundle agent handle_string_item(item)
{
reports:
"$(this.bundle) called with item $(item)";
}
bundle agent handle_map_item(items, index)
{
methods:
"handle_string_item" usebundle => handle_string_item("$(items[$(index)][name])");
}
@tjormola
Copy link
Author

(14:13:43)(tj@ganga)(~)$ cf-agent /tmp/datatest.cf 
R: handle_string_item called with item item1
R: handle_string_item called with item item2
R: handle_list_of_maps called with list [{"name":"item1","url":"url1"},{"name":"item2","url":"url2"}]
R: handle_list_of_maps list has item with name item1 and url url1
R: handle_list_of_maps list has item with name item2 and url url2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment