Skip to content

Commit be304a1

Browse files
committed
Added another comment test
1 parent a4b6003 commit be304a1

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

examples/data1.yaml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
# --------------------------------------------------
2-
# It's a test with comment
3-
# --------------------------------------------------
4-
test:
5-
- name: a
6-
- name: b
7-
- name: c
8-
groups:
9-
- name: a
10-
- name: b
11-
- name: c
12-
- name: c
1+
volumes:
2+
beehive-conf:
3+
driver: local
4+
services:
5+
- beehive

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#
1717
{% endhint %}
1818

1919
## Set line comment
20+
Set the comment on the key node for more reliability (see below).
21+
2022
Given a sample.yml file of:
2123
```yaml
2224
a: cat
@@ -30,6 +32,24 @@ will output
3032
a: cat # single
3133
```
3234
35+
## Set line comment of a maps/arrays
36+
For maps and arrays, you need to set the line comment on the _key_ node. This will also work for scalars.
37+
38+
Given a sample.yml file of:
39+
```yaml
40+
a:
41+
b: things
42+
```
43+
then
44+
```bash
45+
yq '(.a | key) line_comment="single"' sample.yml
46+
```
47+
will output
48+
```yaml
49+
a: # single
50+
b: things
51+
```
52+
3353
## Use update assign to perform relative updates
3454
Given a sample.yml file of:
3555
```yaml

pkg/yqlib/operator_comments_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ import (
66

77
var commentOperatorScenarios = []expressionScenario{
88
{
9-
description: "Set line comment",
10-
document: `a: cat`,
11-
expression: `.a line_comment="single"`,
9+
description: "Set line comment",
10+
subdescription: "Set the comment on the key node for more reliability (see below).",
11+
document: `a: cat`,
12+
expression: `.a line_comment="single"`,
1213
expected: []string{
1314
"D0, P[], (doc)::a: cat # single\n",
1415
},
1516
},
17+
{
18+
description: "Set line comment of a maps/arrays",
19+
subdescription: "For maps and arrays, you need to set the line comment on the _key_ node. This will also work for scalars.",
20+
document: "a:\n b: things",
21+
expression: `(.a | key) line_comment="single"`,
22+
expected: []string{
23+
"D0, P[], (doc)::a: # single\n b: things\n",
24+
},
25+
},
1626
{
1727
skipDoc: true,
1828
document: "a: cat\nb: dog",

0 commit comments

Comments
 (0)