forked from guregu/dynamo
/
encode.go
213 lines (191 loc) · 5.25 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package dynamo
import (
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/gunosy/aws-sdk-go/aws"
"github.com/gunosy/aws-sdk-go/service/dynamodb"
)
type Marshaler interface {
MarshalDynamo() (*dynamodb.AttributeValue, error)
}
func marshalStruct(v interface{}) (map[string]*dynamodb.AttributeValue, error) {
item := make(map[string]*dynamodb.AttributeValue)
var err error
rv := reflect.ValueOf(v)
if rv.Type().Kind() != reflect.Struct {
if rv.Type().Kind() == reflect.Ptr {
return marshalStruct(rv.Elem().Interface())
}
return nil, fmt.Errorf("marshal struct invalid type: %T (%+v)", v, v)
}
for i := 0; i < rv.Type().NumField(); i++ {
field := rv.Type().Field(i)
fv := rv.Field(i)
name, special := fieldName(field)
switch {
case !fv.CanInterface():
continue
case name == "-":
continue
case special == "omitempty",
field.Type.Kind() == reflect.String: // automatically omit empty strings
if isZero(fv) {
continue
}
}
av, err := marshal(fv.Interface())
if err != nil {
return nil, err
}
item[name] = av
}
return item, err
}
func marshal(v interface{}) (*dynamodb.AttributeValue, error) {
switch x := v.(type) {
case Marshaler:
return x.MarshalDynamo()
case encoding.TextMarshaler:
text, err := x.MarshalText()
if err != nil {
return nil, err
}
if len(text) == 0 {
return &dynamodb.AttributeValue{NULL: aws.Bool(true)}, nil
}
return &dynamodb.AttributeValue{S: aws.String(string(text))}, err
case []byte:
return &dynamodb.AttributeValue{B: x}, nil
case [][]byte:
return &dynamodb.AttributeValue{BS: x}, nil
case bool:
return &dynamodb.AttributeValue{BOOL: aws.Bool(x)}, nil
case int:
return &dynamodb.AttributeValue{N: aws.String(strconv.Itoa(x))}, nil
case int64:
return &dynamodb.AttributeValue{N: aws.String(strconv.FormatInt(x, 10))}, nil
case int32:
return &dynamodb.AttributeValue{N: aws.String(strconv.FormatInt(int64(x), 10))}, nil
case int16:
return &dynamodb.AttributeValue{N: aws.String(strconv.FormatInt(int64(x), 10))}, nil
case int8:
return &dynamodb.AttributeValue{N: aws.String(strconv.FormatInt(int64(x), 10))}, nil
case byte:
return &dynamodb.AttributeValue{N: aws.String(strconv.FormatInt(int64(x), 10))}, nil
case float64:
return &dynamodb.AttributeValue{N: aws.String(strconv.FormatFloat(x, 'f', -1, 64))}, nil
case float32:
return &dynamodb.AttributeValue{N: aws.String(strconv.FormatFloat(float64(x), 'f', -1, 32))}, nil
case nil:
return &dynamodb.AttributeValue{NULL: aws.Bool(true)}, nil
case string:
return &dynamodb.AttributeValue{S: aws.String(x)}, nil
case *string:
return &dynamodb.AttributeValue{S: x}, nil
case []string:
if len(x) == 0 {
return &dynamodb.AttributeValue{NULL: aws.Bool(true)}, nil
}
// why are these pointers amazon seriously
strptrs := make([]*string, 0, len(x))
for _, s := range x {
s := s
strptrs = append(strptrs, &s)
}
return &dynamodb.AttributeValue{SS: strptrs}, nil
case []*string:
if len(x) == 0 {
return &dynamodb.AttributeValue{NULL: aws.Bool(true)}, nil
}
return &dynamodb.AttributeValue{SS: x}, nil
default:
return marshalReflect(reflect.ValueOf(x))
}
}
func marshalReflect(rv reflect.Value) (*dynamodb.AttributeValue, error) {
// TODO: byte arrays and array of arrays
// TODO: other kinds of arrays
// TODO: structs
// TODO: maps
switch rv.Kind() {
case reflect.Ptr:
if rv.IsNil() {
return &dynamodb.AttributeValue{NULL: aws.Bool(true)}, nil
} else {
return marshal(rv.Elem().Interface())
}
case reflect.Bool:
return &dynamodb.AttributeValue{BOOL: aws.Bool(rv.Bool())}, nil
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
return &dynamodb.AttributeValue{N: aws.String(strconv.FormatInt(rv.Int(), 10))}, nil
case reflect.String:
return &dynamodb.AttributeValue{S: aws.String(rv.String())}, nil
default:
return nil, fmt.Errorf("dynamo marshal: unknown type %s", rv.Type().String())
}
}
func marshalSlice(values []interface{}) ([]*dynamodb.AttributeValue, error) {
avs := make([]*dynamodb.AttributeValue, 0, len(values))
for _, v := range values {
av, err := marshal(v)
if err != nil {
return nil, err
}
avs = append(avs, av)
}
return avs, nil
}
func fieldName(field reflect.StructField) (name, special string) {
name = field.Tag.Get("dynamo")
switch name {
case "":
// no tag, use the field name
name = field.Name
default:
if idx := strings.IndexRune(name, ','); idx != -1 {
special = name[idx+1:]
if idx > 0 {
name = name[:idx]
} else {
name = field.Name
}
}
}
return
}
type isZeroer interface {
IsZero() bool
}
// thanks James Henstridge
// TODO: tweak
// TODO: IsZero() interface support
func isZero(rv reflect.Value) bool {
// use IsZero for supported types
if rv.CanInterface() {
if zeroer, ok := rv.Interface().(isZeroer); ok {
return zeroer.IsZero()
}
}
switch rv.Kind() {
case reflect.Func, reflect.Map, reflect.Slice:
return rv.IsNil()
case reflect.Array:
z := true
for i := 0; i < rv.Len(); i++ {
z = z && isZero(rv.Index(i))
}
return z
case reflect.Struct:
z := true
for i := 0; i < rv.NumField(); i++ {
z = z && isZero(rv.Field(i))
}
return z
}
// Compare other types directly:
z := reflect.Zero(rv.Type())
return rv.Interface() == z.Interface()
}