mirror of
https://github.com/spf13/viper
synced 2025-05-06 04:07:17 +00:00
Warn when a yaml file has duplicated keys
Try using `UnmarshalStrict`, if it fails, print the errors as a warning and try again with plain `Unmarshal`. Errors look this way: ``` 2019/03/26 15:11:33 warning reading config file: yaml: unmarshal errors: line 6: key "api_key" already set in map ```
This commit is contained in:
parent
10edc96e10
commit
42a499d7ac
1 changed files with 6 additions and 2 deletions
4
viper.go
4
viper.go
|
@ -1462,9 +1462,13 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
|||
|
||||
switch strings.ToLower(v.getConfigType()) {
|
||||
case "yaml", "yml":
|
||||
// Try UnmarshalStrict first, so we can warn about duplicated keys
|
||||
if strictErr := yaml.UnmarshalStrict(buf.Bytes(), &c); strictErr != nil {
|
||||
if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil {
|
||||
return ConfigParseError{err}
|
||||
}
|
||||
log.Printf("warning reading config file: %v\n", strictErr)
|
||||
}
|
||||
|
||||
case "json":
|
||||
if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue