mirror of
https://github.com/spf13/viper
synced 2025-05-06 20:27:17 +00:00
Merge 1af7e37071
into 84f94806c6
This commit is contained in:
commit
cc9aa0f5b0
2 changed files with 14 additions and 4 deletions
2
viper.go
2
viper.go
|
@ -719,7 +719,7 @@ func (v *Viper) GetSizeInBytes(key string) uint {
|
||||||
// UnmarshalKey takes a single key and unmarshals it into a Struct.
|
// UnmarshalKey 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)
|
return decode(v.Get(key), defaultDecoderConfig(rawVal))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
|
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
|
||||||
|
|
|
@ -478,11 +478,17 @@ func TestUnmarshal(t *testing.T) {
|
||||||
SetDefault("port", 1313)
|
SetDefault("port", 1313)
|
||||||
Set("name", "Steve")
|
Set("name", "Steve")
|
||||||
Set("duration", "1s1ms")
|
Set("duration", "1s1ms")
|
||||||
|
Set("sub.duration", "10m")
|
||||||
|
|
||||||
|
type subConfig struct {
|
||||||
|
Duration time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
Port int
|
Port int
|
||||||
Name string
|
Name string
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
|
Sub *subConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
var C config
|
var C config
|
||||||
|
@ -492,14 +498,18 @@ 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, &config{Name: "Steve", Port: 1313, Duration: time.Second + time.Millisecond}, &C)
|
assert.Equal(t, &config{Name: "Steve", Port: 1313, Duration: time.Second + time.Millisecond, Sub: &subConfig{Duration: time.Minute * 10}}, &C)
|
||||||
|
|
||||||
Set("port", 1234)
|
Set("port", 1234)
|
||||||
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, &config{Name: "Steve", Port: 1234, Duration: time.Second + time.Millisecond}, &C)
|
assert.Equal(t, &config{Name: "Steve", Port: 1234, Duration: time.Second + time.Millisecond, Sub: &subConfig{Duration: time.Minute * 10}}, &C)
|
||||||
|
|
||||||
|
sub := &subConfig{}
|
||||||
|
assert.NoError(t, UnmarshalKey("sub", sub))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBindPFlags(t *testing.T) {
|
func TestBindPFlags(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue