Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StringMap and StringsMap #139

Merged
merged 1 commit into from Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 51 additions & 24 deletions getters.go
Expand Up @@ -443,18 +443,22 @@ func (ko *Koanf) StringMap(path string) map[string]string {
return out
}

mp, ok := o.(map[string]interface{})
if !ok {
return out
}
out = make(map[string]string, len(mp))
for k, v := range mp {
switch s := v.(type) {
case string:
out[k] = s
default:
// There's a non string type. Return.
return map[string]string{}
switch mp := o.(type) {
case map[string]string:
out = make(map[string]string, len(mp))
for k, v := range mp {
out[k] = v
}
case map[string]interface{}:
out = make(map[string]string, len(mp))
for k, v := range mp {
switch s := v.(type) {
case string:
out[k] = s
default:
// There's a non string type. Return.
return map[string]string{}
}
}
}

Expand Down Expand Up @@ -483,25 +487,48 @@ func (ko *Koanf) StringsMap(path string) map[string][]string {
return out
}

mp, ok := o.(map[string]interface{})
if !ok {
return out
}
out = make(map[string][]string, len(mp))
for k, v := range mp {
switch s := v.(type) {
case []interface{}:
for _, v := range s {
switch mp := o.(type) {
case map[string][]string:
out = make(map[string][]string, len(mp))
for k, v := range mp {
out[k] = make([]string, 0, len(v))
for _, s := range v {
out[k] = append(out[k], s)
}
}
case map[string][]interface{}:
out = make(map[string][]string, len(mp))
for k, v := range mp {
for _, v := range v {
switch sv := v.(type) {
case string:
out[k] = append(out[k], sv)
default:
return map[string][]string{}
}
}
default:
// There's a non []interface type. Return.
return map[string][]string{}
}
case map[string]interface{}:
out = make(map[string][]string, len(mp))
for k, v := range mp {
switch s := v.(type) {
case []string:
for _, v := range s {
out[k] = append(out[k], v)
}
case []interface{}:
for _, v := range s {
switch sv := v.(type) {
case string:
out[k] = append(out[k], sv)
default:
return map[string][]string{}
}
}
default:
// There's a non []interface type. Return.
return map[string][]string{}
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions koanf_test.go
Expand Up @@ -1188,3 +1188,36 @@ func TestDelete(t *testing.T) {
assert.Equal(false, c.koanf.Exists("empty"))
}
}

func TestGetStringsMap(t *testing.T) {
assert := assert.New(t)

k := koanf.New(delim)

k.Load(confmap.Provider(map[string]interface{}{
"str": map[string]string{
"k1": "value",
},
"strs": map[string][]string{
"k1": {"value"},
},
"iface": map[string]interface{}{
"k2": "value",
},
"ifaces": map[string][]interface{}{
"k2": {"value"},
},
"ifaces2": map[string]interface{}{
"k2": []interface{}{"value"},
},
"ifaces3": map[string]interface{}{
"k2": []string{"value"},
},
}, "."), nil)
assert.Equal(map[string]string{"k1": "value"}, k.StringMap("str"), "types don't match")
assert.Equal(map[string]string{"k2": "value"}, k.StringMap("iface"), "types don't match")
assert.Equal(map[string][]string{"k1": {"value"}}, k.StringsMap("strs"), "types don't match")
assert.Equal(map[string][]string{"k2": {"value"}}, k.StringsMap("ifaces"), "types don't match")
assert.Equal(map[string][]string{"k2": {"value"}}, k.StringsMap("ifaces2"), "types don't match")
assert.Equal(map[string][]string{"k2": {"value"}}, k.StringsMap("ifaces3"), "types don't match")
}