Skip to content

Commit d5757fc

Browse files
committed
Working on clarifying docs
1 parent db2a455 commit d5757fc

File tree

3 files changed

+50
-129
lines changed

3 files changed

+50
-129
lines changed

pkg/yqlib/doc/operators/anchor-and-alias-operators.md

Lines changed: 28 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ Use the `alias` and `anchor` operators to read and write yaml aliases and anchor
88
## NOTE --yaml-fix-merge-anchor-to-spec flag
99
`yq` doesn't merge anchors `<<:` to spec, in some circumstances it incorrectly overrides existing keys when the spec documents not to do that.
1010

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)
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).
1212

13-
See examples of the flag difference below.
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.
1418

1519

1620
## Merge one map
@@ -279,8 +283,8 @@ foobar:
279283
thing: foobar_thing
280284
```
281285
282-
## Merge multiple maps
283-
see https://yaml.org/type/merge.html
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.
284288
285289
Given a sample.yml file of:
286290
```yaml
@@ -309,8 +313,8 @@ x: 1
309313
y: 2
310314
```
311315
312-
## Override
313-
see https://yaml.org/type/merge.html
316+
## LEGACY: Override
317+
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.
314318
315319
Given a sample.yml file of:
316320
```yaml
@@ -342,7 +346,8 @@ y: 2
342346
```
343347
344348
## FIXED: Explode with merge anchors
345-
See the foobarList.b property is still foobarList_b. Set `--yaml-fix-merge-anchor-to-spec=true` to get this correct merge behaviour. Flag will default to true in late 2025
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.
346351

347352
Given a sample.yml file of:
348353
```yaml
@@ -390,8 +395,9 @@ foobar:
390395
thing: foobar_thing
391396
```
392397

393-
## Merge multiple maps
394-
see https://yaml.org/type/merge.html
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.
395401

396402
Given a sample.yml file of:
397403
```yaml
@@ -420,8 +426,9 @@ y: 2
420426
r: 10
421427
```
422428

423-
## Override
424-
see https://yaml.org/type/merge.html
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.
425432

426433
Given a sample.yml file of:
427434
```yaml
@@ -452,114 +459,25 @@ y: 2
452459
x: 1
453460
```
454461

455-
## FIXED: Explode with merge anchors
456-
See the foobarList.b property is still foobarList_b. Set `--yaml-fix-merge-anchor-to-spec=true` to get this correct merge behaviour. Flag will default to true in late 2025
457-
458-
Given a sample.yml file of:
459-
```yaml
460-
foo: &foo
461-
a: foo_a
462-
thing: foo_thing
463-
c: foo_c
464-
bar: &bar
465-
b: bar_b
466-
thing: bar_thing
467-
c: bar_c
468-
foobarList:
469-
b: foobarList_b
470-
!!merge <<:
471-
- *foo
472-
- *bar
473-
c: foobarList_c
474-
foobar:
475-
c: foobar_c
476-
!!merge <<: *foo
477-
thing: foobar_thing
478-
```
479-
then
480-
```bash
481-
yq 'explode(.)' sample.yml
482-
```
483-
will output
484-
```yaml
485-
foo:
486-
a: foo_a
487-
thing: foo_thing
488-
c: foo_c
489-
bar:
490-
b: bar_b
491-
thing: bar_thing
492-
c: bar_c
493-
foobarList:
494-
b: foobarList_b
495-
a: foo_a
496-
thing: foo_thing
497-
c: foobarList_c
498-
foobar:
499-
c: foobar_c
500-
a: foo_a
501-
thing: foobar_thing
502-
```
503-
504-
## Merge multiple maps
505-
see https://yaml.org/type/merge.html
506-
507-
Given a sample.yml file of:
508-
```yaml
509-
- &CENTER
510-
x: 1
511-
y: 2
512-
- &LEFT
513-
x: 0
514-
y: 2
515-
- &BIG
516-
r: 10
517-
- &SMALL
518-
r: 1
519-
- !!merge <<:
520-
- *CENTER
521-
- *BIG
522-
```
523-
then
524-
```bash
525-
yq '.[4] | explode(.)' sample.yml
526-
```
527-
will output
528-
```yaml
529-
x: 1
530-
y: 2
531-
r: 10
532-
```
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).
533464

534-
## Override
535-
see https://yaml.org/type/merge.html
536465

537466
Given a sample.yml file of:
538467
```yaml
539-
- &CENTER
540-
x: 1
541-
y: 2
542-
- &LEFT
543-
x: 0
544-
y: 2
545-
- &BIG
546-
r: 10
547-
- &SMALL
548-
r: 1
549-
- !!merge <<:
550-
- *BIG
551-
- *LEFT
552-
- *SMALL
553-
x: 1
468+
a:
469+
b: &b 42
470+
!!merge <<:
471+
c: *b
554472
```
555473
then
556474
```bash
557-
yq '.[4] | explode(.)' sample.yml
475+
yq 'explode(.) | sort_keys(.)' sample.yml
558476
```
559477
will output
560478
```yaml
561-
r: 10
562-
y: 2
563-
x: 1
479+
a:
480+
b: 42
481+
c: 42
564482
```
565483

pkg/yqlib/operator_anchors_aliases_test.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ var fixedAnchorOperatorScenarios = []expressionScenario{
111111
},
112112
{
113113
description: "FIXED: Explode with merge anchors",
114-
subdescription: "See the foobarList.b property is still foobarList_b. Set `--yaml-fix-merge-anchor-to-spec=true` to get this correct merge behaviour. Flag will default to true in late 2025 ",
114+
subdescription: "Observe that foobarList.b property is still foobarList_b.",
115115
document: mergeDocSample,
116116
expression: `explode(.)`,
117117
expected: []string{explodeMergeAnchorsFixedExpected},
@@ -137,25 +137,24 @@ var fixedAnchorOperatorScenarios = []expressionScenario{
137137
},
138138
},
139139
{
140-
description: "Merge multiple maps",
141-
subdescription: "see https://yaml.org/type/merge.html",
140+
description: "FIXED: Merge multiple maps",
141+
subdescription: "Taken from https://yaml.org/type/merge.html. Same values as legacy, but with the correct key order.",
142142
document: specDocument + "- << : [ *CENTER, *BIG ]\n",
143143
expression: ".[4] | explode(.)",
144144
expected: []string{"D0, P[4], (!!map)::x: 1\ny: 2\nr: 10\n"},
145145
},
146146
{
147-
description: "Override",
148-
subdescription: "see https://yaml.org/type/merge.html",
147+
description: "FIXED: Override",
148+
subdescription: "Taken from https://yaml.org/type/merge.html. Same values as legacy, but with the correct key order.",
149149
document: specDocument + "- << : [ *BIG, *LEFT, *SMALL ]\n x: 1\n",
150150
expression: ".[4] | explode(.)",
151151
expected: []string{"D0, P[4], (!!map)::r: 10\ny: 2\nx: 1\n"},
152152
},
153153
{
154-
skipDoc: true,
155-
description: "Exploding inline merge anchor",
156-
subdescription: "`<<` map must be exploded, otherwise `c: *b` will become invalid",
157-
document: `{a: {b: &b 42}, <<: {c: *b}}`,
158-
expression: `explode(.) | sort_keys(.)`,
154+
description: "Exploding inline merge anchor",
155+
// subdescription: "`<<` map must be exploded, otherwise `c: *b` will become invalid",
156+
document: `{a: {b: &b 42}, <<: {c: *b}}`,
157+
expression: `explode(.) | sort_keys(.)`,
159158
expected: []string{
160159
"D0, P[], (!!map)::{a: {b: 42}, c: 42}\n",
161160
},
@@ -255,18 +254,19 @@ var badAnchorOperatorScenarios = []expressionScenario{
255254
},
256255
},
257256
{
258-
description: "Merge multiple maps",
259-
subdescription: "see https://yaml.org/type/merge.html",
257+
description: "LEGACY: Merge multiple maps",
258+
subdescription: "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.",
260259
document: specDocument + "- << : [ *CENTER, *BIG ]\n",
261260
expression: ".[4] | explode(.)",
262-
expected: []string{"D0, P[4], (!!map)::r: 10\nx: 1\ny: 2\n"}, // correct data, but wrong key order
261+
expected: []string{"D0, P[4], (!!map)::r: 10\nx: 1\ny: 2\n"},
263262
},
264263
{
265-
description: "Override",
266-
subdescription: "see https://yaml.org/type/merge.html",
267-
document: specDocument + "- << : [ *BIG, *LEFT, *SMALL ]\n x: 1\n",
268-
expression: ".[4] | explode(.)",
269-
expected: []string{"D0, P[4], (!!map)::r: 10\nx: 1\ny: 2\n"},
264+
description: "LEGACY: Override",
265+
subdescription: "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.",
266+
267+
document: specDocument + "- << : [ *BIG, *LEFT, *SMALL ]\n x: 1\n",
268+
expression: ".[4] | explode(.)",
269+
expected: []string{"D0, P[4], (!!map)::r: 10\nx: 1\ny: 2\n"},
270270
},
271271
}
272272

@@ -501,7 +501,10 @@ func TestAnchorAliasOperatorAlignedToSpecScenarios(t *testing.T) {
501501
ConfiguredYamlPreferences.FixMergeAnchorToSpec = true
502502
for _, tt := range append(fixedAnchorOperatorScenarios, anchorOperatorScenarios...) {
503503
testScenario(t, &tt)
504+
}
504505

506+
for i, tt := range fixedAnchorOperatorScenarios {
507+
fixedAnchorOperatorScenarios[i].subdescription = "Set `--yaml-fix-merge-anchor-to-spec=true` to get this correct merge behaviour (flag will default to true in late 2025).\n" + tt.subdescription
505508
}
506509
appendOperatorDocumentScenario(t, "anchor-and-alias-operators", fixedAnchorOperatorScenarios)
507510
ConfiguredYamlPreferences.FixMergeAnchorToSpec = false

pkg/yqlib/operators_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func documentScenarios(t *testing.T, folder string, title string, scenarios []in
255255

256256
func appendOperatorDocumentScenario(t *testing.T, title string, scenarios []expressionScenario) {
257257
filename := fmt.Sprintf("doc/%v/%v.md", "operators", title)
258-
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND, fs.ModeAppend)
258+
f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_APPEND, fs.ModeAppend)
259259
if err != nil {
260260
t.Error(err)
261261
return

0 commit comments

Comments
 (0)