-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbound_mapping.go
351 lines (340 loc) · 9.5 KB
/
bound_mapping.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package set
import (
"errors"
"fmt"
"reflect"
"time"
"github.com/nofeaturesonlybugs/set/path"
)
// BoundMapping is returned from Mapper's Bind method.
//
// A BoundMapping must not be copied except via its Copy method.
//
// A BoundMapping should be used in iterative code that needs to read or mutate
// many instances of the same struct. Bound mappings allow for adhoc or indeterminate
// field access within the bound data.
// // adhoc access means different fields can be accessed between calls to Rebind()
// var a, b T
//
// bound := myMapper.Map(&a)
// bound.Set("Field", 10)
// bound.Set("Other", "Hello")
//
// bound.Rebind(&b)
// bound.Set("Bar", 27)
//
// In the preceding example the BoundMapping is first bound to a and later bound to b
// and each instance had different field(s) accessed.
type BoundMapping struct {
// top is the original type used to create the value; it is needed to ensure type compatibility
// when calling Rebind.
// value is the bound value after passing through Writable to get to the end of any pointer chain.
top reflect.Type
value reflect.Value
err error
// NB This field should be treated as read-only.
paths map[string]path.ReflectPath
}
// Assignables returns a slice of pointers to the fields in the currently bound struct
// in the order specified by the fields argument.
//
// To alleviate pressure on the garbage collector the return slice can be pre-allocated and
// passed as the second argument to Assignables. If non-nil it is assumed len(fields) == len(rv)
// and failure to provide an appropriately sized non-nil slice will cause a panic.
//
// During traversal this method will allocate struct fields that are nil pointers.
//
// An example use-case would be obtaining a slice of pointers for Rows.Scan() during database
// query results.
func (b BoundMapping) Assignables(fields []string, rv []interface{}) ([]interface{}, error) {
if b.err != nil && errors.Is(b.err, ErrReadOnly) {
return rv, b.err.(pkgerr).WithCallSite("BoundMapping.Assignables")
}
if rv == nil {
rv = make([]interface{}, len(fields))
}
for fieldN, name := range fields {
step, ok := b.paths[name]
if !ok {
err := pkgerr{
Err: ErrUnknownField,
CallSite: "BoundMapping.Assignables",
Context: "field [" + name + "] not found in type " + b.top.String(),
}
return rv, err
}
v := b.value
if step.HasPointer { // NB Begin manual inline of path.ReflectPath.Value
for _, n := range step.Index {
v = v.Field(n)
for ; v.Kind() == reflect.Ptr; v = v.Elem() {
if v.IsNil() && v.CanSet() {
v.Set(reflect.New(v.Type().Elem()))
}
}
}
} else {
for _, n := range step.Index {
v = v.Field(n)
}
}
v = v.Field(step.Last) // NB End manual inline of path.ReflectPath.Value
rv[fieldN] = v.Addr().Interface()
}
return rv, nil
}
// Copy creates an exact copy of the BoundMapping.
//
// One use case for Copy is to create a set of BoundMappings early in a program's
// init phase. During later execution when a BoundMapping is needed for type T
// it can be obtained by calling Copy on the cached BoundMapping for that type.
func (b BoundMapping) Copy() BoundMapping {
return BoundMapping{
top: b.top,
value: b.value,
err: b.err,
paths: b.paths,
}
}
// Err returns an error that may have occurred during repeated calls to Set(); it is reset on
// calls to Rebind()
func (b BoundMapping) Err() error {
return b.err
}
// Field returns the Value for field.
func (b BoundMapping) Field(field string) (Value, error) {
if b.err != nil && errors.Is(b.err, ErrReadOnly) {
return zeroV, b.err.(pkgerr).WithCallSite("BoundMapping.Field")
}
step, ok := b.paths[field]
if !ok {
err := pkgerr{
Err: ErrUnknownField,
CallSite: "BoundMapping.Field",
Context: "field [" + field + "] not found in type " + b.top.String(),
}
return zeroV, err
}
v := b.value
if step.HasPointer { // NB Begin manual inline of path.ReflectPath.Value
for _, n := range step.Index {
v = v.Field(n)
for ; v.Kind() == reflect.Ptr; v = v.Elem() {
if v.IsNil() && v.CanSet() {
v.Set(reflect.New(v.Type().Elem()))
}
}
}
} else {
for _, n := range step.Index {
v = v.Field(n)
}
}
v = v.Field(step.Last) // NB End manual inline of path.ReflectPath.Value
//
return V(v), nil
}
// Fields returns a slice of values to the fields in the currently bound struct in the order
// specified by the fields argument.
//
// To alleviate pressure on the garbage collector the return slice can be pre-allocated and
// passed as the second argument to Fields. If non-nil it is assumed len(fields) == len(rv)
// and failure to provide an appropriately sized non-nil slice will cause a panic.
//
// During traversal this method will allocate struct fields that are nil pointers.
//
// An example use-case would be obtaining a slice of query arguments by column name during
// database queries.
func (b BoundMapping) Fields(fields []string, rv []interface{}) ([]interface{}, error) {
if b.err != nil && errors.Is(b.err, ErrReadOnly) {
return rv, b.err.(pkgerr).WithCallSite("BoundMapping.Fields")
}
if rv == nil {
rv = make([]interface{}, len(fields))
}
for fieldN, name := range fields {
step, ok := b.paths[name]
if !ok {
err := pkgerr{
Err: ErrUnknownField,
CallSite: "BoundMapping.Fields",
Context: "field [" + name + "] not found in type " + b.top.String(),
}
return rv, err
}
v := b.value
if step.HasPointer { // NB Begin manual inline of path.ReflectPath.Value
for _, n := range step.Index {
v = v.Field(n)
for ; v.Kind() == reflect.Ptr; v = v.Elem() {
if v.IsNil() && v.CanSet() {
v.Set(reflect.New(v.Type().Elem()))
}
}
}
} else {
for _, n := range step.Index {
v = v.Field(n)
}
}
v = v.Field(step.Last) // NB End manual inline of path.ReflectPath.Value
// NB The value we want is v.Interface() which performs a number of allocations for built-in primitives.
// If we switch off v's type as a pointer and it is a primitive we can skip the allocations.
switch ptr := v.Addr().Interface().(type) {
case *bool:
rv[fieldN] = *ptr
case *int:
rv[fieldN] = *ptr
case *int8:
rv[fieldN] = *ptr
case *int16:
rv[fieldN] = *ptr
case *int32:
rv[fieldN] = *ptr
case *int64:
rv[fieldN] = *ptr
case *uint:
rv[fieldN] = *ptr
case *uint8:
rv[fieldN] = *ptr
case *uint16:
rv[fieldN] = *ptr
case *uint32:
rv[fieldN] = *ptr
case *uint64:
rv[fieldN] = *ptr
case *float32:
rv[fieldN] = *ptr
case *float64:
rv[fieldN] = *ptr
case *string:
rv[fieldN] = *ptr
case *time.Time:
rv[fieldN] = *ptr
default:
rv[fieldN] = v.Interface()
}
}
return rv, nil
}
// Rebind will replace the currently bound value with the new variable v.
//
// v must have the same type as the original value used to create the BoundMapping
// otherwise a panic will occur.
//
// As a convenience Rebind allows v to be an instance of reflect.Value. This prevents
// unnecessary calls to reflect.Value.Interface().
func (b *BoundMapping) Rebind(v interface{}) {
if b.err != nil && errors.Is(b.err, ErrReadOnly) {
return
}
//
// Allow reflect.Value to be passed directly.
var rv reflect.Value
var T reflect.Type
switch sw := v.(type) {
case reflect.Value:
rv = sw
T = rv.Type()
default:
rv = reflect.ValueOf(v)
T = rv.Type()
}
if b.top != T {
panic(fmt.Sprintf("mismatching types during Rebind; have %v and got %T", b.top.String(), v)) // TODO ErrRebind maybe?
}
b.err = nil
b.value, _ = Writable(rv)
}
// Set effectively sets V[field] = value.
func (b *BoundMapping) Set(field string, value interface{}) error {
if b.err != nil && errors.Is(b.err, ErrReadOnly) {
return b.err.(pkgerr).WithCallSite("BoundMapping.Set")
}
//
step, ok := b.paths[field]
if !ok {
err := pkgerr{
Err: ErrUnknownField,
CallSite: "BoundMapping.Set",
Context: "field [" + field + "] not found in type " + b.top.String(),
}
if b.err == nil {
b.err = err
}
return err
}
v := b.value
if step.HasPointer { // NB Begin manual inline of path.ReflectPath.Value
for _, n := range step.Index {
v = v.Field(n)
for ; v.Kind() == reflect.Ptr; v = v.Elem() {
if v.IsNil() && v.CanSet() {
v.Set(reflect.New(v.Type().Elem()))
}
}
}
} else {
for _, n := range step.Index {
v = v.Field(n)
}
}
v = v.Field(step.Last) // NB End manual inline of path.ReflectPath.Value
//
// If the types are directly equatable then we might be able to avoid creating a V(fieldValue),
// which will cut down our allocations and increase speed.
if v.Type() == reflect.TypeOf(value) {
switch tt := value.(type) {
case bool:
v.SetBool(tt)
return nil
case int:
v.SetInt(int64(tt))
return nil
case int8:
v.SetInt(int64(tt))
return nil
case int16:
v.SetInt(int64(tt))
return nil
case int32:
v.SetInt(int64(tt))
return nil
case int64:
v.SetInt(tt)
return nil
case uint:
v.SetUint(uint64(tt))
return nil
case uint8:
v.SetUint(uint64(tt))
return nil
case uint16:
v.SetUint(uint64(tt))
return nil
case uint32:
v.SetUint(uint64(tt))
return nil
case uint64:
v.SetUint(tt)
return nil
case float32:
v.SetFloat(float64(tt))
return nil
case float64:
v.SetFloat(tt)
return nil
case string:
v.SetString(tt)
return nil
}
}
//
// If the type-switch above didn't hit then we'll coerce the
// fieldValue to a Value and use our swiss-army knife Value.To().
err := V(v).To(value)
if err != nil && b.err == nil {
b.err = err // TODO Possibly wrap with more information.
}
return err
}