Repository: goccy/go-yaml
Pull request #645 (2025-02-09 06:04:14)

Fix format with comment
SSIA

codecov-commenter: :warning: Please install the !['codecov app svg image'](https://github.com/codecov/engineering-team/assets/152432831/e90313f4-9d3a-4b63-8b54-cfe14e7ec20d) to ensure uploads and comments are reliably processed by Codecov.

## [Codecov](https://app.codecov.io/gh/goccy/go-yaml/pull/645?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 77.29%. Comparing base [(`0653c80`)](https://app.codecov.io/gh/goccy/go-yaml/commit/0653c8084372eacf15d2985c06d15554b7038776?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) to head [(`ce87ce6`)](https://app.codecov.io/gh/goccy/go-yaml/commit/ce87ce6f0130683618d5d7184bb94b46976e38cf?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima).

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master     #645   +/-   ##
=======================================
  Coverage   77.29%   77.29%           
=======================================
  Files          22       22           
  Lines        7888     7888           
=======================================
  Hits         6097     6097           
  Misses       1372     1372           
  Partials      419      419           
```

</details>

Patch:
diff --git a/internal/format/format.go b/internal/format/format.go
index a52ca8f..1137a51 100644
--- a/internal/format/format.go
+++ b/internal/format/format.go
@@ -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 {


Test patch:
diff --git a/decode_test.go b/decode_test.go
index 45e02b0..6790395 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -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
@@ -2807,8 +2808,9 @@ config:
 func TestDecoder_UnmarshalBytesWithSeparatedList(t *testing.T) {
 	yml := `
 a:
- - b: c
-   d: |
+ - b: c # comment
+   # comment
+   d: | # comment
      hello
 
      hello
@@ -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 {