-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreflect_helper.go
253 lines (236 loc) · 5.53 KB
/
reflect_helper.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
package dynmgrm
import (
"github.com/iancoleman/strcase"
"reflect"
"strings"
)
type gormTag struct {
Column string
DBType string
}
// gormTagWithStructTag returns gormTag representation of reflect.StructTag
func newGormTag(tag reflect.StructTag) gormTag {
gTag := gormTag{}
for _, value := range strings.Split(tag.Get("gorm"), ";") {
if value == "" {
continue
}
kv := strings.Split(value, ":")
if len(kv) != 2 {
continue
}
switch kv[0] {
case "column":
gTag.Column = kv[1]
case "type":
gTag.DBType = kv[1]
}
}
return gTag
}
// getDBNameFromStructField returns the name of the field in the struct
func getDBNameFromStructField(tf reflect.StructField) string {
gTag := newGormTag(tf.Tag)
if gTag.Column != "" {
return gTag.Column
}
return strcase.ToSnake(tf.Name)
}
// getDBTypeFromStructField returns the type of the field in the struct
func getDBTypeFromStructField(tf reflect.StructField) string {
gTag := newGormTag(tf.Tag)
return gTag.DBType
}
type secondaryIndexKind int
const (
secondaryIndexKindLSI secondaryIndexKind = iota + 1
secondaryIndexKindGSI
)
func (sik secondaryIndexKind) String() string {
switch sik {
case secondaryIndexKindLSI:
return "LSI"
case secondaryIndexKindGSI:
return "GSI"
}
return ""
}
type secondaryIndexProperty struct {
PK bool
SK bool
Name string
Kind secondaryIndexKind
}
type dynmgrmTag struct {
PK bool
SK bool
IndexProperty []secondaryIndexProperty
NonProjective []string
}
func newDynmgrmTag(tag reflect.StructTag) dynmgrmTag {
res := dynmgrmTag{}
for _, value := range strings.Split(tag.Get("dynmgrm"), ";") {
if value == "" {
continue
}
kv := strings.Split(value, ":")
switch tn := kv[0]; tn {
case "pk":
if res.SK {
continue
}
res.PK = true
case "sk":
if res.PK {
continue
}
res.SK = true
case "gsi-pk":
iprp := secondaryIndexProperty{
PK: true,
Name: kv[1],
Kind: secondaryIndexKindGSI,
}
res.IndexProperty = append(res.IndexProperty, iprp)
case "gsi-sk":
iprp := secondaryIndexProperty{
SK: true,
Name: kv[1],
Kind: secondaryIndexKindGSI,
}
res.IndexProperty = append(res.IndexProperty, iprp)
case "lsi-sk":
iprp := secondaryIndexProperty{
Name: kv[1],
SK: true,
Kind: secondaryIndexKindLSI,
}
res.IndexProperty = append(res.IndexProperty, iprp)
case "non-projective":
npl := strings.ReplaceAll(strings.ReplaceAll(kv[1], "[", ""), "]", "")
for _, np := range strings.Split(npl, ",") {
res.NonProjective = append(res.NonProjective, np)
}
}
}
return res
}
type dynmgrmKeyDefine struct {
Name string
DataType string
}
type dynmgrmSecondaryIndexDefine struct {
PK dynmgrmKeyDefine
SK dynmgrmKeyDefine
NonProjectiveAttrs []string
}
type dynmgrmTableDefine struct {
PK dynmgrmKeyDefine
SK dynmgrmKeyDefine
NonKeyAttr []string
GSI map[string]*dynmgrmSecondaryIndexDefine
LSI map[string]*dynmgrmSecondaryIndexDefine
}
func newDynmgrmTableDefine(modelMeta reflect.Type) dynmgrmTableDefine {
res := dynmgrmTableDefine{
NonKeyAttr: make([]string, 0, modelMeta.NumField()),
GSI: make(map[string]*dynmgrmSecondaryIndexDefine),
LSI: make(map[string]*dynmgrmSecondaryIndexDefine),
}
nonProjectiveAttrsMap := make(map[string]*[]string)
for i := 0; i < modelMeta.NumField(); i++ {
tf := modelMeta.Field(i)
dTag := newDynmgrmTag(tf.Tag)
cn := getDBNameFromStructField(tf)
isKey := false
if dTag.PK {
res.PK = dynmgrmKeyDefine{
Name: cn,
DataType: extractDBTypeFromStructField(tf),
}
isKey = true
}
if dTag.SK {
res.SK = dynmgrmKeyDefine{
Name: cn,
DataType: extractDBTypeFromStructField(tf),
}
isKey = true
}
if !isKey {
res.NonKeyAttr = append(res.NonKeyAttr, cn)
}
for _, ip := range dTag.IndexProperty {
switch ip.Kind {
case secondaryIndexKindGSI:
sid, ok := res.GSI[ip.Name]
if !ok {
sid = &dynmgrmSecondaryIndexDefine{}
res.GSI[ip.Name] = sid
}
if ip.PK {
sid.PK = dynmgrmKeyDefine{
Name: cn,
DataType: extractDBTypeFromStructField(tf),
}
}
if ip.SK {
sid.SK = dynmgrmKeyDefine{
Name: cn,
DataType: extractDBTypeFromStructField(tf),
}
}
case secondaryIndexKindLSI:
sid, ok := res.LSI[ip.Name]
if !ok {
sid = &dynmgrmSecondaryIndexDefine{}
res.LSI[ip.Name] = sid
}
sid.SK = dynmgrmKeyDefine{
Name: cn,
DataType: extractDBTypeFromStructField(tf),
}
}
}
for _, np := range dTag.NonProjective {
list, ok := nonProjectiveAttrsMap[np]
if !ok {
physicalList := make([]string, 0)
list = &physicalList
nonProjectiveAttrsMap[np] = list
}
*list = append(*list, cn)
}
}
for idxn, list := range nonProjectiveAttrsMap {
index, ok := res.LSI[idxn]
if !ok {
index, ok = res.GSI[idxn]
if !ok {
continue
}
}
// list will never be nil
if pl := *list; len(pl) > 0 {
index.NonProjectiveAttrs = append(index.NonProjectiveAttrs, pl...)
}
}
return res
}
func extractDBTypeFromStructField(field reflect.StructField) string {
dbType := getDBTypeFromStructField(field)
if dbType != "" {
return dbType
}
switch field.Type.Kind() {
case reflect.String:
return KeySchemaDataTypeString.String()
case reflect.Int, reflect.Float64:
return KeySchemaDataTypeNumber.String()
case reflect.Slice:
if field.Type.Elem().Kind() == reflect.Uint8 {
return KeySchemaDataTypeBinary.String()
}
}
return KeySchemaDataTypeString.String()
}