Skip to content

Commit

Permalink
fx: Treat empty nodes as zero values
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemassa committed Dec 29, 2023
1 parent 0640a15 commit 28df8de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
4 changes: 4 additions & 0 deletions decode_test.go
Expand Up @@ -591,6 +591,10 @@ func TestDecoder(t *testing.T) {
"v:\n - A\n - B",
map[string]interface{}{"v": []interface{}{"A", "B"}},
},
{
"v:\n-",
map[string][]string{"v": []string{""}},
},
{
"v:\n - A\n - B\n - C",
map[string][]string{"v": []string{"A", "B", "C"}},
Expand Down
27 changes: 15 additions & 12 deletions parser/parser.go
Expand Up @@ -327,20 +327,23 @@ func (p *parser) parseSequenceEntry(ctx *context) (*ast.SequenceNode, error) {
for tk.Type == token.SequenceEntryType {
ctx.progress(1) // skip sequence token
tk = ctx.currentToken()
if tk == nil {
return nil, errors.ErrSyntax("empty sequence entry", ctx.previousToken())
}
var comment *ast.CommentGroupNode
if tk.Type == token.CommentType {
comment = p.parseCommentOnly(ctx)
tk = ctx.currentToken()
if tk.Type == token.SequenceEntryType {
ctx.progress(1) // skip sequence token
var value ast.Node
var err error
if tk == nil {
value = &ast.NullNode{}
} else {
if tk.Type == token.CommentType {
comment = p.parseCommentOnly(ctx)
tk = ctx.currentToken()
if tk.Type == token.SequenceEntryType {
ctx.progress(1) // skip sequence token
}
}
value, err = p.parseToken(ctx.withIndex(uint(len(sequenceNode.Values))), ctx.currentToken())
if err != nil {
return nil, errors.Wrapf(err, "failed to parse sequence")
}
}
value, err := p.parseToken(ctx.withIndex(uint(len(sequenceNode.Values))), ctx.currentToken())
if err != nil {
return nil, errors.Wrapf(err, "failed to parse sequence")
}
if comment != nil {
comment.SetPath(ctx.withIndex(uint(len(sequenceNode.Values))).path)
Expand Down
13 changes: 0 additions & 13 deletions parser/parser_test.go
Expand Up @@ -679,19 +679,6 @@ a
[1:1] unterminated flow mapping
> 1 | { "key": "value"
^
`,
},
{
`
a:
- b: c
- `,
`
[4:1] empty sequence entry
2 | a:
3 | - b: c
> 4 | -
^
`,
},
}
Expand Down

0 comments on commit 28df8de

Please sign in to comment.