Don't allow tables to be redefined
diff --git a/parser.go b/parser.go index 54750c3..f91e80c 100644 --- a/parser.go +++ b/parser.go
@@ -87,6 +87,9 @@ if key.typ != tokenKeyGroup { panic(fmt.Sprintf("unexpected token %s, was expecting a key group", key)) } + if p.tree.Has(key.val) { + panic("duplicated tables") + } p.tree.createSubTree(key.val) p.assume(tokenRightBracket) p.currentGroup = key.val
diff --git a/parser_test.go b/parser_test.go index 1f55faa..cda0322 100644 --- a/parser_test.go +++ b/parser_test.go
@@ -196,6 +196,13 @@ }) } +func TestDuplicateGroups(t *testing.T) { + _, err := Load("[foo]\na=2\n[foo]b=3") + if err.Error() != "duplicated tables" { + t.Error("Bad error message:", err.Error()) + } +} + func TestMissingFile(t *testing.T) { _, err := LoadFile("foo.toml") if err.Error() != "open foo.toml: no such file or directory" {
diff --git a/toml.go b/toml.go index 410bab6..bfd12b2 100644 --- a/toml.go +++ b/toml.go
@@ -15,6 +15,18 @@ // This is the result of the parsing of a TOML file. type TomlTree map[string]interface{} +// Has returns a boolean indicating if the toplevel tree contains the given +// key. +func (t *TomlTree) Has(key string) bool { + mp := (map[string]interface{})(*t) + for k, _ := range mp { + if k == key { + return true + } + } + return false +} + // Keys returns the keys of the toplevel tree. // Warning: this is a costly operation. func (t *TomlTree) Keys() []string {