Skip to content

Commit

Permalink
Fix sequence with comment
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Sep 15, 2023
1 parent 1c0fdf0 commit 66eae87
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
5 changes: 2 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 66eae87

Please sign in to comment.