Skip to content

Commit 60f30f8

Browse files
committed
Added comment operator examples
1 parent 2362451 commit 60f30f8

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ will output
108108
fc: ""
109109
```
110110
111+
## Retreive comment - map key example
112+
From the previous example, we know that the comment is on the 'hello' _key_ as a lineComment
113+
114+
Given a sample.yml file of:
115+
```yaml
116+
hello: # hello-world-comment
117+
message: world
118+
```
119+
then
120+
```bash
121+
yq '.hello | key | line_comment' sample.yml
122+
```
123+
will output
124+
```yaml
125+
hello-world-comment
126+
```
127+
111128
## Where is the comment - array example
112129
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).
113130
From this, you can see the 'under-name-comment' is actually on the first child
@@ -146,6 +163,24 @@ will output
146163
fc: ""
147164
```
148165
166+
## Retreive comment - array example
167+
From the previous example, we know that the comment is on the first child as a headComment
168+
169+
Given a sample.yml file of:
170+
```yaml
171+
name:
172+
# under-name-comment
173+
- first-array-child
174+
```
175+
then
176+
```bash
177+
yq '.name[0] | headComment' sample.yml
178+
```
179+
will output
180+
```yaml
181+
under-name-comment
182+
```
183+
149184
## Set head comment
150185
Given a sample.yml file of:
151186
```yaml

pkg/yqlib/operator_comments_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ var commentOperatorScenarios = []expressionScenario{
114114
expectedWhereIsMyCommentMapKey,
115115
},
116116
},
117+
{
118+
description: "Retreive comment - map key example",
119+
subdescription: "From the previous example, we know that the comment is on the 'hello' _key_ as a lineComment",
120+
document: "hello: # hello-world-comment\n message: world",
121+
expression: `.hello | key | line_comment`,
122+
expected: []string{
123+
"D0, P[hello], (!!str)::hello-world-comment\n",
124+
},
125+
},
117126
{
118127
description: "Where is the comment - array example",
119128
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",
@@ -123,6 +132,15 @@ var commentOperatorScenarios = []expressionScenario{
123132
expectedWhereIsMyCommentArray,
124133
},
125134
},
135+
{
136+
description: "Retreive comment - array example",
137+
subdescription: "From the previous example, we know that the comment is on the first child as a headComment",
138+
document: "name:\n # under-name-comment\n - first-array-child",
139+
expression: `.name[0] | headComment`,
140+
expected: []string{
141+
"D0, P[name 0], (!!str)::under-name-comment\n",
142+
},
143+
},
126144
{
127145
description: "Set head comment",
128146
document: `a: cat`,

0 commit comments

Comments
 (0)