This commit is contained in:
Jimmi Dyson 2016-09-22 16:09:31 +00:00 committed by GitHub
commit 8e2e50bc37
2 changed files with 30 additions and 6 deletions

View file

@ -617,15 +617,35 @@ func (v *Viper) GetSizeInBytes(key string) uint {
// UnmarshalKey takes a single key and unmarshals it into a Struct.
func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
return mapstructure.Decode(v.Get(key), rawVal)
config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: rawVal,
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(v.Get(key))
}
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
// on the fields of the structure are properly set.
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
func (v *Viper) Unmarshal(rawVal interface{}) error {
err := mapstructure.WeakDecode(v.AllSettings(), rawVal)
config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: rawVal,
WeaklyTypedInput: true,
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
err = decoder.Decode(v.AllSettings())
if err != nil {
return err
}
@ -643,6 +663,7 @@ func weakDecodeExact(input, output interface{}) error {
Metadata: nil,
Result: output,
WeaklyTypedInput: true,
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
}
decoder, err := mapstructure.NewDecoder(config)

View file

@ -453,10 +453,12 @@ func TestRecursiveAliases(t *testing.T) {
func TestUnmarshal(t *testing.T) {
SetDefault("port", 1313)
Set("name", "Steve")
Set("duration", "10s")
type config struct {
Port int
Name string
Duration time.Duration
}
var C config
@ -466,14 +468,15 @@ func TestUnmarshal(t *testing.T) {
t.Fatalf("unable to decode into struct, %v", err)
}
assert.Equal(t, &C, &config{Name: "Steve", Port: 1313})
assert.Equal(t, &config{Name: "Steve", Port: 1313, Duration: 10 * time.Second}, &C)
Set("port", 1234)
Set("duration", "20m")
err = Unmarshal(&C)
if err != nil {
t.Fatalf("unable to decode into struct, %v", err)
}
assert.Equal(t, &C, &config{Name: "Steve", Port: 1234})
assert.Equal(t, &config{Name: "Steve", Port: 1234, Duration: 20 * time.Minute}, &C)
}
func TestBindPFlags(t *testing.T) {