forked from ghetzel/pivot
/
record_loader.go
123 lines (95 loc) · 2.86 KB
/
record_loader.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
package dal
import (
"fmt"
"reflect"
"strings"
"github.com/fatih/structs"
"github.com/ghetzel/go-stockutil/sliceutil"
)
var RecordStructTag = `pivot`
var DefaultStructIdentityFieldName = `ID`
type fieldDescription struct {
Field *structs.Field
ReflectField reflect.Value
Identity bool
OmitEmpty bool
}
type Model interface{}
func GetIdentityFieldName(instance interface{}, fallbackIdentityFieldName string) (string, error) {
if err := validatePtrToStructType(instance); err != nil {
return ``, err
}
s := structs.New(instance)
// find a field with an ",identity" tag and get its value
for _, field := range s.Fields() {
if tag := field.Tag(RecordStructTag); tag != `` {
v := strings.Split(tag, `,`)
if sliceutil.ContainsString(v[1:], `identity`) {
return field.Name(), nil
}
}
}
if fallbackIdentityFieldName == `` {
fallbackIdentityFieldName = DefaultStructIdentityFieldName
}
if _, ok := s.FieldOk(fallbackIdentityFieldName); ok {
return fallbackIdentityFieldName, nil
} else if _, ok := s.FieldOk(DefaultStructIdentityFieldName); ok {
return DefaultStructIdentityFieldName, nil
}
return ``, fmt.Errorf("No identity field could be found for type %T", instance)
}
func validatePtrToStructType(instance interface{}) error {
vInstance := reflect.ValueOf(instance)
if vInstance.IsValid() {
if vInstance.Kind() == reflect.Ptr {
vInstance = vInstance.Elem()
}
if vInstance.Kind() == reflect.Struct {
return nil
} else {
return fmt.Errorf("Can only operate on pointer to struct, got %T", instance)
}
} else {
return fmt.Errorf("invalid value %T", instance)
}
}
func getFieldsForStruct(instance interface{}) (map[string]fieldDescription, error) {
fields := make(map[string]fieldDescription)
identitySet := false
reflectStruct := reflect.ValueOf(instance)
if reflectStruct.Kind() == reflect.Ptr {
reflectStruct = reflectStruct.Elem()
}
if reflectStruct.Kind() != reflect.Struct {
return nil, fmt.Errorf("value must be a struct")
}
instanceStruct := structs.New(instance)
for _, field := range instanceStruct.Fields() {
var identity, omitEmpty bool
name := field.Name()
// read struct tags to determine how values are mapped to struct fields
if tag := field.Tag(RecordStructTag); tag != `` {
v := strings.Split(tag, `,`)
// if the first value isn't an empty string, that's what we're calling the field
if v[0] != `` {
name = v[0]
}
// set additional flags from tag options
if len(v) > 1 {
identity = sliceutil.ContainsString(v[1:], `identity`)
omitEmpty = sliceutil.ContainsString(v[1:], `omitempty`)
}
}
if !identitySet && identity {
identitySet = true
}
fields[name] = fieldDescription{
Field: field,
ReflectField: reflectStruct.FieldByName(field.Name()),
Identity: identity,
OmitEmpty: omitEmpty,
}
}
return fields, nil
}