diff --git a/README.md b/README.md index b557355..3573357 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,9 @@ override your defaults. This is simply done by: itkconfig.LoadConfig("filename.conf", cfg) ``` +Non-slice keys can only be defined once per config file. Multiple +definitions will produce an error. + ## Authors * Trygve Aaberge ([trygveaa@samfundet.no](mailto:trygveaa@samfundet.no)) diff --git a/config.go b/config.go index aef7207..0af5421 100644 --- a/config.go +++ b/config.go @@ -165,6 +165,10 @@ func LoadConfig(filename string, config interface{}) error { field.Set(reflect.Append(field, v)) default: + if lastUpdate[*key] != 0 { + return syntaxError(fmt.Sprintf("key '%s' was defined multiple times, initially on line %d (did you mean to define a slice?)", *key, lastUpdate[*key])) + } + v, err := parseField(*key, *value, field.Type()) if err != nil { return syntaxError(err.Error()) diff --git a/config_test.go b/config_test.go index 2005984..d9ecc48 100644 --- a/config_test.go +++ b/config_test.go @@ -462,3 +462,17 @@ Could not parse config with multiple quotes correctly. got: %#v`, want, config) } } + +func TestMultipleDefinitions(t *testing.T) { + type Config struct { + Foo string + } + + config := Config{ + Foo: "", + } + err := LoadConfig("test_configs/multipledefinitions.cfg", &config) + if err == nil { + t.Fatalf("Should not be allowed to redefine a key.") + } +} diff --git a/test_configs/multipledefinitions.cfg b/test_configs/multipledefinitions.cfg new file mode 100644 index 0000000..a43f4f2 --- /dev/null +++ b/test_configs/multipledefinitions.cfg @@ -0,0 +1,2 @@ +Foo = "bar" +Foo = "baz" \ No newline at end of file