@@ -89,13 +89,49 @@ import (
89
89
// Field map[string]string `properties:"myName"`
90
90
func (p * Properties ) Decode (x interface {}) error {
91
91
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
+ }
94
113
}
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
97
134
}
98
- return nil
99
135
}
100
136
101
137
func dec (p * Properties , key string , def * string , opts map [string ]string , v reflect.Value ) error {
0 commit comments