-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve_path.go
173 lines (151 loc) · 3.42 KB
/
resolve_path.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
package laravalidate
import (
"reflect"
"strconv"
)
func resolveWithValue(value reflect.Value, path []string) *Needle {
if len(path) == 0 {
valueType := value.Type()
return &Needle{
Value: &value,
Type: valueType,
}
}
for value.Kind() == reflect.Ptr {
if value.IsNil() {
return resolveWithType(value.Type().Elem(), path)
}
value = value.Elem()
}
needle := path[0]
path = path[1:]
if needle == "" {
return nil
}
kind := value.Kind()
switch kind {
case reflect.Struct:
field, ok := value.Type().FieldByName(needle)
if !ok {
return nil
}
return resolveWithValue(value.FieldByIndex(field.Index), path)
case reflect.Slice, reflect.Array:
needleNumber, err := strconv.Atoi(needle)
if err != nil {
return nil
}
if kind == reflect.Slice && value.IsNil() {
return resolveWithType(value.Type().Elem(), path)
}
if needleNumber < 0 || needleNumber >= value.Len() {
return nil
}
return resolveWithValue(value.Index(needleNumber), path)
case reflect.Map:
var key reflect.Value
keySet := false
valueType := value.Type()
keyKind := valueType.Key().Kind()
switch keyKind {
case reflect.Bool:
if needle == "true" {
key = reflect.ValueOf(true)
keySet = true
} else if needle == "false" {
key = reflect.ValueOf(false)
keySet = true
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
index, err := strconv.ParseInt(needle, 10, 64)
if err != nil {
return nil
}
switch keyKind {
case reflect.Int:
key = reflect.ValueOf(int(index))
case reflect.Int8:
if int64(int8(index)) != index {
return nil
}
key = reflect.ValueOf(int8(index))
case reflect.Int16:
if int64(int16(index)) != index {
return nil
}
key = reflect.ValueOf(int16(index))
case reflect.Int32:
if int64(int32(index)) != index {
return nil
}
key = reflect.ValueOf(int32(index))
case reflect.Int64:
key = reflect.ValueOf(int64(index))
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
index, err := strconv.ParseUint(needle, 10, 64)
if err != nil {
return nil
}
switch keyKind {
case reflect.Uint:
key = reflect.ValueOf(int(index))
case reflect.Int8:
if uint64(uint8(index)) != index {
return nil
}
key = reflect.ValueOf(int8(index))
case reflect.Uint16:
if uint64(uint16(index)) != index {
return nil
}
key = reflect.ValueOf(int16(index))
case reflect.Uint32:
if uint64(uint32(index)) != index {
return nil
}
key = reflect.ValueOf(int32(index))
case reflect.Uint64:
key = reflect.ValueOf(int64(index))
}
case reflect.String:
key = reflect.ValueOf(needle)
keySet = true
}
if !keySet {
return nil
}
field := value.MapIndex(key)
if field.Kind() == reflect.Invalid {
return resolveWithType(valueType.Elem(), path)
}
return resolveWithValue(field, path)
default:
return nil
}
}
func resolveWithType(valueType reflect.Type, path []string) *Needle {
if len(path) == 0 {
return &Needle{
Type: valueType,
}
}
for valueType.Kind() == reflect.Ptr {
valueType = valueType.Elem()
}
needle := path[0]
path = path[1:]
if needle == "" {
return nil
}
kind := valueType.Kind()
switch kind {
case reflect.Struct:
field, ok := valueType.FieldByName(needle)
if !ok {
return nil
}
return resolveWithType(field.Type, path)
}
return nil
}