Skip to content

Commit

Permalink
fix format with comment (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored Feb 9, 2025
1 parent 0653c80 commit e6c495e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
13 changes: 8 additions & 5 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2757,8 +2757,9 @@ type unmarshalList struct {

func (u *unmarshalList) UnmarshalYAML(b []byte) error {
expected := `
- b: c
d: |
- b: c # comment
# comment
d: | # comment
hello
hello
Expand Down Expand Up @@ -2807,8 +2808,9 @@ config:
func TestDecoder_UnmarshalBytesWithSeparatedList(t *testing.T) {
yml := `
a:
- b: c
d: |
- b: c # comment
# comment
d: | # comment
hello
hello
Expand All @@ -2818,7 +2820,8 @@ a:
var v struct {
A unmarshalList
}
if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
cm := yaml.CommentMap{}
if err := yaml.UnmarshalWithOptions([]byte(yml), &v, yaml.CommentToMap(cm)); err != nil {
t.Fatal(err)
}
if len(v.A.v) != 2 {
Expand Down
14 changes: 7 additions & 7 deletions internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,31 @@ func (f *Formatter) formatDocument(n *ast.DocumentNode) string {
}

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

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

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

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

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

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

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

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

0 comments on commit e6c495e

Please sign in to comment.