Skip to content

Commit

Permalink
fixed parsing array that contains substitution and concatenation, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gurkankaymak committed Dec 20, 2022
1 parent c4959a7 commit 896f9c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
17 changes: 8 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,18 +582,19 @@ func (p *parser) extractArray() (Array, error) {
return nil, err
}

//array = append(array, value)
token = p.scanner.TokenText()

if p.scanner.Line == lastRow && token != commaToken && token != arrayEndToken {
if isUnquotedString(token) {
concatenatedValue, err := p.checkConcatenation(value)
if err != nil {
return nil, err
}
concatenatedValue, err := p.checkConcatenation(value)
if err != nil {
return nil, err
}
if concatenatedValue == nil {
return nil, missingCommaError(p.scanner.Line, p.scanner.Column)
} else {
lastValue := concatenatedValue
token = p.scanner.TokenText()
for concatenatedValue != nil && isUnquotedString(token) && token != commaToken && token != arrayEndToken {
for concatenatedValue != nil && token != commaToken && token != arrayEndToken {
concatenatedValue, err = p.checkConcatenation(lastValue)
if err != nil {
return nil, err
Expand All @@ -606,8 +607,6 @@ func (p *parser) extractArray() (Array, error) {
token = p.scanner.TokenText()
}
array = append(array, lastValue)
} else {
return nil, missingCommaError(p.scanner.Line, p.scanner.Column)
}
} else {
array = append(array, value)
Expand Down
19 changes: 19 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,25 @@ func TestExtractObject(t *testing.T) {
assertNoError(t, err)
assertDeepEqual(t, got, Object{"uuid": concatenation{String("123e4567"), String(""), String("-e89b-12d3-a456-426614174000")}})
})

t.Run("extract the object that contains an array with substitution and concatenation", func(t *testing.T) {
parser := newParser(strings.NewReader(`{x:a, y:b, arr: [${x}"."${y}]}`))
parser.advance()
got, err := parser.extractObject()
expected := Object{
"x": String("a"),
"y": String("b"),
"arr": Array{concatenation{
&Substitution{path: "x", optional: false},
String(""),
String("."),
String(""),
&Substitution{path: "y", optional: false},
}},
}
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
}

func TestMergeObjects(t *testing.T) {
Expand Down

0 comments on commit 896f9c2

Please sign in to comment.