-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsdk_attribute_value.go
251 lines (226 loc) · 5.61 KB
/
sdk_attribute_value.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
// DynamoDB utility
package dynamodb
import (
"fmt"
"reflect"
"strconv"
SDK "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/evalphobia/aws-sdk-go-wrapper/private/pointers"
)
func newAttributeValue(typ string, val interface{}) *SDK.AttributeValue {
switch typ {
case AttributeTypeString:
return newAttributeValueS(val)
case AttributeTypeNumber:
return newAttributeValueN(val)
case AttributeTypeBinary:
return newAttributeValueB(val)
case AttributeTypeBool:
return newAttributeValueBOOL(val)
case AttributeTypeStringSet:
return newAttributeValueSS(val)
case AttributeTypeNumberSet:
return newAttributeValueNS(val)
case AttributeTypeBinarySet:
return newAttributeValueBS(val)
case AttributeTypeList:
return newAttributeValueL(val)
case AttributeTypeMap:
return newAttributeValueM(val)
case AttributeTypeNull:
return newAttributeValueNull(val)
}
return nil
}
func newAttributeValueS(val interface{}) *SDK.AttributeValue {
return &SDK.AttributeValue{S: pointers.String(fmt.Sprint(val))}
}
func newAttributeValueN(val interface{}) *SDK.AttributeValue {
return &SDK.AttributeValue{N: pointers.String(fmt.Sprint(val))}
}
func newAttributeValueB(val interface{}) *SDK.AttributeValue {
switch t := val.(type) {
case []byte:
return &SDK.AttributeValue{B: t}
}
return nil
}
func newAttributeValueBOOL(val interface{}) *SDK.AttributeValue {
switch t := val.(type) {
case bool:
return &SDK.AttributeValue{BOOL: pointers.Bool(t)}
}
return nil
}
func newAttributeValueSS(val interface{}) *SDK.AttributeValue {
switch t := val.(type) {
case []string:
return &SDK.AttributeValue{SS: createPointerSliceString(t)}
}
return nil
}
func newAttributeValueNS(val interface{}) *SDK.AttributeValue {
return &SDK.AttributeValue{NS: MarshalStringSlice(val)}
}
func newAttributeValueBS(val interface{}) *SDK.AttributeValue {
switch t := val.(type) {
case [][]byte:
return &SDK.AttributeValue{BS: t}
}
return nil
}
func newAttributeValueM(val interface{}) *SDK.AttributeValue {
v, ok := val.(map[string]interface{})
if !ok {
return nil
}
return &SDK.AttributeValue{M: Marshal(v)}
}
func newAttributeValueL(val interface{}) *SDK.AttributeValue {
// TODO: implement...
values, ok := val.([]interface{})
if !ok {
return nil
}
var list []*SDK.AttributeValue
for _, v := range values {
list = append(list, createAttributeValue(v))
}
return &SDK.AttributeValue{L: list}
}
func newAttributeValueNull(val interface{}) *SDK.AttributeValue {
switch t := val.(type) {
case bool:
return &SDK.AttributeValue{NULL: pointers.Bool(t)}
}
return nil
}
// Create new AttributeValue from the type of value
func createAttributeValue(v interface{}) *SDK.AttributeValue {
switch t := v.(type) {
case string:
return &SDK.AttributeValue{
S: pointers.String(t),
}
case int, int32, int64, uint, uint32, uint64, float32, float64:
return &SDK.AttributeValue{
N: pointers.String(fmt.Sprint(t)),
}
case []byte:
return &SDK.AttributeValue{
B: t,
}
case bool:
return &SDK.AttributeValue{
BOOL: pointers.Bool(t),
}
case []string:
return &SDK.AttributeValue{
SS: createPointerSliceString(t),
}
case [][]byte:
return &SDK.AttributeValue{
BS: t,
}
case []int, []int32, []int64, []uint, []uint32, []uint64, []float32, []float64:
return &SDK.AttributeValue{
NS: MarshalStringSlice(t),
}
case []map[string]interface{}:
return &SDK.AttributeValue{
L: createPointerMap(v.([]map[string]interface{})),
}
}
k := reflect.ValueOf(v)
switch {
case k.Kind() == reflect.Map:
return &SDK.AttributeValue{
M: Marshal(v.(map[string]interface{})),
}
}
return &SDK.AttributeValue{}
}
func createPointerMap(values []map[string]interface{}) []*SDK.AttributeValue {
var p []*SDK.AttributeValue
for _, val := range values {
p = append(p, &SDK.AttributeValue{
M: Marshal(val),
})
}
return p
}
func createPointerSliceString(values []string) []*string {
var p []*string
for _, v := range values {
str := v
p = append(p, &str)
}
return p
}
// Retrieve value from DynamoDB type
func getItemValue(val *SDK.AttributeValue) interface{} {
switch {
case val.N != nil:
data, _ := strconv.Atoi(*val.N)
return data
case val.S != nil:
return *val.S
case val.BOOL != nil:
return *val.BOOL
case len(val.B) > 0:
return val.B
case len(val.M) > 0:
return UnmarshalAttributeValue(val.M)
case len(val.NS) > 0:
var data []*int
for _, vString := range val.NS {
vInt, _ := strconv.Atoi(*vString)
data = append(data, &vInt)
}
return data
case len(val.SS) > 0:
return val.SS
case len(val.BS) > 0:
return val.BS
case len(val.L) > 0:
var data []interface{}
for _, v := range val.L {
data = append(data, getItemValue(v))
}
return data
}
return nil
}
// UnmarshalAttributeValue converts DynamoDB Item to map data.
func UnmarshalAttributeValue(item map[string]*SDK.AttributeValue) map[string]interface{} {
data := make(map[string]interface{})
if item == nil {
return data
}
for key, val := range item {
data[key] = getItemValue(val)
}
return data
}
// Marshal converts map data to DynamoDB Item data.
func Marshal(item map[string]interface{}) map[string]*SDK.AttributeValue {
data := make(map[string]*SDK.AttributeValue)
for key, val := range item {
data[key] = createAttributeValue(val)
}
return data
}
// MarshalStringSlice converts string slice to DynamoDB Item data.
func MarshalStringSlice(item interface{}) []*string {
var data []*string
switch reflect.TypeOf(item).Kind() {
case reflect.Slice:
val := reflect.ValueOf(item)
max := val.Len()
for i := 0; i < max; i++ {
s := fmt.Sprint(val.Index(i).Interface())
data = append(data, &s)
}
}
return data
}