-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreflect.go
90 lines (73 loc) · 2.61 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
package tormenta
import (
"fmt"
"reflect"
)
var (
typeInt = reflect.TypeOf(0)
typeUint = reflect.TypeOf(uint(0))
typeFloat = reflect.TypeOf(0.99)
typeString = reflect.TypeOf("")
typeBool = reflect.TypeOf(true)
)
// The idea here is to keep all the reflect code in one place,
// which might help to spot potential optimisations / refactors
func indexStringForThisEntity(record Record) string {
return string(typeToIndexString(reflect.TypeOf(record).String()))
}
func entityTypeAndValue(t interface{}) ([]byte, reflect.Value) {
e := reflect.Indirect(reflect.ValueOf(t))
return typeToKeyRoot(e.Type().String()), e
}
func newRecordFromSlice(target interface{}) Record {
_, value := entityTypeAndValue(target)
typ := value.Type().Elem()
return reflect.New(typ).Interface().(Record)
}
func newRecord(target interface{}) Record {
_, value := entityTypeAndValue(target)
typ := value.Type()
return reflect.New(typ).Interface().(Record)
}
func newResultsArray(sliceTarget interface{}) reflect.Value {
return reflect.Indirect(reflect.ValueOf(sliceTarget))
}
func recordValue(record Record) reflect.Value {
return reflect.Indirect(reflect.ValueOf(record))
}
func setResultsArrayOntoTarget(sliceTarget interface{}, records reflect.Value) {
reflect.Indirect(reflect.ValueOf(sliceTarget)).Set(records)
}
func setSingleResultOntoTarget(target interface{}, record Record) {
reflect.Indirect(reflect.ValueOf(target)).Set(reflect.Indirect(reflect.ValueOf(record)))
}
func fieldValue(entity Record, fieldName string) reflect.Value {
return recordValue(entity).FieldByName(fieldName)
}
func fieldKind(target interface{}, fieldName string) (reflect.Kind, error) {
// The target will either be a pointer to slice or struct
// Start of assuming its a pointer to a struct
ss := reflect.ValueOf(target).Elem()
// Check if its a slice, and if so,
// get the underlying type, create a new struct value pointer,
// and dereference it
if ss.Type().Kind() == reflect.Slice {
ss = reflect.New(ss.Type().Elem()).Elem()
}
// At this point, independently of whether the input was a struct or slice,
// we can get the required field by name and get its kind
v := ss.FieldByName(fieldName)
if !v.IsValid() {
return 0, fmt.Errorf(ErrFieldCouldNotBeFound, fieldName)
}
return v.Type().Kind(), nil
}
// newSlice sets up a new target slice for results
// this was arrived at after a lot of experimentation
// so might not be the most efficient way!! TODO
func newSlice(t reflect.Type, l int) interface{} {
asSlice := reflect.MakeSlice(reflect.SliceOf(t), 0, l)
new := reflect.New(asSlice.Type())
new.Elem().Set(asSlice)
return new.Interface()
}