You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -5,66 +5,19 @@ Use the `alias` and `anchor` operators to read and write yaml aliases and anchor
5
5
`yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag.
6
6
7
7
8
-
## Merge one map
9
-
see https://yaml.org/type/merge.html
8
+
## NOTE --yaml-fix-merge-anchor-to-spec flag
9
+
`yq` doesn't merge anchors `<<:` to spec, in some circumstances it incorrectly overrides existing keys when the spec documents not to do that.
10
10
11
-
Given a sample.yml file of:
12
-
```yaml
13
-
- &CENTER
14
-
x: 1
15
-
y: 2
16
-
- &LEFT
17
-
x: 0
18
-
y: 2
19
-
- &BIG
20
-
r: 10
21
-
- &SMALL
22
-
r: 1
23
-
- !!merge <<: *CENTER
24
-
r: 10
25
-
```
26
-
then
27
-
```bash
28
-
yq '.[4] | explode(.)' sample.yml
29
-
```
30
-
will output
31
-
```yaml
32
-
x: 1
33
-
y: 2
34
-
r: 10
35
-
```
11
+
To minimise disruption while still fixing the issue, a flag has been added to toggle this behaviour. This will first default to false; and log warnings to users. Then it will default to true (and still allow users to specify false if needed).
36
12
37
-
## Merge multiple maps
38
-
see https://yaml.org/type/merge.html
13
+
This flag also enables advanced merging, like inline maps, as well as fixes to ensure when exploding a particular path, neighbours are not affect ed.
14
+
15
+
Long story short, you should be setting this flag to true.
16
+
17
+
See examples of the flag differences below, where LEGACY is with the flag off; and FIXED is with the flag on.
39
18
40
-
Given a sample.yml file of:
41
-
```yaml
42
-
- &CENTER
43
-
x: 1
44
-
y: 2
45
-
- &LEFT
46
-
x: 0
47
-
y: 2
48
-
- &BIG
49
-
r: 10
50
-
- &SMALL
51
-
r: 1
52
-
- !!merge <<:
53
-
- *CENTER
54
-
- *BIG
55
-
```
56
-
then
57
-
```bash
58
-
yq '.[4] | explode(.)' sample.yml
59
-
```
60
-
will output
61
-
```yaml
62
-
r: 10
63
-
x: 1
64
-
y: 2
65
-
```
66
19
67
-
## Override
20
+
## Merge one map
68
21
see https://yaml.org/type/merge.html
69
22
70
23
Given a sample.yml file of:
@@ -79,21 +32,18 @@ Given a sample.yml file of:
79
32
r: 10
80
33
- &SMALL
81
34
r: 1
82
-
- !!merge <<:
83
-
- *BIG
84
-
- *LEFT
85
-
- *SMALL
86
-
x: 1
35
+
- !!merge <<: *CENTER
36
+
r: 10
87
37
```
88
38
then
89
39
```bash
90
40
yq '.[4] | explode(.)' sample.yml
91
41
```
92
42
will output
93
43
```yaml
94
-
r: 10
95
44
x: 1
96
45
y: 2
46
+
r: 10
97
47
```
98
48
99
49
## Get anchor
@@ -254,7 +204,39 @@ f:
254
204
cat: b
255
205
```
256
206
257
-
## Explode with merge anchors
207
+
## Dereference and update a field
208
+
Use explode with multiply to dereference an object
Caution: this is for when --yaml-fix-merge-anchor-to-spec=false; it's not to YAML spec because the merge anchors incorrectly override the object values (foobarList.b is set to bar_b when it should still be foobarList_b). Flag will default to true in late 2025
239
+
258
240
Given a sample.yml file of:
259
241
```yaml
260
242
foo: &foo
@@ -301,33 +283,201 @@ foobar:
301
283
thing: foobar_thing
302
284
```
303
285
304
-
## Dereference and update a field
305
-
Use explode with multiply to dereference an object
286
+
## LEGACY: Merge multiple maps
287
+
see https://yaml.org/type/merge.html. This has the correct data, but the wrong key order; set --yaml-fix-merge-anchor-to-spec=true to fix the key order.
see https://yaml.org/type/merge.html. This has the correct data, but the wrong key order; set --yaml-fix-merge-anchor-to-spec=true to fix the key order.
318
+
319
+
Given a sample.yml file of:
320
+
```yaml
321
+
- &CENTER
322
+
x: 1
323
+
y: 2
324
+
- &LEFT
325
+
x: 0
326
+
y: 2
327
+
- &BIG
328
+
r: 10
329
+
- &SMALL
330
+
r: 1
331
+
- !!merge <<:
332
+
- *BIG
333
+
- *LEFT
334
+
- *SMALL
335
+
x: 1
336
+
```
337
+
then
338
+
```bash
339
+
yq '.[4] | explode(.)' sample.yml
340
+
```
341
+
will output
342
+
```yaml
343
+
r: 10
344
+
x: 1
345
+
y: 2
346
+
```
347
+
348
+
## FIXED: Explode with merge anchors
349
+
Set `--yaml-fix-merge-anchor-to-spec=true` to get this correct merge behaviour (flag will default to true in late 2025).
350
+
Observe that foobarList.b property is still foobarList_b.
351
+
352
+
Given a sample.yml file of:
353
+
```yaml
354
+
foo: &foo
355
+
a: foo_a
356
+
thing: foo_thing
357
+
c: foo_c
358
+
bar: &bar
359
+
b: bar_b
360
+
thing: bar_thing
361
+
c: bar_c
362
+
foobarList:
363
+
b: foobarList_b
364
+
!!merge <<:
365
+
- *foo
366
+
- *bar
367
+
c: foobarList_c
368
+
foobar:
369
+
c: foobar_c
370
+
!!merge <<: *foo
371
+
thing: foobar_thing
372
+
```
373
+
then
374
+
```bash
375
+
yq 'explode(.)' sample.yml
376
+
```
377
+
will output
378
+
```yaml
379
+
foo:
380
+
a: foo_a
381
+
thing: foo_thing
382
+
c: foo_c
383
+
bar:
384
+
b: bar_b
385
+
thing: bar_thing
386
+
c: bar_c
387
+
foobarList:
388
+
b: foobarList_b
389
+
a: foo_a
390
+
thing: foo_thing
391
+
c: foobarList_c
392
+
foobar:
393
+
c: foobar_c
394
+
a: foo_a
395
+
thing: foobar_thing
396
+
```
397
+
398
+
## FIXED: Merge multiple maps
399
+
Set `--yaml-fix-merge-anchor-to-spec=true` to get this correct merge behaviour (flag will default to true in late 2025).
400
+
Taken from https://yaml.org/type/merge.html. Same values as legacy, but with the correct key order.
401
+
402
+
Given a sample.yml file of:
403
+
```yaml
404
+
- &CENTER
405
+
x: 1
406
+
y: 2
407
+
- &LEFT
408
+
x: 0
409
+
y: 2
410
+
- &BIG
411
+
r: 10
412
+
- &SMALL
413
+
r: 1
414
+
- !!merge <<:
415
+
- *CENTER
416
+
- *BIG
417
+
```
418
+
then
419
+
```bash
420
+
yq '.[4] | explode(.)' sample.yml
421
+
```
422
+
will output
423
+
```yaml
424
+
x: 1
425
+
y: 2
426
+
r: 10
427
+
```
428
+
429
+
## FIXED: Override
430
+
Set `--yaml-fix-merge-anchor-to-spec=true` to get this correct merge behaviour (flag will default to true in late 2025).
431
+
Taken from https://yaml.org/type/merge.html. Same values as legacy, but with the correct key order.
432
+
433
+
Given a sample.yml file of:
434
+
```yaml
435
+
- &CENTER
436
+
x: 1
437
+
y: 2
438
+
- &LEFT
439
+
x: 0
440
+
y: 2
441
+
- &BIG
442
+
r: 10
443
+
- &SMALL
444
+
r: 1
445
+
- !!merge <<:
446
+
- *BIG
447
+
- *LEFT
448
+
- *SMALL
449
+
x: 1
450
+
```
451
+
then
452
+
```bash
453
+
yq '.[4] | explode(.)' sample.yml
454
+
```
455
+
will output
456
+
```yaml
457
+
r: 10
458
+
y: 2
459
+
x: 1
460
+
```
461
+
462
+
## Exploding inline merge anchor
463
+
Set `--yaml-fix-merge-anchor-to-spec=true` to get this correct merge behaviour (flag will default to true in late 2025).
Copy file name to clipboardExpand all lines: pkg/yqlib/doc/operators/headers/anchor-and-alias-operators.md
+12Lines changed: 12 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,3 +4,15 @@ Use the `alias` and `anchor` operators to read and write yaml aliases and anchor
4
4
5
5
`yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag.
6
6
7
+
8
+
## NOTE --yaml-fix-merge-anchor-to-spec flag
9
+
`yq` doesn't merge anchors `<<:` to spec, in some circumstances it incorrectly overrides existing keys when the spec documents not to do that.
10
+
11
+
To minimise disruption while still fixing the issue, a flag has been added to toggle this behaviour. This will first default to false; and log warnings to users. Then it will default to true (and still allow users to specify false if needed).
12
+
13
+
This flag also enables advanced merging, like inline maps, as well as fixes to ensure when exploding a particular path, neighbours are not affect ed.
14
+
15
+
Long story short, you should be setting this flag to true.
16
+
17
+
See examples of the flag differences below, where LEGACY is with the flag off; and FIXED is with the flag on.
0 commit comments