From e0e27a665935a04d216bd5667f312bddbc8c2d64 Mon Sep 17 00:00:00 2001 From: gurkankaymak Date: Tue, 8 Nov 2022 09:50:06 +0300 Subject: [PATCH] handle the case where the comment contains "'" character, fixes #26 --- parser.go | 2 +- parser_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/parser.go b/parser.go index 0dfe03b..0e9f4c3 100644 --- a/parser.go +++ b/parser.go @@ -742,7 +742,7 @@ func (p *parser) extractSubstitution() (*Substitution, error) { } func (p *parser) consumeComment() { - for token := p.scanner.Peek(); token != '\n' && token != scanner.EOF; token = p.scanner.Peek() { + for token := p.scanner.Peek(); token != '\n' && token != scanner.EOF && !strings.HasSuffix(p.scanner.TokenText(), "\n"); token = p.scanner.Peek() { p.advance() } p.advance() diff --git a/parser_test.go b/parser_test.go index e9c87a3..1cc90b1 100644 --- a/parser_test.go +++ b/parser_test.go @@ -500,6 +500,18 @@ func TestExtractObject(t *testing.T) { assertDeepEqual(t, got, Object{"name": String("value")}) }) + t.Run("should parse properly if the comment contains a `'` character (which results golang scanner to append `\n` to the latest token instead of a separate token)", func(t *testing.T) { + config := ` + # it's a comment + name: value + ` + parser := newParser(strings.NewReader(config)) + parser.advance() + got, err := parser.extractObject() + assertNoError(t, err) + assertDeepEqual(t, got, Object{"name": String("value")}) + }) + t.Run("return missingCommaError if there is no comma or ASCII newline between the object elements", func(t *testing.T) { parser := newParser(strings.NewReader("{a:1 b:2}")) parser.advance()