-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
281 lines (271 loc) · 6.79 KB
/
field.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
package dynago
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
var timeType = reflect.TypeOf(time.Now())
var fmtRegExp = regexp.MustCompile(`\{([A-Z]?[a-zA-Z0-9_]*)\}`)
type field struct {
attrName string
attrType string
fmt string
fmtRegExps map[string]*regexp.Regexp
layout string
index int
attrsToCopy []string
client *Dynago
}
func (d *Dynago) field(sf reflect.StructField, index int) (*field, error) {
var f field
f.index = index
f.client = d
if sf.IsExported() {
if tag, ok := sf.Tag.Lookup(d.config.AttrTagName); ok {
f.attrName = tag
} else {
f.attrName = sf.Name
}
} else {
f.attrName = "-"
return &f, nil
}
ty := sf.Type
for ty.Kind() == reflect.Pointer {
ty = ty.Elem()
}
kind := ty.Kind()
if tag, ok := sf.Tag.Lookup(d.config.TypeTagName); ok {
f.attrType = tag
} else {
switch kind {
case reflect.String:
f.attrType = "S"
case reflect.Int, reflect.Uint, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float64, reflect.Float32:
f.attrType = "N"
case reflect.Bool:
f.attrType = "BOOL"
case reflect.Slice:
switch ty.Elem().Kind() {
case reflect.Uint8:
f.attrType = "B"
default:
f.attrType = "L"
}
case reflect.Struct:
switch ty {
case timeType:
f.attrType = "S"
default:
f.attrType = "M"
}
}
}
if tag, ok := sf.Tag.Lookup(d.config.FmtTagName); ok {
f.fmt = tag
f.attrType = "S"
} else {
f.fmt = "{}"
}
f.fmtRegExps = make(map[string]*regexp.Regexp)
for _, match := range fmtRegExp.FindAllString(f.fmt, -1) {
fname := trimDelims(match)
f.fmtRegExps[fname] = regexp.MustCompile("(?s)^" + fmtRegExp.ReplaceAllString(strings.ReplaceAll(f.fmt, match, "(.*?)"), ".*?") + "$")
}
if tag, ok := sf.Tag.Lookup(d.config.LayoutTagName); ok {
f.layout = tag
} else {
switch kind {
case reflect.Struct:
switch ty {
case timeType:
f.layout = time.RFC3339
}
case reflect.Slice:
elTy := ty.Elem()
for elTy.Kind() == reflect.Pointer {
elTy = elTy.Elem()
}
switch elTy {
case timeType:
f.layout = time.RFC3339
}
}
}
if tag, ok := sf.Tag.Lookup(d.config.AttrsToCopyTagName); ok {
f.attrsToCopy = strings.Split(tag, ",")
}
return &f, nil
}
func (f *field) format(v reflect.Value) (*string, error) {
output := f.fmt
for _, match := range fmtRegExp.FindAllString(f.fmt, -1) {
var fval reflect.Value
var refFieldLayout string
if match == "{}" {
fval = v.Field(f.index)
refFieldLayout = f.layout
} else {
cache, err := f.client.cachedStruct(v.Type()) // TODO: do this lazily
if err != nil {
return nil, err
}
fname := trimDelims(match)
vt := v.Type()
for i := 0; i < vt.NumField(); i++ {
fld := vt.Field(i)
if fld.Name == fname {
fval = v.Field(i)
ff := cache[i]
refFieldLayout = ff.layout
break
}
}
}
for fval.Kind() == reflect.Pointer {
fval = fval.Elem()
}
switch fval.Kind() {
case reflect.String:
output = strings.ReplaceAll(output, match, fval.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
output = strings.ReplaceAll(output, match, strconv.FormatInt(fval.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
output = strings.ReplaceAll(output, match, strconv.FormatUint(fval.Uint(), 10))
case reflect.Float32:
output = strings.ReplaceAll(output, match, strconv.FormatFloat(fval.Float(), 'f', -1, 32))
case reflect.Float64:
output = strings.ReplaceAll(output, match, strconv.FormatFloat(fval.Float(), 'f', -1, 64))
case reflect.Struct:
switch val := fval.Interface().(type) {
case time.Time:
output = strings.ReplaceAll(output, match, val.Format(refFieldLayout))
}
}
}
return &output, nil
}
func (f *field) parse(s string, v reflect.Value) error {
for _, match := range fmtRegExp.FindAllString(f.fmt, -1) {
fname := trimDelims(match)
strSubs := f.fmtRegExps[fname].FindStringSubmatch(s)
if strSubs == nil {
continue
}
str := strSubs[1]
var fval reflect.Value
var refFieldLayout string
if fname == "" {
fval = v.Field(f.index)
refFieldLayout = f.layout
} else {
fval = v.FieldByName(fname)
cache, err := f.client.cachedStruct(v.Type()) // TODO: do this lazily
if err != nil {
return err
}
vt := v.Type()
for i := 0; i < vt.NumField(); i++ {
fld := vt.Field(i)
if fld.Name == fname {
fval = v.Field(i)
ff := cache[i]
refFieldLayout = ff.layout
break
}
}
}
for fval.Kind() == reflect.Pointer {
fval = fval.Elem()
}
fty := fval.Type()
switch fty.Kind() {
case reflect.String:
fval.SetString(str)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val, err := strconv.ParseInt(str, 10, fval.Type().Bits())
if err != nil {
return err
}
fval.SetInt(val)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val, err := strconv.ParseUint(str, 10, fval.Type().Bits())
if err != nil {
return err
}
fval.SetUint(val)
case reflect.Float32, reflect.Float64:
val, err := strconv.ParseFloat(str, fval.Type().Bits())
if err != nil {
return err
}
fval.SetFloat(val)
case reflect.Struct:
switch fty {
case timeType:
t, err := time.Parse(refFieldLayout, str)
if err != nil {
return fmt.Errorf("time.Parse: %w", err)
}
fval.Set(reflect.ValueOf(t))
}
}
}
return nil
}
func (f *field) attrVal(v reflect.Value) (*dynamodb.AttributeValue, error) {
fv := v.Field(f.index)
for fv.Kind() == reflect.Pointer {
fv = fv.Elem()
}
switch f.attrType {
case "S":
s, err := f.format(v)
if err != nil {
return nil, err
}
return &dynamodb.AttributeValue{S: s}, nil
case "SS":
av := &dynamodb.AttributeValue{}
ss := fv.Interface().([]string)
av.SS = make([]*string, len(ss))
for i := range ss {
av.SS[i] = &ss[i]
}
return av, nil
}
return f.client.simpleMarshal(fv, f.layout)
}
func (f *field) unmarshal(item map[string]*dynamodb.AttributeValue, v reflect.Value) error {
fv := v.Field(f.index)
for fv.Kind() == reflect.Pointer {
if fv.IsNil() {
fv.Set(reflect.New(fv.Type().Elem()))
}
fv = fv.Elem()
}
switch f.attrType {
case "S":
if item[f.attrName] != nil && item[f.attrName].S != nil {
if err := f.parse(*item[f.attrName].S, v); err != nil {
return fmt.Errorf("parse: %s", err)
}
}
case "SS":
if item[f.attrName] != nil && item[f.attrName].SS != nil {
ssptr := item[f.attrName].SS
ss := make([]string, len(ssptr))
for i := range ssptr {
ss[i] = *ssptr[i]
}
fv.Set(reflect.ValueOf(ss))
}
default:
return f.client.simpleUnmarshal(fv, item[f.attrName], f.layout)
}
return nil
}