mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
Handle durations in Unmarshaling
This commit is contained in:
parent
c975dc1b4e
commit
e2a9ac4231
2 changed files with 30 additions and 6 deletions
25
viper.go
25
viper.go
|
@ -605,15 +605,35 @@ func (v *Viper) GetSizeInBytes(key string) uint {
|
||||||
// Takes a single key and unmarshals it into a Struct
|
// Takes a single key and unmarshals it into a Struct
|
||||||
func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
|
func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
|
||||||
func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
|
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshals the config into a Struct. Make sure that the tags
|
// Unmarshals the config into a Struct. Make sure that the tags
|
||||||
// on the fields of the structure are properly set.
|
// on the fields of the structure are properly set.
|
||||||
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
|
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
|
||||||
func (v *Viper) Unmarshal(rawVal interface{}) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -631,6 +651,7 @@ func weakDecodeExact(input, output interface{}) error {
|
||||||
Metadata: nil,
|
Metadata: nil,
|
||||||
Result: output,
|
Result: output,
|
||||||
WeaklyTypedInput: true,
|
WeaklyTypedInput: true,
|
||||||
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder, err := mapstructure.NewDecoder(config)
|
decoder, err := mapstructure.NewDecoder(config)
|
||||||
|
|
|
@ -453,10 +453,12 @@ func TestRecursiveAliases(t *testing.T) {
|
||||||
func TestUnmarshal(t *testing.T) {
|
func TestUnmarshal(t *testing.T) {
|
||||||
SetDefault("port", 1313)
|
SetDefault("port", 1313)
|
||||||
Set("name", "Steve")
|
Set("name", "Steve")
|
||||||
|
Set("duration", "10s")
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
Port int
|
Port int
|
||||||
Name string
|
Name string
|
||||||
|
Duration time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
var C config
|
var C config
|
||||||
|
@ -466,14 +468,15 @@ func TestUnmarshal(t *testing.T) {
|
||||||
t.Fatalf("unable to decode into struct, %v", err)
|
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("port", 1234)
|
||||||
|
Set("duration", "20m")
|
||||||
err = Unmarshal(&C)
|
err = Unmarshal(&C)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to decode into struct, %v", err)
|
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) {
|
func TestBindPFlags(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue