From d921461e3a5072c9139679fd98164d6f8fd74323 Mon Sep 17 00:00:00 2001 From: gurkankaymak Date: Mon, 5 Dec 2022 11:37:04 +0300 Subject: [PATCH] handle multiple include statements, fixes #30 --- parser.go | 2 +- parser_test.go | 13 +++++++++++++ testdata/b.conf | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 testdata/b.conf diff --git a/parser.go b/parser.go index 33e94d5..7187e65 100644 --- a/parser.go +++ b/parser.go @@ -241,7 +241,7 @@ func (p *parser) extractObject(isSubObject ...bool) (Object, error) { p.consumeComment() } - if p.scanner.TokenText() == includeToken { + for p.scanner.TokenText() == includeToken { p.advance() includedObject, err := p.parseIncludedResource() diff --git a/parser_test.go b/parser_test.go index 1a775c0..55d0134 100644 --- a/parser_test.go +++ b/parser_test.go @@ -202,6 +202,19 @@ func TestExtractObject(t *testing.T) { assertDeepEqual(t, got, expected) }) + t.Run("merge multiple included objects with the existing", func(t *testing.T) { + parser := newParser(strings.NewReader( + `c:3 + include "testdata/a.conf" + include "testdata/b.conf" + `)) + parser.advance() + expected := Object{"a": Int(1), "b": Int(2), "c": Int(3)} + got, err := parser.extractObject() + assertNoError(t, err) + assertDeepEqual(t, got, expected) + }) + t.Run("parse correctly if the last line is a comment", func(t *testing.T) { config := `{ a: 1 diff --git a/testdata/b.conf b/testdata/b.conf new file mode 100644 index 0000000..1b69e58 --- /dev/null +++ b/testdata/b.conf @@ -0,0 +1 @@ +b:2 \ No newline at end of file