-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.go
More file actions
executable file
·154 lines (140 loc) · 3.5 KB
/
Copy pathstruct.go
File metadata and controls
executable file
·154 lines (140 loc) · 3.5 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package introspect
import (
"fmt"
"reflect"
)
// Struct : object struct to do introspection
type Struct struct {
Separator string
obj interface{}
data map[string]*interface{}
keys []string
}
// NewStruct : create object Struct to do introspection
func NewStruct(obj interface{}, args ...string) *Struct {
me := new(Struct)
if len(args) > 0 {
me.Separator = args[0]
} else {
me.Separator = "."
}
me.obj = obj
me.data = make(map[string]*interface{})
me.walk(me.obj, "")
keys := reflect.ValueOf(me.data).MapKeys()
me.keys = make([]string, len(keys))
for i := 0; i < len(keys); i++ {
me.keys[i] = keys[i].String()
}
return me
}
// Get an array of paths
func (me *Struct) Keys() []string {
return me.keys
}
// Get type of value for path
func (me *Struct) TypeOf(path string) string {
if me.data[path] == nil {
return "nil"
}
return reflect.TypeOf(*me.data[path]).String()
}
// Get value from path
func (me *Struct) Value(path string) interface{} {
if _, ok := me.data[path]; ok {
return *me.data[path]
}
return nil
}
// Get ptr for path
func (me *Struct) Get(path string) *interface{} {
return me.data[path]
}
// Set value for path
func (me *Struct) Set(path string, value interface{}) {
if _, ok := me.data[path]; ok {
*me.data[path] = value
}
}
// From : https://stackoverflow.com/questions/24348184/get-pointer-to-value-using-reflection
func (me *Struct) walk(obj interface{}, path string) {
var prefix string
if t := reflect.TypeOf(obj); t.Kind() == reflect.Ptr {
prefix = t.Elem().Name()
} else {
prefix = t.Name()
}
if prefix != "" {
path = me.Separator + prefix + me.Separator
} else {
path += prefix + me.Separator
}
val := reflect.ValueOf(obj)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
switch val.Kind() {
case reflect.Struct:
for i := 0; i < val.NumField(); i++ {
item := val.Field(i)
itemType := val.Type().Field(i)
if item.Kind() == reflect.Ptr {
item = item.Elem()
}
fullpath := path + itemType.Name
if item.Kind() == reflect.Struct && item.CanInterface() {
me.walk(item.Interface(), fullpath)
} else if (item.CanAddr() || item.Kind() != reflect.Invalid) && item.CanInterface() {
itemInterface := item.Interface()
switch reflect.TypeOf(itemInterface).Kind() {
case reflect.Map:
me.walk(itemInterface, fullpath)
case reflect.Slice:
me.walk(itemInterface, fullpath)
default:
me.data[fullpath] = &itemInterface
}
}
}
case reflect.Map:
for _, key := range val.MapKeys() {
item := val.MapIndex(key)
itemInterface := item.Interface()
if itemInterface == nil {
continue
}
fullpath := path + fmt.Sprint(key.Interface())
switch reflect.TypeOf(item.Interface()).Kind() {
case reflect.Map:
me.walk(itemInterface, fullpath)
case reflect.Slice:
me.walk(itemInterface.([]interface{}), fullpath)
default:
me.data[fullpath] = &itemInterface
}
}
case reflect.Slice:
items := reflect.ValueOf(obj)
for i := 0; i < items.Len(); i++ {
item := items.Index(i)
itemInterface := item.Interface()
itemType := reflect.ValueOf(itemInterface).Kind()
fullpath := path + fmt.Sprint(i)
switch itemType {
case reflect.Map:
me.walk(itemInterface, fullpath)
case reflect.Slice:
me.walk(itemInterface, fullpath)
default:
me.data[fullpath] = &itemInterface
}
}
default:
if reflect.TypeOf(obj).Kind() == reflect.Ptr {
item := reflect.ValueOf(obj).Elem().Interface()
me.data[path] = &item
} else {
me.data[path] = &obj
}
}
}