forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect.go
180 lines (156 loc) · 4.48 KB
/
reflect.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package utils
import (
"encoding/json"
"errors"
"fmt"
"github.com/golang/glog"
"reflect"
)
var SkipReflection = errors.New("skip this value")
// JsonMergeStruct merges src into dest
// It uses a JSON marshal & unmarshal, so only fields that are JSON-visible will be copied
func JsonMergeStruct(dest, src interface{}) {
// Not the most efficient approach, but simple & relatively well defined
j, err := json.Marshal(src)
if err != nil {
glog.Fatalf("error marshalling config: %v", err)
}
err = json.Unmarshal(j, dest)
if err != nil {
glog.Fatalf("error unmarshalling config: %v", err)
}
}
// InvokeMethod calls the specified method by reflection
func InvokeMethod(target interface{}, name string, args ...interface{}) ([]reflect.Value, error) {
v := reflect.ValueOf(target)
method, found := v.Type().MethodByName(name)
if !found {
return nil, fmt.Errorf("method %q not found on %T", name, target)
}
var argValues []reflect.Value
for _, a := range args {
argValues = append(argValues, reflect.ValueOf(a))
}
glog.V(8).Infof("Calling method %s on %T", method.Name, target)
m := v.MethodByName(method.Name)
rv := m.Call(argValues)
return rv, nil
}
func BuildTypeName(t reflect.Type) string {
switch t.Kind() {
case reflect.Ptr:
return "*" + BuildTypeName(t.Elem())
case reflect.Slice:
return "[]" + BuildTypeName(t.Elem())
case reflect.Struct, reflect.Interface:
return t.Name()
case reflect.String, reflect.Bool, reflect.Int64:
return t.Name()
case reflect.Map:
return "map[" + BuildTypeName(t.Key()) + "]" + BuildTypeName(t.Elem())
default:
glog.Errorf("cannot find type name for: %v, assuming %s", t, t.Name())
return t.Name()
}
}
type visitorFunc func(path string, field *reflect.StructField, v reflect.Value) error
// ReflectRecursive calls visitor with v and every recursive sub-value, skipping subtrees if SkipReflection is returned
func ReflectRecursive(v reflect.Value, visitor visitorFunc) error {
return reflectRecursive("", v, visitor)
}
func reflectRecursive(path string, v reflect.Value, visitor visitorFunc) error {
vType := v.Type()
err := visitor(path, nil, v)
if err != nil {
if err == SkipReflection {
return nil
}
return err
}
switch v.Kind() {
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
structField := vType.Field(i)
if structField.PkgPath != "" {
// Field not exported
continue
}
f := v.Field(i)
childPath := path + "." + structField.Name
// TODO: I think we are double visiting here; we should instead pass down structField into reflectRecursive
err := visitor(childPath, &structField, f)
if err != nil && err != SkipReflection {
return err
}
if err == nil {
err = reflectRecursive(childPath, f, visitor)
if err != nil {
return err
}
}
}
break
case reflect.Map:
keys := v.MapKeys()
for _, key := range keys {
mv := v.MapIndex(key)
childPath := path + "[" + fmt.Sprintf("%s", key.Interface()) + "]"
err := visitor(childPath, nil, mv)
if err != nil && err != SkipReflection {
return err
}
if err == nil {
err = reflectRecursive(childPath, mv, visitor)
if err != nil {
return err
}
}
}
break
case reflect.Array, reflect.Slice:
len := v.Len()
for i := 0; i < len; i++ {
av := v.Index(i)
childPath := path + "[" + fmt.Sprintf("%d", i) + "]"
err := visitor(childPath, nil, av)
if err != nil && err != SkipReflection {
return err
}
if err == nil {
err = reflectRecursive(childPath, av, visitor)
if err != nil {
return err
}
}
}
break
case reflect.Ptr, reflect.Interface:
if !v.IsNil() {
e := v.Elem()
err = reflectRecursive(path, e, visitor)
if err != nil {
return err
}
}
break
}
return nil
}
// IsPrimitiveValue returns true if passed a value of primitive type: int, bool, etc
// Note that string (like []byte) is not treated as a primitive type
func IsPrimitiveValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
return true
// The less-obvious cases!
case reflect.String, reflect.Slice, reflect.Array:
return false
case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func, reflect.Map, reflect.Struct, reflect.UnsafePointer:
return false
default:
glog.Fatalf("Unhandled kind: %v", v.Kind())
return false
}
}