Skip to content

Commit

Permalink
增加反射读取
Browse files Browse the repository at this point in the history
  • Loading branch information
swxctx committed Dec 20, 2018
1 parent 87b224b commit ad601fc
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions sub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"reflect"
)

// get sub
func (xj *xjson) subStruct(key string, out interface{}) {
dout := out.(map[string]interface{})
m := map[int]string{}
index := 0
sk := xj.keyCase(key)
m[index] = fmt.Sprintf(Xbegin, sk)
for k, v := range dout {
orik, k := xj.keyFilter(k)
tmptype := fmt.Sprintf(Xstr, reflect.TypeOf(v))
if tmptype == Xmap {
xj.subStruct(orik, v)
tmptype = k
}
if tmptype == Xlist {
xj.subList(orik, v)
tmptype = k
}

index++
m[index] = fmt.Sprintf(Xkeyv, k, tmptype, xj.MapTag[k])
}
m[index+1] = Xend
xj.Sub = append(xj.Sub, m)
}

// get sublist
func (xj *xjson) subList(key string, out interface{}) {
dout := out.([]interface{})
list := map[string]interface{}{}
for _, vl := range dout {
vtype := fmt.Sprintf(Xstr, reflect.TypeOf(vl))
if vtype == Xlist {
xj.subList(key, vl)
continue
}

if vtype == Xmap {
m := vl.(map[string]interface{})
for k, v := range m {
_, k := xj.keyFilter(k)
if _, ok := list[k]; ok {
continue
}
list[k] = v
}
}

}
xj.subStruct(key, list)
}

0 comments on commit ad601fc

Please sign in to comment.