Skip to content

Commit e6c495e

Browse files
authored
fix format with comment (#645)
1 parent 0653c80 commit e6c495e

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

‎decode_test.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,8 +2757,9 @@ type unmarshalList struct {
27572757

27582758
func (u *unmarshalList) UnmarshalYAML(b []byte) error {
27592759
expected := `
2760-
- b: c
2761-
d: |
2760+
- b: c # comment
2761+
# comment
2762+
d: | # comment
27622763
hello
27632764
27642765
hello
@@ -2807,8 +2808,9 @@ config:
28072808
func TestDecoder_UnmarshalBytesWithSeparatedList(t *testing.T) {
28082809
yml := `
28092810
a:
2810-
- b: c
2811-
d: |
2811+
- b: c # comment
2812+
# comment
2813+
d: | # comment
28122814
hello
28132815
28142816
hello
@@ -2818,7 +2820,8 @@ a:
28182820
var v struct {
28192821
A unmarshalList
28202822
}
2821-
if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
2823+
cm := yaml.CommentMap{}
2824+
if err := yaml.UnmarshalWithOptions([]byte(yml), &v, yaml.CommentToMap(cm)); err != nil {
28222825
t.Fatal(err)
28232826
}
28242827
if len(v.A.v) != 2 {

‎internal/format/format.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,31 @@ func (f *Formatter) formatDocument(n *ast.DocumentNode) string {
8080
}
8181

8282
func (f *Formatter) formatNull(n *ast.NullNode) string {
83-
return f.formatCommentGroup(n.Comment) + f.origin(n.Token)
83+
return f.origin(n.Token) + f.formatCommentGroup(n.Comment)
8484
}
8585

8686
func (f *Formatter) formatString(n *ast.StringNode) string {
87-
return f.formatCommentGroup(n.Comment) + f.origin(n.Token)
87+
return f.origin(n.Token) + f.formatCommentGroup(n.Comment)
8888
}
8989

9090
func (f *Formatter) formatInteger(n *ast.IntegerNode) string {
91-
return f.formatCommentGroup(n.Comment) + f.origin(n.Token)
91+
return f.origin(n.Token) + f.formatCommentGroup(n.Comment)
9292
}
9393

9494
func (f *Formatter) formatFloat(n *ast.FloatNode) string {
95-
return f.formatCommentGroup(n.Comment) + f.origin(n.Token)
95+
return f.origin(n.Token) + f.formatCommentGroup(n.Comment)
9696
}
9797

9898
func (f *Formatter) formatBool(n *ast.BoolNode) string {
99-
return f.formatCommentGroup(n.Comment) + f.origin(n.Token)
99+
return f.origin(n.Token) + f.formatCommentGroup(n.Comment)
100100
}
101101

102102
func (f *Formatter) formatInfinity(n *ast.InfinityNode) string {
103-
return f.formatCommentGroup(n.Comment) + f.origin(n.Token)
103+
return f.origin(n.Token) + f.formatCommentGroup(n.Comment)
104104
}
105105

106106
func (f *Formatter) formatNan(n *ast.NanNode) string {
107-
return f.formatCommentGroup(n.Comment) + f.origin(n.Token)
107+
return f.origin(n.Token) + f.formatCommentGroup(n.Comment)
108108
}
109109

110110
func (f *Formatter) formatLiteral(n *ast.LiteralNode) string {

0 commit comments

Comments
 (0)