-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore.go
98 lines (85 loc) · 1.93 KB
/
core.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package goconfig
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"reflect"
)
// 通过key获取json数据的核心文件
type jsonStore struct {
node string // 当前节点的key
v []byte
m map[string]*jsonStore
s []*jsonStore // 不知道需不需要排序,暂时不排序
}
var store *jsonStore
func init() {
store = &jsonStore{}
}
func Start() (err error) {
store.v, err = ioutil.ReadFile("aaa.json")
if err != nil {
return
}
// 判断数据是slice 还是 struct
var jsonConfig interface{}
err = json.Unmarshal(store.v, &jsonConfig)
if err != nil {
return
}
// store := &jsonStore{}
jt := reflect.TypeOf(jsonConfig)
jv := reflect.ValueOf(jsonConfig)
switch jt.Kind() {
case reflect.Map:
fmt.Println(jv)
store.m = make(map[string]*jsonStore)
jr := jv.MapRange()
for jr.Next() {
// var vb []byte
// vb, err = json.Marshal(jr.Value().Interface)
// if err != nil {
// return
// }
// store.m[jr.Key().String()] = vb
}
for i := 0; i < jt.NumField(); i++ {
// store.m[]
// fmt.Println(jv.Field(i))
fmt.Println(jt.Field(i))
}
case reflect.Slice:
store.s = make([]*jsonStore, 0)
for i := 0; i < jv.Len(); i++ {
// 判断数据类型, 只有整型字符串
store.s = append(store.s,
sliceHandle(jv.Index(i).Interface()))
// fj := &jsonStore{}
}
default:
return errors.New("not support")
}
if reflect.TypeOf(jsonConfig).Kind() == reflect.Map {
}
fmt.Println(reflect.TypeOf(jsonConfig).Kind())
return
}
func sliceHandle(value interface{}) *jsonStore {
js := &jsonStore{}
st := reflect.TypeOf(value)
switch st.Kind() {
// 数组里面可以是map, slice, string, int, bool, float
case reflect.Bool:
case reflect.Map:
}
return js
}
func (js *jsonStore) mapHandle(value interface{}) {
st := reflect.TypeOf(value)
switch st.Kind() {
// 数组里面可以是map, slice, string, int, bool, float
case reflect.Bool:
case reflect.Map:
}
}