-
Notifications
You must be signed in to change notification settings - Fork 13
/
reflect.go
126 lines (115 loc) · 2.94 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
package core
import (
"fmt"
"reflect"
"strings"
"sync"
"time"
)
var (
typeCache = make(map[reflect.Value]map[string]int)
typeLock sync.RWMutex
)
// Resolves the value of field within data
func Resolve(data interface{}, field string) interface{} {
switch typed := data.(type) {
case map[string]string:
return typed[field]
case map[string]interface{}:
return typed[field]
case map[string]int:
return typed[field]
case map[string]bool:
return typed[field]
case map[string]float64:
return typed[field]
case map[string]byte:
return typed[field]
case map[string][]byte:
return typed[field]
}
v := reflect.ValueOf(data)
switch v.Kind() {
case reflect.Map:
value := v.MapIndex(reflect.ValueOf(field))
if value.IsValid() {
return value.Interface()
}
return nil
case reflect.Struct:
return resolveStruct(v, field)
case reflect.Ptr:
v = reflect.Indirect(v)
if v.Kind() != reflect.Struct {
return nil
}
return resolveStruct(v, field)
default:
return nil
}
}
// This is necessary in the case where the value references a struct directly:
// template := "{{ user }}""
// data := map[string]interface{}{"user": &User{"Leto"}},
// Without this step, the above would result in a value which points to the User
// we need to resolve this a step further and get the value of "User" (which
// will either me the output of its String() method, or %v)
//
// Of course, we only want this final resolution once we need the value. If we
// call this too early, say in Resolve above, we won't be able to build nested
// paths
func ResolveFinal(value interface{}) interface{} {
if _, ok := value.(time.Time); ok {
return value
}
kind := reflect.ValueOf(value).Kind()
if kind == reflect.Ptr || kind == reflect.Struct {
return resolvePtrOrStruct(value)
}
return value
}
func resolvePtrOrStruct(value interface{}) interface{} {
if s, ok := value.(fmt.Stringer); ok {
return s.String()
}
return ToBytes(value)
}
func resolveStruct(value reflect.Value, field string) interface{} {
typeLock.RLock()
typeData, exists := typeCache[value]
typeLock.RUnlock()
if exists == false {
typeData = buildTypeData(value)
}
if index, exists := typeData[field]; exists {
return value.Field(index).Interface()
}
return nil
}
func buildTypeData(value reflect.Value) map[string]int {
t := value.Type()
fieldCount := t.NumField()
typeData := make(map[string]int, fieldCount)
for i := 0; i < fieldCount; i++ {
typeData[strings.ToLower(t.Field(i).Name)] = i
}
typeLock.Lock()
defer typeLock.Unlock()
if typeData, exists := typeCache[value]; exists {
return typeData
}
typeCache[value] = typeData
return typeData
}
//gets the length of string, map or array
func ToLength(input interface{}) (int, bool) {
if s, ok := input.(string); ok {
return len(s), true
}
value := reflect.ValueOf(input)
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map {
return value.Len(), true
}
return 0, false
}