Skip to content

Commit 2362451

Browse files
committed
Added comment operator examples
1 parent 55a7fdf commit 2362451

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,86 @@ a: cat # cat
6666
b: dog # dog
6767
```
6868
69+
## Where is the comment - map key example
70+
The underlying yaml parser can assign comments in a document to surprising nodes. Use an expression like this to find where you comment is. 'p' indicates the path, 'isKey' is if the node is a map key (as opposed to a map value).
71+
From this, you can see the 'hello-world-comment' is actually on the 'hello' key
72+
73+
Given a sample.yml file of:
74+
```yaml
75+
hello: # hello-world-comment
76+
message: world
77+
```
78+
then
79+
```bash
80+
yq '[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]' sample.yml
81+
```
82+
will output
83+
```yaml
84+
- p: ""
85+
isKey: false
86+
hc: ""
87+
lc: ""
88+
fc: ""
89+
- p: hello
90+
isKey: true
91+
hc: ""
92+
lc: hello-world-comment
93+
fc: ""
94+
- p: hello
95+
isKey: false
96+
hc: ""
97+
lc: ""
98+
fc: ""
99+
- p: hello.message
100+
isKey: true
101+
hc: ""
102+
lc: ""
103+
fc: ""
104+
- p: hello.message
105+
isKey: false
106+
hc: ""
107+
lc: ""
108+
fc: ""
109+
```
110+
111+
## Where is the comment - array example
112+
The underlying yaml parser can assign comments in a document to surprising nodes. Use an expression like this to find where you comment is. 'p' indicates the path, 'isKey' is if the node is a map key (as opposed to a map value).
113+
From this, you can see the 'under-name-comment' is actually on the first child
114+
115+
Given a sample.yml file of:
116+
```yaml
117+
name:
118+
# under-name-comment
119+
- first-array-child
120+
```
121+
then
122+
```bash
123+
yq '[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]' sample.yml
124+
```
125+
will output
126+
```yaml
127+
- p: ""
128+
isKey: false
129+
hc: ""
130+
lc: ""
131+
fc: ""
132+
- p: name
133+
isKey: true
134+
hc: ""
135+
lc: ""
136+
fc: ""
137+
- p: name
138+
isKey: false
139+
hc: ""
140+
lc: ""
141+
fc: ""
142+
- p: name.0
143+
isKey: false
144+
hc: under-name-comment
145+
lc: ""
146+
fc: ""
147+
```
148+
69149
## Set head comment
70150
Given a sample.yml file of:
71151
```yaml

pkg/yqlib/operator_comments_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,55 @@ import (
44
"testing"
55
)
66

7+
var expectedWhereIsMyCommentMapKey = `D0, P[], (!!seq)::- p: ""
8+
isKey: false
9+
hc: ""
10+
lc: ""
11+
fc: ""
12+
- p: hello
13+
isKey: true
14+
hc: ""
15+
lc: hello-world-comment
16+
fc: ""
17+
- p: hello
18+
isKey: false
19+
hc: ""
20+
lc: ""
21+
fc: ""
22+
- p: hello.message
23+
isKey: true
24+
hc: ""
25+
lc: ""
26+
fc: ""
27+
- p: hello.message
28+
isKey: false
29+
hc: ""
30+
lc: ""
31+
fc: ""
32+
`
33+
34+
var expectedWhereIsMyCommentArray = `D0, P[], (!!seq)::- p: ""
35+
isKey: false
36+
hc: ""
37+
lc: ""
38+
fc: ""
39+
- p: name
40+
isKey: true
41+
hc: ""
42+
lc: ""
43+
fc: ""
44+
- p: name
45+
isKey: false
46+
hc: ""
47+
lc: ""
48+
fc: ""
49+
- p: name.0
50+
isKey: false
51+
hc: under-name-comment
52+
lc: ""
53+
fc: ""
54+
`
55+
756
var commentOperatorScenarios = []expressionScenario{
857
{
958
description: "Set line comment",
@@ -56,6 +105,24 @@ var commentOperatorScenarios = []expressionScenario{
56105
"D0, P[], (!!map)::a: cat # cat\n# cat\n\n# cat\nb: dog # dog\n# dog\n\n# dog\n",
57106
},
58107
},
108+
{
109+
description: "Where is the comment - map key example",
110+
subdescription: "The underlying yaml parser can assign comments in a document to surprising nodes. Use an expression like this to find where you comment is. 'p' indicates the path, 'isKey' is if the node is a map key (as opposed to a map value).\nFrom this, you can see the 'hello-world-comment' is actually on the 'hello' key",
111+
document: "hello: # hello-world-comment\n message: world",
112+
expression: `[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]`,
113+
expected: []string{
114+
expectedWhereIsMyCommentMapKey,
115+
},
116+
},
117+
{
118+
description: "Where is the comment - array example",
119+
subdescription: "The underlying yaml parser can assign comments in a document to surprising nodes. Use an expression like this to find where you comment is. 'p' indicates the path, 'isKey' is if the node is a map key (as opposed to a map value).\nFrom this, you can see the 'under-name-comment' is actually on the first child",
120+
document: "name:\n # under-name-comment\n - first-array-child",
121+
expression: `[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]`,
122+
expected: []string{
123+
expectedWhereIsMyCommentArray,
124+
},
125+
},
59126
{
60127
description: "Set head comment",
61128
document: `a: cat`,

0 commit comments

Comments
 (0)