From 30854544771ad0be7415f0035e1fc03ac722dd02 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Tue, 10 Dec 2013 17:50:59 +0100 Subject: [PATCH] Don't allow duplicate keys --- parser.go | 3 +++ parser_test.go | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/parser.go b/parser.go index ee5758a..ba78f3d 100644 --- a/parser.go +++ b/parser.go @@ -104,6 +104,9 @@ func parseAssign(p *parser) parserStateFn { if p.currentGroup != "" { final_key = p.currentGroup + "." + key.val } + if p.tree.Get(final_key) != nil { + panic(fmt.Sprintf("the following key was defined twice: %s", final_key)) + } p.tree.Set(final_key, value) return parseStart(p) } diff --git a/parser_test.go b/parser_test.go index 9108bab..66e7eaa 100644 --- a/parser_test.go +++ b/parser_test.go @@ -149,6 +149,7 @@ func TestNestedEmptyArrays(t *testing.T) { }) } + func TestArrayMixedTypes(t *testing.T) { _, err := Load("a = [42, 16.0]") if err.Error() != "mixed types in array" { @@ -210,6 +211,13 @@ func TestDuplicateGroups(t *testing.T) { } } +func TestDuplicateKeys(t *testing.T) { + _, err := Load("foo = 2\nfoo = 3") + if err.Error() != "the following key was defined twice: foo" { + t.Error("Bad error message:", err.Error()) + } +} + func TestEmptyIntermediateTable(t *testing.T) { _, err := Load("[foo..bar]") if err.Error() != "empty intermediate table" {