diff --git a/README.md b/README.md index 64bf474..c5c3381 100644 --- a/README.md +++ b/README.md @@ -432,6 +432,7 @@ The following functions and methods exist: * `GetInt(key string) : int` * `GetString(key string) : string` * `GetStringMap(key string) : map[string]interface{}` + * `GetStringMapStruct(key string) : map[string]struct{}` * `GetStringMapString(key string) : map[string]string` * `GetStringSlice(key string) : []string` * `GetTime(key string) : time.Time` diff --git a/viper.go b/viper.go index ad8a037..8f49451 100644 --- a/viper.go +++ b/viper.go @@ -712,6 +712,12 @@ func (v *Viper) GetStringSlice(key string) []string { return cast.ToStringSlice(v.Get(key)) } +// GetStringMapStruct returns the value associated with the key as a slice of strings. +func GetStringMapStruct(key string) map[string]struct{} { return v.GetStringMapStruct(key) } +func (v *Viper) GetStringMapStruct(key string) map[string]struct{} { + return cast.ToStringMapStruct(v.Get(key)) +} + // GetStringMap returns the value associated with the key as a map of interfaces. func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) } func (v *Viper) GetStringMap(key string) map[string]interface{} { diff --git a/viper_test.go b/viper_test.go index c93480e..ff17504 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1076,6 +1076,10 @@ func TestMergeConfig(t *testing.T) { t.Fatalf("len(world) != 4, = %d", len(world)) } + if world := v.GetStringMapStruct("hello.world"); len(world) != 4 { + t.Fatalf("len(world) != 4, = %d, %#v", len(world), world) + } + if fu := v.GetString("fu"); fu != "" { t.Fatalf("fu != \"\", = %s", fu) }