Skip to content

Commit 55c5267

Browse files
committed
classfilterdata() now supports object of objects and object of arrays
The class expression can be stored in either the key to the object itself, or key/index inside object. Ticket: CFE-4562 Changelog: Commit Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent 5376ae9 commit 55c5267

9 files changed

+658
-69
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#+begin_src cfengine3
2+
bundle agent __main__
3+
{
4+
classes:
5+
"role_2";
6+
7+
vars:
8+
"original"
9+
data => '{
10+
"alice": [ "role_1", 32 ],
11+
"bob": [ "!role_1", 24 ],
12+
"malcom": [ "role_2", 27 ]
13+
}';
14+
15+
"filtered"
16+
data => classfilterdata("original", "object_of_arrays", "0");
17+
18+
reports:
19+
"Filtered data: $(with)"
20+
with => storejson("filtered");
21+
}
22+
#+end_src
23+
24+
#+begin_src example_output
25+
#@ ```
26+
#@ R: Filtered data: {
27+
#@ "bob": [
28+
#@ "!role_1",
29+
#@ 24
30+
#@ ],
31+
#@ "malcom": [
32+
#@ "role_2",
33+
#@ 27
34+
#@ ]
35+
#@ }
36+
#@ ```
37+
#+end_src
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#+begin_src cfengine3
2+
bundle agent __main__
3+
{
4+
classes:
5+
"role_2";
6+
7+
vars:
8+
"original"
9+
data => '{
10+
"role_1" : [ "alice", 32 ],
11+
"!role_1" : [ "bob", 24 ],
12+
"role_2" : [ "malcom", 27 ]
13+
}';
14+
15+
# Using exogenous key (i.e., the key of the child array within the parent object)
16+
"filtered"
17+
data => classfilterdata("original", "object_of_arrays");
18+
19+
reports:
20+
"Filtered data: $(with)"
21+
with => storejson("filtered");
22+
}
23+
#+end_src
24+
25+
#+begin_src example_output
26+
#@ ```
27+
#@ R: Filtered data: {
28+
#@ "!role_1": [
29+
#@ "bob",
30+
#@ 24
31+
#@ ],
32+
#@ "role_2": [
33+
#@ "malcom",
34+
#@ 27
35+
#@ ]
36+
#@ }
37+
#@ ```
38+
#+end_src
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#+begin_src cfengine3
2+
bundle agent __main__
3+
{
4+
classes:
5+
"role_2";
6+
7+
vars:
8+
"original"
9+
data => '{
10+
"/tmp/foo": { "ifvarclass": "role_1" },
11+
"/tmp/bar": { "ifvarclass": "role_2" },
12+
"/tmp/baz": { "ifvarclass": "(role_1|role_2)" }
13+
}';
14+
15+
"filtered"
16+
data => classfilterdata("original", "object_of_objects", "ifvarclass");
17+
18+
reports:
19+
"Filtered data: $(with)"
20+
with => storejson("filtered");
21+
}
22+
#+end_src
23+
24+
#+begin_src example_output
25+
#@ ```
26+
#@ R: Filtered data: {
27+
#@ "/tmp/bar": {
28+
#@ "ifvarclass": "role_2"
29+
#@ },
30+
#@ "/tmp/baz": {
31+
#@ "ifvarclass": "(role_1|role_2)"
32+
#@ }
33+
#@ }
34+
#@ ```
35+
#+end_src
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#+begin_src cfengine3
2+
bundle agent __main__
3+
{
4+
classes:
5+
"role_2";
6+
7+
vars:
8+
"original"
9+
data => '{
10+
"role_1": { "file": "/tmp/foo" },
11+
"role_2": { "file": "/tmp/bar" },
12+
"(role_1|role_2)": { "file": "/tmp/baz" }
13+
}';
14+
15+
# Using exogenous key (i.e., the key of the child object within the parent object)
16+
"filtered"
17+
data => classfilterdata("original", "object_of_objects");
18+
19+
reports:
20+
"Filtered data: $(with)"
21+
with => storejson("filtered");
22+
}
23+
#+end_src
24+
25+
#+begin_src example_output
26+
#@ ```
27+
#@ R: Filtered data: {
28+
#@ "(role_1|role_2)": {
29+
#@ "file": "/tmp/baz"
30+
#@ },
31+
#@ "role_2": {
32+
#@ "file": "/tmp/bar"
33+
#@ }
34+
#@ }
35+
#@ ```
36+
#+end_src

0 commit comments

Comments
 (0)