Add ToStringMapStruct()

This commit is contained in:
Albert Le Batteux 2018-01-11 13:13:31 +01:00
parent aafc9e6bc7
commit 5f41252f95
No known key found for this signature in database
GPG key ID: 6ECDB1C9555BCE18
3 changed files with 11 additions and 0 deletions

View file

@ -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`

View file

@ -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{} {

View file

@ -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)
}