Skip to content

Commit

Permalink
handle the case when path expression in key ends with a number (golan…
Browse files Browse the repository at this point in the history
…g Scanner does not separate the dot from number, handled this case manually) fixes #45
  • Loading branch information
gurkankaymak committed Oct 19, 2023
1 parent 9ccc664 commit cd91c92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ func (p *parser) extractObject(isSubObject ...bool) (Object, error) {
}

key := strings.Trim(p.scanner.TokenText(), `"`)
if strings.HasPrefix(key, dotToken) && key != dotToken {
key = strings.TrimPrefix(key, dotToken)
}

if forbiddenCharacters[key] {
return nil, invalidKeyError(key, p.scanner.Line, p.scanner.Column)
}
Expand All @@ -289,11 +293,13 @@ func (p *parser) extractObject(isSubObject ...bool) (Object, error) {
p.advance()
text := p.scanner.TokenText()

if text == dotToken || text == objectStartToken {
startsWithDot := strings.HasPrefix(text, dotToken) && text != dotToken

if text == dotToken || text == objectStartToken || startsWithDot {
if text == dotToken {
p.advance() // skip "."

if p.scanner.TokenText() == dotToken {
if p.scanner.TokenText() == dotToken || strings.HasPrefix(p.scanner.TokenText(), dotToken) {
return nil, adjacentPeriodsError(p.scanner.Line, p.scanner.Column)
}

Expand Down
8 changes: 8 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,14 @@ func TestExtractObject(t *testing.T) {
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})

t.Run("should parse properly when the path expression key ends with a number", func(t *testing.T) {
parser := newParser(strings.NewReader(`a.100:[1,2]`))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"a": Object{"100": Array{Int(1), Int(2)}}})
})
}

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

0 comments on commit cd91c92

Please sign in to comment.