Skip to content

Commit ad0b154

Browse files
committed
Decode(map[string]interface{})
1 parent 497d0af commit ad0b154

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

decode.go

+41-5
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,49 @@ import (
8989
// Field map[string]string `properties:"myName"`
9090
func (p *Properties) Decode(x interface{}) error {
9191
t, v := reflect.TypeOf(x), reflect.ValueOf(x)
92-
if t.Kind() != reflect.Ptr || v.Elem().Type().Kind() != reflect.Struct {
93-
return fmt.Errorf("not a pointer to struct: %s", t)
92+
93+
if isPtr(t) {
94+
elemT := v.Elem().Type()
95+
switch {
96+
case isMap(elemT):
97+
m, ok := x.(*map[string]interface{})
98+
if !ok {
99+
return fmt.Errorf("map type not map[string]interface {}", elemT)
100+
}
101+
for _, key := range p.Keys() {
102+
value, _ := p.Get(key)
103+
setNestedKey(*m, key, value)
104+
}
105+
return nil
106+
107+
case isStruct(elemT):
108+
if err := dec(p, "", nil, nil, v); err != nil {
109+
return err
110+
}
111+
return nil
112+
}
94113
}
95-
if err := dec(p, "", nil, nil, v); err != nil {
96-
return err
114+
115+
return fmt.Errorf("not a pointer to map or struct: %s", t)
116+
}
117+
118+
func setNestedKey(m map[string]interface{}, key string, value interface{}) {
119+
if strings.Contains(key, ".") {
120+
kk := strings.SplitN(key, ".", 2)
121+
prefix, rest := kk[0], kk[1]
122+
if _, ok := m[prefix]; !ok {
123+
m[prefix] = map[string]interface{}{}
124+
}
125+
setNestedKey(m[prefix].(map[string]interface{}), rest, value)
126+
} else {
127+
if reflect.TypeOf(value).Kind() == reflect.String {
128+
val := value.(string)
129+
if strings.Contains(val, ";") {
130+
value = split(val, ";")
131+
}
132+
}
133+
m[key] = value
97134
}
98-
return nil
99135
}
100136

101137
func dec(p *Properties, key string, def *string, opts map[string]string, v reflect.Value) error {

decode_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,31 @@ func TestDecodeMap(t *testing.T) {
273273
testDecode(t, in, &X{}, out)
274274
}
275275

276+
func TestDecodeToStringMap(t *testing.T) {
277+
type S struct {
278+
A string
279+
}
280+
X := make(map[string]interface{})
281+
in := `
282+
A.foo=bar
283+
A.bar=bang
284+
B.foo=a;b;c
285+
B.bar=1;2;3
286+
C.foo.one=1
287+
C.foo.two=2
288+
C.bar.three=3
289+
C.bar.four=4
290+
D.foo.a=bar
291+
`
292+
out := &map[string]interface{}{
293+
"A": map[string]interface{}{"foo": "bar", "bar": "bang"},
294+
"B": map[string]interface{}{"foo": []string{"a", "b", "c"}, "bar": []string{"1", "2", "3"}},
295+
"C": map[string]interface{}{"foo": map[string]interface{}{"one": "1", "two": "2"}, "bar": map[string]interface{}{"three": "3", "four": "4"}},
296+
"D": map[string]interface{}{"foo": map[string]interface{}{"a": "bar"}},
297+
}
298+
testDecode(t, in, &X, out)
299+
}
300+
276301
func testDecode(t *testing.T, in string, v, out interface{}) {
277302
p, err := parse(in)
278303
if err != nil {

0 commit comments

Comments
 (0)