1#  Copyright 2021 Northern.tech AS
2
3#  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
4
5#  This program is free software; you can redistribute it and/or modify it
6#  under the terms of the GNU General Public License as published by the
7#  Free Software Foundation; version 3.
8
9#  This program is distributed in the hope that it will be useful,
10#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12#  GNU General Public License for more details.
13
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
17
18# To the extent this program is licensed as part of the Enterprise
19# versions of Cfengine, the applicable Commercial Open Source License
20# (COSL) may apply to this file if you as a licensee so wish it. See
21# included file COSL.txt.
22
23#+begin_src cfengine3
24body common control
25{
26      bundlesequence => { "run" };
27}
28
29bundle agent run
30{
31  vars:
32      "tester" data => '{ "x": 100, "y": [ true, "a", "b" ] }';
33
34      # "jq ." returns the same thing that was passed in
35      "pipe_passthrough" data => mapdata("json_pipe", '$(def.jq) .', tester);
36      "pipe_passthrough_str" string => format("%S", pipe_passthrough);
37
38      # "jq .x" returns what was under x wrapped in an array: [100]
39      "pipe_justx" data => mapdata("json_pipe", '$(def.jq) .x', tester);
40      "pipe_justx_str" string => format("%S", pipe_justx);
41
42      # "jq .y" returns what was under y wrapped in an array: [[true,"a","b"]]
43      "pipe_justy" data => mapdata("json_pipe", '$(def.jq) .y', tester);
44      "pipe_justy_str" string => format("%S", pipe_justy);
45
46      # "jq .y[]" returns each entry under y *separately*: [true,"a","b"]
47      "pipe_yarray" data => mapdata("json_pipe", '$(def.jq) .y[]', tester);
48      "pipe_yarray_str" string => format("%S", pipe_yarray);
49
50      # "jq .z" returns null because the key "z" is missing: [null]
51      "pipe_justz" data => mapdata("json_pipe", '$(def.jq) .z', tester);
52      "pipe_justz_str" string => format("%S", pipe_justz);
53
54      # "jq" can do math too! and much more!
55      "pipe_jqmath" data => mapdata("json_pipe", '$(def.jq) 1+2+3', tester);
56      "pipe_jqmath_str" string => format("%S", pipe_jqmath);
57
58  reports:
59      "mapdata/json_pipe passthrough result: $(pipe_passthrough_str)";
60      "mapdata/json_pipe just x result: $(pipe_justx_str)";
61      "mapdata/json_pipe just y result: $(pipe_justy_str)";
62      "mapdata/json_pipe array under y result: $(pipe_yarray_str)";
63      "mapdata/json_pipe just z result: $(pipe_justz_str)";
64      "mapdata/json_pipe math expression result: $(pipe_jqmath_str)";
65}
66
67#+end_src
68###############################################################################
69#+begin_src output
70#@ ```
71#@ R: mapdata/json_pipe passthrough result: [{"x":100,"y":[true,"a","b"]}]
72#@ R: mapdata/json_pipe just x result: [100]
73#@ R: mapdata/json_pipe just y result: [[true,"a","b"]]
74#@ R: mapdata/json_pipe array under y result: [true,"a","b"]
75#@ R: mapdata/json_pipe just z result: [null]
76#@ R: mapdata/json_pipe math expression result: [6]
77#@ ```
78#+end_src
79