From ad601fc2af54d7b02a0525e759d4773c5838f450 Mon Sep 17 00:00:00 2001 From: swxctx Date: Thu, 20 Dec 2018 20:28:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8F=8D=E5=B0=84=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sub.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 sub.go diff --git a/sub.go b/sub.go new file mode 100644 index 0000000..9dd283f --- /dev/null +++ b/sub.go @@ -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) +}