-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
encode.go
199 lines (172 loc) · 4.27 KB
/
encode.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
package dynamo
import (
"encoding"
"reflect"
"strconv"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// Marshaler is the interface implemented by objects that can marshal themselves into
// an AttributeValue.
type Marshaler interface {
MarshalDynamo() (types.AttributeValue, error)
}
// ItemMarshaler is the interface implemented by objects that can marshal themselves
// into an Item (a map of strings to AttributeValues).
type ItemMarshaler interface {
MarshalDynamoItem() (Item, error)
}
// MarshalItem converts the given struct into a DynamoDB item.
func MarshalItem(v interface{}) (Item, error) {
return marshalItem(v)
}
func marshalItem(v interface{}) (Item, error) {
rv := reflect.ValueOf(v)
rt := rv.Type()
plan, err := typedefOf(rt)
if err != nil {
return nil, err
}
return plan.encodeItem(rv)
}
// Marshal converts the given value into a DynamoDB attribute value.
func Marshal(v interface{}) (types.AttributeValue, error) {
return marshal(v, flagNone)
}
func marshal(v interface{}, flags encodeFlags) (types.AttributeValue, error) {
rv := reflect.ValueOf(v)
if !rv.IsValid() {
return nil, nil
}
rt := rv.Type()
def, err := typedefOf(rt)
if err != nil {
return nil, err
}
enc, err := def.encodeType(rt, flags, nil)
if err != nil {
return nil, err
}
rv = indirectPtrNoAlloc(rv)
if !rv.IsValid() {
return nil, nil
}
return enc(rv, flags)
}
func marshalSliceNoOmit(values []interface{}) ([]types.AttributeValue, error) {
avs := make([]types.AttributeValue, 0, len(values))
for _, v := range values {
av, err := marshal(v, flagAllowEmpty)
if err != nil {
return nil, err
}
avs = append(avs, av)
}
return avs, nil
}
func encodeItem(fields []structField, rv reflect.Value) (Item, error) {
item := make(Item, len(fields))
for _, field := range fields {
fv := dig(rv, field.index)
if !fv.IsValid() {
// TODO: encode NULL?
continue
}
if field.flags&flagOmitEmpty != 0 && field.isZero != nil {
if field.isZero(fv) {
continue
}
}
if field.enc == nil {
continue
}
av, err := field.enc(fv, field.flags)
if err != nil {
return nil, err
}
if av == nil {
if field.flags&flagNull != 0 {
item[field.name] = nullAV
}
continue
}
item[field.name] = av
}
return item, nil
}
type isZeroer interface {
IsZero() bool
}
func (def *typedef) isZeroFunc(rt reflect.Type) func(rv reflect.Value) bool {
if rt.Implements(rtypeIsZeroer) {
return isZeroIface(rt, func(v isZeroer) bool {
return v.IsZero()
})
}
// simplified check for certain interfaces
// their output will be checked during encoding process
switch {
case rt.Implements(rtypeMarshaler):
return isZeroIface(rt, func(v Marshaler) bool {
return false
})
case rt.Implements(rtypeTextMarshaler):
return isZeroIface(rt, func(v encoding.TextMarshaler) bool {
return false
})
}
switch rt.Kind() {
case reflect.Map, reflect.Slice:
return isNil
case reflect.Array:
return def.isZeroArray(rt)
case reflect.Struct:
return def.isZeroStruct(rt)
}
return isZeroValue
}
func isZeroIface[T any](rt reflect.Type, isZero func(v T) bool) func(rv reflect.Value) bool {
ifaceType := reflect.TypeOf((*T)(nil)).Elem()
// use IsZero for supported types
if (rt.Kind() == reflect.Pointer && rt.Elem().Implements(ifaceType)) || rt.Kind() == reflect.Interface {
// avoid calling IsZero if it would panic
return func(rv reflect.Value) bool {
if rv.IsNil() || !rv.CanInterface() {
return true
}
return isZero(rv.Interface().(T))
}
}
return func(rv reflect.Value) bool {
if !rv.CanInterface() {
return true
}
return isZero(rv.Interface().(T))
}
}
func (def *typedef) isZeroStruct(rt reflect.Type) func(rv reflect.Value) bool {
if fn := def.info.findZero(rt); fn != nil {
return fn
}
child, _ := def.structInfo(rt, def.info)
return child.isZero
}
func (def *typedef) isZeroArray(rt reflect.Type) func(reflect.Value) bool {
elemIsZero := def.isZeroFunc(rt.Elem())
return func(rv reflect.Value) bool {
for i := 0; i < rv.Len(); i++ {
if !elemIsZero(rv.Index(i)) {
return false
}
}
return true
}
}
func isZeroValue(rv reflect.Value) bool {
return rv.IsZero()
}
func isNil(rv reflect.Value) bool {
return rv.IsNil()
}
func formatFloat(f float64, _ int) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}