Repository: goccy/go-yaml
Pull request #390 (2023-09-15 03:08:06)

Fix sequence with comment
fix https://github.com/goccy/go-yaml/issues/389


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/goccy/go-yaml/pull/390?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
> Merging [#390](https://app.codecov.io/gh/goccy/go-yaml/pull/390?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) (66eae87) into [master](https://app.codecov.io/gh/goccy/go-yaml/commit/1c0fdf02087a5a34a9d21f6f94184541fbeb1f17?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) (1c0fdf0) will **increase** coverage by `0.09%`.
> Report is 1 commits behind head on master.
> The diff coverage is `100.00%`.

: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     #390      +/-   ##
==========================================
+ Coverage   75.92%   76.02%   +0.09%     
==========================================
  Files          13       13              
  Lines        4678     4692      +14     
==========================================
+ Hits         3552     3567      +15     
+ Misses        867      866       -1     
  Partials      259      259              
```

</details>

Patch:
diff --git a/parser/parser.go b/parser/parser.go
index 6989a37..13ada50 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -334,10 +334,9 @@ func (p *parser) parseSequenceEntry(ctx *context) (*ast.SequenceNode, error) {
 		if tk.Type == token.CommentType {
 			comment = p.parseCommentOnly(ctx)
 			tk = ctx.currentToken()
-			if tk.Type != token.SequenceEntryType {
-				break
+			if tk.Type == token.SequenceEntryType {
+				ctx.progress(1) // skip sequence token
 			}
-			ctx.progress(1) // skip sequence token
 		}
 		value, err := p.parseToken(ctx.withIndex(uint(len(sequenceNode.Values))), ctx.currentToken())
 		if err != nil {


Test patch:
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 13157c0..8d697e8 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -7,6 +7,7 @@ import (
 	"strings"
 	"testing"
 
+	"github.com/goccy/go-yaml"
 	"github.com/goccy/go-yaml/ast"
 	"github.com/goccy/go-yaml/lexer"
 	"github.com/goccy/go-yaml/parser"
@@ -873,7 +874,58 @@ baz: 1`
 			t.Fatal("failed to parse comment")
 		}
 	})
+}
 
+func TestSequenceComment(t *testing.T) {
+	content := `
+foo:
+  - # comment
+    bar: 1
+baz:
+  - xxx
+`
+	f, err := parser.ParseBytes([]byte(content), parser.ParseComments)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if len(f.Docs) != 1 {
+		t.Fatal("failed to parse content with next line with sequence")
+	}
+	expected := `
+foo:
+  # comment
+  - bar: 1
+baz:
+  - xxx`
+	if f.Docs[0].String() != strings.TrimPrefix(expected, "\n") {
+		t.Fatal("failed to parse comment")
+	}
+	t.Run("foo[0].bar", func(t *testing.T) {
+		path, err := yaml.PathString("$.foo[0].bar")
+		if err != nil {
+			t.Fatal(err)
+		}
+		v, err := path.FilterFile(f)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if v.String() != "1" {
+			t.Fatal("failed to get foo[0].bar value")
+		}
+	})
+	t.Run("baz[0]", func(t *testing.T) {
+		path, err := yaml.PathString("$.baz[0]")
+		if err != nil {
+			t.Fatal(err)
+		}
+		v, err := path.FilterFile(f)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if v.String() != "xxx" {
+			t.Fatal("failed to get baz[0] value")
+		}
+	})
 }
 
 func TestNodePath(t *testing.T) {