-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshal.go
201 lines (161 loc) · 4.73 KB
/
marshal.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
package ddb
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
var typeOfBytes = reflect.TypeOf([]byte(nil))
// Marshal converts map or struct to dynamodb attribute value
func Marshal(iv interface{}) map[string]*dynamodb.AttributeValue {
kind := reflect.TypeOf(iv).Kind()
if kind == reflect.Map {
return marshalMap(reflect.ValueOf(iv))
} else if kind == reflect.Ptr {
return marshalStruct(reflect.ValueOf(iv).Elem())
}
return nil
}
func marshalMap(value reflect.Value) map[string]*dynamodb.AttributeValue {
ret := make(map[string]*dynamodb.AttributeValue)
for _, keyValue := range value.MapKeys() {
if keyValue.Type().Kind() == reflect.String {
ret[keyValue.String()] = marshalValue(value.MapIndex(keyValue))
} else {
panic("map key must be string")
}
}
return ret
}
func marshalStruct(value reflect.Value) map[string]*dynamodb.AttributeValue {
t := value.Type()
numField := t.NumField()
ret := make(map[string]*dynamodb.AttributeValue)
for i := 0; i < numField; i++ {
f := t.Field(i)
if f.PkgPath != "" {
continue
}
name, option := parseTag(f.Tag.Get("json"))
if name == "-" {
continue
}
if option == "omitifempty" {
continue
}
if name == "" {
name = f.Name
}
ret[name] = marshalValue(value.FieldByIndex(f.Index))
}
return ret
}
func parseTag(tag string) (string, string) {
if idx := strings.Index(tag, ","); idx != -1 {
return tag[:idx], tag[idx+1:]
}
return tag, ""
}
func marshalValue(value reflect.Value) *dynamodb.AttributeValue {
switch value.Type().Kind() {
case reflect.String:
return marshalStringValue(value)
case reflect.Bool:
return marshalBoolValue(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return marshalInt64Value(value)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return marshalUint64Value(value)
case reflect.Float32, reflect.Float64:
return marshalFloat64Value(value)
case reflect.Array:
return marshalArrayValue(value)
case reflect.Interface:
return marshalInterfaceValue(value)
case reflect.Map:
return marshalMapValue(value)
case reflect.Ptr:
return marshalPtrValue(value)
case reflect.Slice:
if value.Type() == typeOfBytes {
return marshalBytesValue(value)
}
return marshalSliceValue(value)
case reflect.Struct:
return marshalStructValue(value)
case reflect.UnsafePointer:
return nil
}
return nil
}
func marshalStringValue(value reflect.Value) *dynamodb.AttributeValue {
str := value.String()
if str == "" {
return makeNullAttrValue()
}
return makeStringAttrValue(str)
}
func makeNullAttrValue() *dynamodb.AttributeValue {
return &dynamodb.AttributeValue{NULL: aws.Bool(true)}
}
func makeStringAttrValue(str string) *dynamodb.AttributeValue {
return &dynamodb.AttributeValue{S: aws.String(str)}
}
func marshalBoolValue(value reflect.Value) *dynamodb.AttributeValue {
return &dynamodb.AttributeValue{BOOL: aws.Bool(value.Bool())}
}
func marshalInt64Value(value reflect.Value) *dynamodb.AttributeValue {
return makeNumberAttrValue(fmt.Sprintf("%d", value.Int()))
}
func marshalUint64Value(value reflect.Value) *dynamodb.AttributeValue {
return makeNumberAttrValue(fmt.Sprintf("%d", value.Uint()))
}
func marshalFloat64Value(value reflect.Value) *dynamodb.AttributeValue {
return makeNumberAttrValue(strconv.FormatFloat(value.Float(), 'f', -1, 64))
}
func makeNumberAttrValue(str string) *dynamodb.AttributeValue {
return &dynamodb.AttributeValue{N: aws.String(str)}
}
func marshalArrayValue(value reflect.Value) *dynamodb.AttributeValue {
length := value.Len()
list := make([]*dynamodb.AttributeValue, length)
for i := 0; i < length; i++ {
list[i] = marshalValue(value.Index(i))
}
return &dynamodb.AttributeValue{L: list}
}
func marshalMapValue(value reflect.Value) *dynamodb.AttributeValue {
if value.IsNil() {
return makeNullAttrValue()
}
return &dynamodb.AttributeValue{M: marshalMap(value)}
}
func marshalInterfaceValue(value reflect.Value) *dynamodb.AttributeValue {
if value.IsNil() {
return makeNullAttrValue()
}
return marshalValue(value.Elem())
}
func marshalPtrValue(value reflect.Value) *dynamodb.AttributeValue {
if value.IsNil() {
return makeNullAttrValue()
}
return marshalValue(value.Elem())
}
func marshalBytesValue(value reflect.Value) *dynamodb.AttributeValue {
if value.IsNil() {
return makeNullAttrValue()
}
return &dynamodb.AttributeValue{B: value.Bytes()}
}
func marshalSliceValue(value reflect.Value) *dynamodb.AttributeValue {
if value.IsNil() {
return makeNullAttrValue()
}
return marshalArrayValue(value)
}
func marshalStructValue(value reflect.Value) *dynamodb.AttributeValue {
return &dynamodb.AttributeValue{M: marshalStruct(value)}
}