Added another comment test

This commit is contained in:
Mike Farah 2022-09-30 10:15:41 +10:00
parent be304a1102
commit 3dd5f0c80c
3 changed files with 29 additions and 2 deletions

View File

@ -1,6 +1,6 @@
# Comment Operators
Use these comment operators to set or retrieve comments.
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.
Like the `=` and `|=` assign operators, the same syntax applies when updating comments:
@ -82,6 +82,25 @@ will output
a: cat
```
## Set head comment of a map entry
Given a sample.yml file of:
```yaml
f: foo
a:
b: cat
```
then
```bash
yq '(.a | key) head_comment="single"' sample.yml
```
will output
```yaml
f: foo
# single
a:
b: cat
```
## Set foot comment, using an expression
Given a sample.yml file of:
```yaml

View File

@ -1,6 +1,6 @@
# Comment Operators
Use these comment operators to set or retrieve comments.
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.
Like the `=` and `|=` assign operators, the same syntax applies when updating comments:

View File

@ -64,6 +64,14 @@ var commentOperatorScenarios = []expressionScenario{
"D0, P[], (doc)::# single\n\na: cat\n",
},
},
{
description: "Set head comment of a map entry",
document: "f: foo\na:\n b: cat",
expression: `(.a | key) head_comment="single"`,
expected: []string{
"D0, P[], (doc)::f: foo\n# single\na:\n b: cat\n",
},
},
{
description: "Set foot comment, using an expression",
document: `a: cat`,