Skip to content

Commit 3dd5f0c

Browse files
committed
Added another comment test
1 parent be304a1 commit 3dd5f0c

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

pkg/yqlib/doc/operators/comment-operators.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Comment Operators
22

3-
Use these comment operators to set or retrieve comments.
3+
Use these comment operators to set or retrieve comments. Note that line comments on maps/arrays are actually set on the _key_ node as opposed to the _value_ (map/array). See below for examples.
44

55
Like the `=` and `|=` assign operators, the same syntax applies when updating comments:
66

@@ -82,6 +82,25 @@ will output
8282
a: cat
8383
```
8484
85+
## Set head comment of a map entry
86+
Given a sample.yml file of:
87+
```yaml
88+
f: foo
89+
a:
90+
b: cat
91+
```
92+
then
93+
```bash
94+
yq '(.a | key) head_comment="single"' sample.yml
95+
```
96+
will output
97+
```yaml
98+
f: foo
99+
# single
100+
a:
101+
b: cat
102+
```
103+
85104
## Set foot comment, using an expression
86105
Given a sample.yml file of:
87106
```yaml

pkg/yqlib/doc/operators/headers/comment-operators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Comment Operators
22

3-
Use these comment operators to set or retrieve comments.
3+
Use these comment operators to set or retrieve comments. Note that line comments on maps/arrays are actually set on the _key_ node as opposed to the _value_ (map/array). See below for examples.
44

55
Like the `=` and `|=` assign operators, the same syntax applies when updating comments:
66

pkg/yqlib/operator_comments_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ var commentOperatorScenarios = []expressionScenario{
6464
"D0, P[], (doc)::# single\n\na: cat\n",
6565
},
6666
},
67+
{
68+
description: "Set head comment of a map entry",
69+
document: "f: foo\na:\n b: cat",
70+
expression: `(.a | key) head_comment="single"`,
71+
expected: []string{
72+
"D0, P[], (doc)::f: foo\n# single\na:\n b: cat\n",
73+
},
74+
},
6775
{
6876
description: "Set foot comment, using an expression",
6977
document: `a: cat`,

0 commit comments

Comments
 (0)