Skip to content

Commit 7fd2a2b

Browse files
committed
fix
1 parent edb6149 commit 7fd2a2b

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

decode.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,14 @@ func (md *MetaData) unifyMap(mapping interface{}, rv reflect.Value) error {
264264
if !ok {
265265
return badtype("map", mapping)
266266
}
267-
if rv.IsNil() {
267+
isNil := rv.IsNil()
268+
if isNil {
268269
rv.Set(reflect.MakeMap(rv.Type()))
269270
}
270271
for k, v := range tmap {
271272
md.decoded[md.context.add(k).String()] = true
272273
md.context = append(md.context, k)
273-
274274
rvkey := indirect(reflect.New(rv.Type().Key()))
275-
rvval := reflect.Indirect(reflect.New(rv.Type().Elem()))
276-
if err := md.unify(v, rvval); err != nil {
277-
return err
278-
}
279-
md.context = md.context[0 : len(md.context)-1]
280275
keyType := rvkey.Kind()
281276
if keyType >= reflect.Int && keyType <= reflect.Uint64 {
282277
i, err := strconv.ParseInt(k, 10, 64)
@@ -295,6 +290,28 @@ func (md *MetaData) unifyMap(mapping interface{}, rv reflect.Value) error {
295290
keyType, mapping)
296291
}
297292
}
293+
var rvval reflect.Value
294+
destType := rv.Type().Elem()
295+
if !isNil {
296+
prval := rv.MapIndex(rvkey)
297+
if prval.IsValid() {
298+
if prval.Kind() == reflect.Interface {
299+
prval = reflect.ValueOf(prval.Interface())
300+
}
301+
destType = prval.Type()
302+
if destType.Kind() == reflect.Map {
303+
rvval = prval
304+
}
305+
//fmt.Printf("[confl] %v(%v) =====> %v\n", k, rv.Type().Key().Kind(), destType.Kind())
306+
}
307+
}
308+
if rvval.Kind() == reflect.Invalid {
309+
rvval = reflect.Indirect(reflect.New(destType))
310+
}
311+
if err := md.unify(v, rvval); err != nil {
312+
return err
313+
}
314+
md.context = md.context[0 : len(md.context)-1]
298315
rv.SetMapIndex(rvkey, rvval)
299316
}
300317
return nil

decode_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package confl
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"log"
76
"reflect"
@@ -454,8 +453,9 @@ func TestDecodeMap(t *testing.T) {
454453
}
455454
var tab table
456455
tab.H = map[string]interface{}{
457-
`table`: &Table2{Name: `0`},
458-
`list`: []string{`1`, `2`},
456+
`table`: &Table2{Name: `0`},
457+
`table2`: Table2{Name: `00`},
458+
`list`: []string{`1`, `2`},
459459
`mapInMap`: map[string]interface{}{
460460
`inString`: `v`,
461461
`inList`: []string{`1`, `2`},
@@ -465,6 +465,9 @@ func TestDecodeMap(t *testing.T) {
465465
table : {
466466
Name : "1"
467467
}
468+
table2 : {
469+
Name : "10"
470+
}
468471
list : ["11","12"]
469472
mapInMap : {
470473
"inString" : "y"
@@ -474,19 +477,18 @@ func TestDecodeMap(t *testing.T) {
474477
if _, err := Decode(str, &tab); err != nil {
475478
t.Fatal(err)
476479
}
477-
expected, _ := json.Marshal(table{
480+
expected := table{
478481
H: map[string]interface{}{
479-
`table`: &Table2{Name: `1`},
480-
`list`: []string{`11`, `12`},
482+
`table`: &Table2{Name: `1`},
483+
`table2`: Table2{Name: `10`},
484+
`list`: []string{`11`, `12`},
481485
`mapInMap`: map[string]interface{}{
482486
`inString`: `y`,
483487
`inList`: []string{`21`, `22`},
484488
},
485489
},
486-
})
487-
actual, _ := json.Marshal(tab)
488-
println(string(actual))
489-
assert.Equal(t, string(expected), string(actual))
490+
}
491+
assert.Equal(t, expected, tab)
490492
}
491493

492494
func TestDecodeSizedInts(t *testing.T) {

doc.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@ Package confl provides facilities for decoding and encoding TOML/NGINX configura
33
files via reflection.
44
*/
55
package confl
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
)
11+
12+
func printJSON(v interface{}) {
13+
b, _ := json.MarshalIndent(v, ``, ` `)
14+
fmt.Println(string(b))
15+
}

0 commit comments

Comments
 (0)