Skip to content

Commit ba1b973

Browse files
committed
Added recursive update map keys example
1 parent d74e701 commit ba1b973

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

pkg/yqlib/doc/operators/entries.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Similar to the same named functions in `jq` these functions convert to/from an object and an array of key-value pairs. This is most useful for performing operations on keys of maps.
44

5+
Use `with_entries(op)` as a syntatic sugar for doing `to_entries | op | from_entries`.
6+
57
## to_entries Map
68
Given a sample.yml file of:
79
```yaml
@@ -101,6 +103,28 @@ KEY_a: 1
101103
KEY_b: 2
102104
```
103105
106+
## Use with_entries to update keys recursively
107+
We use (.. | select(tag="map")) to find all the maps in the doc, then |= to update each one of those maps. In the update, with_entries is used.
108+
109+
Given a sample.yml file of:
110+
```yaml
111+
a: 1
112+
b:
113+
b_a: nested
114+
b_b: thing
115+
```
116+
then
117+
```bash
118+
yq '(.. | select(tag=="!!map")) |= with_entries(.key |= "KEY_" + .)' sample.yml
119+
```
120+
will output
121+
```yaml
122+
KEY_a: 1
123+
KEY_b:
124+
KEY_b_a: nested
125+
KEY_b_b: thing
126+
```
127+
104128
## Custom sort map keys
105129
Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.
106130
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Entries
22

33
Similar to the same named functions in `jq` these functions convert to/from an object and an array of key-value pairs. This is most useful for performing operations on keys of maps.
4+
5+
Use `with_entries(op)` as a syntatic sugar for doing `to_entries | op | from_entries`.

pkg/yqlib/doc/operators/sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ cool:
110110
```
111111
112112
## Sort a map
113-
Sorting a map, by default, will sort by the values
113+
Sorting a map, by default this will sort by the values
114114
115115
Given a sample.yml file of:
116116
```yaml

pkg/yqlib/operator_entries_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ var entriesOperatorScenarios = []expressionScenario{
7272
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n",
7373
},
7474
},
75+
{
76+
description: "Use with_entries to update keys recursively",
77+
document: `{a: 1, b: {b_a: nested, b_b: thing}}`,
78+
expression: `(.. | select(tag=="!!map")) |= with_entries(.key |= "KEY_" + .)`,
79+
subdescription: "We use (.. | select(tag=\"map\")) to find all the maps in the doc, then |= to update each one of those maps. In the update, with_entries is used.",
80+
expected: []string{
81+
"D0, P[], (!!map)::{KEY_a: 1, KEY_b: {KEY_b_a: nested, KEY_b_b: thing}}\n",
82+
},
83+
},
7584
{
7685
skipDoc: true,
7786
description: "Use with_entries to update keys comment",

0 commit comments

Comments
 (0)