Skip to content

Commit

Permalink
fixed parsing unquoted strings which Scanner library recognizes as fl…
Browse files Browse the repository at this point in the history
…oat (like 123e4567), fixes #32
  • Loading branch information
gurkankaymak committed Dec 19, 2022
1 parent b8644ed commit c4959a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,12 @@ func (p *parser) extractValue() (Value, error) {
case scanner.Float:
value, err := strconv.ParseFloat(token, 64)
if err != nil {
return nil, err
if isUnquotedString(token) {
p.advance()
return String(token), nil
} else {
return nil, err
}
}

durationUnit := p.extractDurationUnit()
Expand Down
16 changes: 16 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,14 @@ func TestExtractObject(t *testing.T) {
assertError(t, err, expectedError)
assertNil(t, got)
})

t.Run("extract the object with unquoted string that starts with number and contains an 'e' (which causes Scanner library to recognize it as float)", func(t *testing.T) {
parser := newParser(strings.NewReader("uuid: 123e4567-e89b-12d3-a456-426614174000"))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"uuid": concatenation{String("123e4567"), String(""), String("-e89b-12d3-a456-426614174000")}})
})
}

func TestMergeObjects(t *testing.T) {
Expand Down Expand Up @@ -1160,6 +1168,14 @@ func TestExtractValue(t *testing.T) {
assertEquals(t, got, Float64(1.5))
})

t.Run("extract the value that starts with number and contains an 'e' (which causes Scanner library to recognize it as float)", func(t *testing.T) {
parser := newParser(strings.NewReader("uuid = 123e4567-e89b-12d3-a456-426614174000"))
advanceScanner(t, parser, "123e4567")
got, err := parser.extractValue()
assertNoError(t, err)
assertEquals(t, got, String("123e4567"))
})

t.Run("extract multi-line string", func(t *testing.T) {
config := `a: """
this is a
Expand Down

0 comments on commit c4959a7

Please sign in to comment.