From c4959a755c4e2a9ad77484aab74c5cc36f3ed3a7 Mon Sep 17 00:00:00 2001 From: gurkankaymak Date: Mon, 19 Dec 2022 20:40:09 +0300 Subject: [PATCH] fixed parsing unquoted strings which Scanner library recognizes as float (like 123e4567), fixes #32 --- parser.go | 7 ++++++- parser_test.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/parser.go b/parser.go index c0b7e5c..8bc00dc 100644 --- a/parser.go +++ b/parser.go @@ -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() diff --git a/parser_test.go b/parser_test.go index 979f2d4..9e91e90 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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) { @@ -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