mirror of
https://github.com/jinzhu/configor.git
synced 2020-06-03 17:06:22 +00:00
f78b6dc2e1
* Also improve the tests for ErrorOnUnmatchedKeys with toml and yaml files
29 lines
637 B
Go
29 lines
637 B
Go
// +build go1.10
|
|
|
|
package configor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
// unmarshalJSON unmarshals the given data into the config interface.
|
|
// If the errorOnUnmatchedKeys boolean is true, an error will be returned if there
|
|
// are keys in the data that do not match fields in the config interface.
|
|
func unmarshalJSON(data []byte, config interface{}, errorOnUnmatchedKeys bool) error {
|
|
reader := strings.NewReader(string(data))
|
|
decoder := json.NewDecoder(reader)
|
|
|
|
if errorOnUnmatchedKeys {
|
|
decoder.DisallowUnknownFields()
|
|
}
|
|
|
|
err := decoder.Decode(config)
|
|
if err != nil && err != io.EOF {
|
|
return err
|
|
}
|
|
return nil
|
|
|
|
}
|