This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathget.go
165 lines (137 loc) · 4.02 KB
/
get.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
package pointerstructure
import (
"fmt"
"reflect"
"strings"
)
// Get reads the value out of the total value v.
//
// For struct values a `pointer:"<name>"` tag on the struct's
// fields may be used to override that field's name for lookup purposes.
// Alternatively the tag name used can be overridden in the `Config`.
func (p *Pointer) Get(v interface{}) (interface{}, error) {
// fast-path the empty address case to avoid reflect.ValueOf below
if len(p.Parts) == 0 {
return v, nil
}
// Map for lookup of getter to call for type
funcMap := map[reflect.Kind]func(string, reflect.Value) (reflect.Value, error){
reflect.Array: p.getSlice,
reflect.Map: p.getMap,
reflect.Slice: p.getSlice,
reflect.Struct: p.getStruct,
}
currentVal := reflect.ValueOf(v)
for i, part := range p.Parts {
for currentVal.Kind() == reflect.Interface {
currentVal = currentVal.Elem()
}
for currentVal.Kind() == reflect.Ptr {
currentVal = reflect.Indirect(currentVal)
}
f, ok := funcMap[currentVal.Kind()]
if !ok {
return nil, fmt.Errorf(
"%s: at part %d, %w: %s", p, i, ErrInvalidKind, currentVal.Kind())
}
var err error
currentVal, err = f(part, currentVal)
if err != nil {
return nil, fmt.Errorf("%s at part %d: %w", p, i, err)
}
if p.Config.ValueTransformationHook != nil {
currentVal = p.Config.ValueTransformationHook(currentVal)
if currentVal == reflect.ValueOf(nil) {
return nil, fmt.Errorf("%s at part %d: ValueTransformationHook returned the value of a nil interface", p, i)
}
}
}
return currentVal.Interface(), nil
}
func (p *Pointer) getMap(part string, m reflect.Value) (reflect.Value, error) {
var zeroValue reflect.Value
// Coerce the string part to the correct key type
key, err := coerce(reflect.ValueOf(part), m.Type().Key())
if err != nil {
return zeroValue, err
}
// Verify that the key exists
found := false
for _, k := range m.MapKeys() {
if k.Interface() == key.Interface() {
found = true
break
}
}
if !found {
return zeroValue, fmt.Errorf("%w %#v", ErrNotFound, key.Interface())
}
// Get the key
return m.MapIndex(key), nil
}
func (p *Pointer) getSlice(part string, v reflect.Value) (reflect.Value, error) {
var zeroValue reflect.Value
// Coerce the key to an int
idxVal, err := coerce(reflect.ValueOf(part), reflect.TypeOf(42))
if err != nil {
return zeroValue, err
}
idx := int(idxVal.Int())
// Verify we're within bounds
if idx < 0 || idx >= v.Len() {
return zeroValue, fmt.Errorf(
"index %d is %w (length = %d)", idx, ErrOutOfRange, v.Len())
}
// Get the key
return v.Index(idx), nil
}
func (p *Pointer) getStruct(part string, m reflect.Value) (reflect.Value, error) {
var foundField reflect.Value
var found bool
var ignored bool
typ := m.Type()
tagName := p.Config.TagName
if tagName == "" {
tagName = "pointer"
}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if field.PkgPath != "" {
// this is an unexported field so ignore it
continue
}
fieldTag := field.Tag.Get(tagName)
if fieldTag != "" {
if idx := strings.Index(fieldTag, ","); idx != -1 {
fieldTag = fieldTag[0:idx]
}
if strings.Contains(fieldTag, "|") {
// should this panic instead?
return foundField, fmt.Errorf("pointer struct tag cannot contain the '|' character")
}
if fieldTag == "-" {
// we should ignore this field but cannot immediately return because its possible another
// field has a tag that would allow it to assume this ones name.
if field.Name == part {
found = true
ignored = true
}
continue
} else if fieldTag == part {
// we can go ahead and return now as the tag is enough to
// indicate that this is the correct field
return m.Field(i), nil
}
} else if field.Name == part {
foundField = m.Field(i)
found = true
}
}
if !found {
return reflect.Value{}, fmt.Errorf("%w: struct field with name %q", ErrNotFound, part)
}
if ignored {
return reflect.Value{}, fmt.Errorf("struct field %q is ignored and cannot be used", part)
}
return foundField, nil
}