Skip to content

Commit b0ab069

Browse files
authored
Support line comment for flow sequence or flow map (#834)
* support line comment for flow sequence or flow map * fix lint error * fix bytes unmarshaler for flow sequence or flow map with line comment
1 parent 9e98b0c commit b0ab069

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

ast/ast.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,11 @@ func (n *SequenceNode) flowStyleString() string {
16231623
for _, value := range n.Values {
16241624
values = append(values, value.String())
16251625
}
1626-
return fmt.Sprintf("[%s]", strings.Join(values, ", "))
1626+
seqText := fmt.Sprintf("[%s]", strings.Join(values, ", "))
1627+
if n.Comment != nil {
1628+
return addCommentString(seqText, n.Comment)
1629+
}
1630+
return seqText
16271631
}
16281632

16291633
func (n *SequenceNode) blockStyleString() string {

decode.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ func (d *Decoder) addSequenceNodeCommentToMap(node *ast.SequenceNode) {
288288
texts = append(texts, comment.Token.Value)
289289
}
290290
if len(texts) != 0 {
291-
d.addCommentToMap(node.Values[0].GetPath(), HeadComment(texts...))
291+
if len(node.Values) != 0 {
292+
d.addCommentToMap(node.Values[0].GetPath(), HeadComment(texts...))
293+
}
292294
}
293295
}
294296
}

decode_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,7 +2955,10 @@ func (u *unmarshalList) UnmarshalYAML(b []byte) error {
29552955
29562956
hello
29572957
f: g
2958-
- h: i`
2958+
- h: i
2959+
- j: [] # comment
2960+
- k: {} # comment
2961+
`
29592962
actual := "\n" + string(b)
29602963
if expected != actual {
29612964
return fmt.Errorf("unexpected bytes: expected [%q] but got [%q]", expected, actual)
@@ -3007,6 +3010,8 @@ a:
30073010
hello
30083011
f: g
30093012
- h: i
3013+
- j: [] # comment
3014+
- k: {} # comment
30103015
`
30113016
var v struct {
30123017
A unmarshalList
@@ -3015,7 +3020,7 @@ a:
30153020
if err := yaml.UnmarshalWithOptions([]byte(yml), &v, yaml.CommentToMap(cm)); err != nil {
30163021
t.Fatal(err)
30173022
}
3018-
if len(v.A.v) != 2 {
3023+
if len(v.A.v) != 4 {
30193024
t.Fatalf("failed to unmarshal %+v", v)
30203025
}
30213026
if len(v.A.v[0]) != 3 {

internal/format/format.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,9 @@ func (f *Formatter) formatMapping(n *ast.MappingNode) string {
351351
var ret string
352352
if n.IsFlowStyle {
353353
ret = f.origin(n.Start)
354+
} else {
355+
ret += f.formatCommentGroup(n.Comment)
354356
}
355-
ret += f.formatCommentGroup(n.Comment)
356357
for _, value := range n.Values {
357358
if value.CollectEntry != nil {
358359
ret += f.origin(value.CollectEntry)
@@ -361,6 +362,7 @@ func (f *Formatter) formatMapping(n *ast.MappingNode) string {
361362
}
362363
if n.IsFlowStyle {
363364
ret += f.origin(n.End)
365+
ret += f.formatCommentGroup(n.Comment)
364366
}
365367
return ret
366368
}
@@ -377,8 +379,7 @@ func (f *Formatter) formatSequence(n *ast.SequenceNode) string {
377379
var ret string
378380
if n.IsFlowStyle {
379381
ret = f.origin(n.Start)
380-
}
381-
if n.Comment != nil {
382+
} else {
382383
// add head comment.
383384
ret += f.formatCommentGroup(n.Comment)
384385
}
@@ -387,6 +388,7 @@ func (f *Formatter) formatSequence(n *ast.SequenceNode) string {
387388
}
388389
if n.IsFlowStyle {
389390
ret += f.origin(n.End)
391+
ret += f.formatCommentGroup(n.Comment)
390392
}
391393
ret += f.formatCommentGroup(n.FootComment)
392394
return ret

parser/parser.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ func (p *parser) parseFlowMap(ctx *context) (*ast.MappingNode, error) {
426426
if node.End == nil {
427427
return nil, errors.ErrSyntax("could not find flow mapping end token '}'", node.Start)
428428
}
429+
430+
// set line comment if exists. e.g.) } # comment
431+
if err := setLineComment(ctx, node, ctx.currentToken()); err != nil {
432+
return nil, err
433+
}
429434
ctx.goNext() // skip mapping end token.
430435
return node, nil
431436
}
@@ -1066,6 +1071,11 @@ func (p *parser) parseFlowSequence(ctx *context) (*ast.SequenceNode, error) {
10661071
if node.End == nil {
10671072
return nil, errors.ErrSyntax("sequence end token ']' not found", node.Start)
10681073
}
1074+
1075+
// set line comment if exists. e.g.) ] # comment
1076+
if err := setLineComment(ctx, node, ctx.currentToken()); err != nil {
1077+
return nil, err
1078+
}
10691079
ctx.goNext() // skip sequence end token.
10701080
return node, nil
10711081
}

parser/parser_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,22 @@ elem1:
15701570
elem1:
15711571
- elem2: # comment
15721572
[a, b, c, d]
1573+
`,
1574+
},
1575+
{
1576+
name: "flow map with inline value comment",
1577+
yaml: `
1578+
a:
1579+
b: {} # comment
1580+
c: d
1581+
`,
1582+
},
1583+
{
1584+
name: "flow array with inline value comment",
1585+
yaml: `
1586+
a:
1587+
b: [] # comment
1588+
c: d
15731589
`,
15741590
},
15751591
{

0 commit comments

Comments
 (0)