/
attribute_map.go
215 lines (183 loc) · 6.14 KB
/
attribute_map.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
package dynamodbtk
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
type DynamoDBClient interface {
GetItem(context.Context, *dynamodb.GetItemInput, ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
PutItem(context.Context, *dynamodb.PutItemInput, ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
ListTables(context.Context, *dynamodb.ListTablesInput, ...func(*dynamodb.Options)) (*dynamodb.ListTablesOutput, error)
Scan(context.Context, *dynamodb.ScanInput, ...func(*dynamodb.Options)) (*dynamodb.ScanOutput, error)
UpdateItem(context.Context, *dynamodb.UpdateItemInput, ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
}
/*
If the tag is not present for the field, the field will serialize to an
"S"-typed column named the same as the struct field.
If the tag is present, the converter will look for exactly 1 or 2
comma-delimited fields in the tag.
The first part is the DDB column name. The second part is the column
type.
If the entire tag is "-", the field is ignored.
If the column name is "-", the default column name is used instead.
If the tag type is "", the default is used instead.
If the tag type is neither "S" nor "N", it is an error and the field is
ignored
Returns: shouldInclude, ddbName, ddbType
*/
func extractDdbTags(fieldSpec reflect.StructField) (bool, string, string) {
ddbName := fieldSpec.Name
ddbType := "S"
ddbTag := fieldSpec.Tag.Get("ddb")
parts := strings.Split(ddbTag, ",")
if len(parts) > 2 {
return false, "", ""
}
if len(parts) == 0 {
return true, ddbName, ddbType
}
val1 := strings.TrimSpace(parts[0])
if len(parts) == 1 && val1 == "-" {
return false, "", ""
}
if val1 != "" && val1 != "-" {
ddbName = val1
}
if len(parts) == 2 {
val2 := strings.TrimSpace(parts[1])
if val2 != "" {
ddbType = val2
}
if ddbType != "S" && ddbType != "N" {
return false, "", ""
}
}
return true, ddbName, ddbType
}
func StructToAttributeMap(input interface{}) map[string]types.AttributeValue {
inputValue := reflect.ValueOf(input)
inputType := inputValue.Type()
am := map[string]types.AttributeValue{}
for i := 0; i < inputType.NumField(); i++ {
f := inputType.Field(i)
fieldValue := inputValue.Field(i)
var strValue string
switch {
case fieldValue.CanInt():
strValue = strconv.FormatInt(fieldValue.Int(), 10)
case fieldValue.CanUint():
strValue = strconv.FormatUint(fieldValue.Uint(), 10)
case fieldValue.CanFloat():
strValue = strconv.FormatFloat(fieldValue.Float(), 'E', -1, 64)
case fieldValue.Kind() == reflect.String:
strValue = fieldValue.String()
case fieldValue.Kind() == reflect.Bool:
var intVal int64 = 0
if fieldValue.Bool() {
intVal = 1
}
strValue = strconv.FormatInt(intVal, 10)
default:
panic(fmt.Sprintf("unsupported kind %v for field %q", fieldValue.Kind(), f.Name))
}
fieldSpec := inputType.Field(i)
shouldInclude, ddbName, ddbType := extractDdbTags(fieldSpec)
if !shouldInclude {
continue
}
var v types.AttributeValue
switch ddbType {
case "N":
v = &types.AttributeValueMemberN{Value: strValue}
case "S":
v = &types.AttributeValueMemberS{Value: strValue}
}
am[ddbName] = v
}
return am
}
func AttributeMapToStruct(am map[string]types.AttributeValue, out interface{}) error {
outPtrValue := reflect.ValueOf(out)
if outPtrValue.Kind() != reflect.Pointer {
panic(fmt.Sprintf("output value must be a pointer-to-struct, but got %v", outPtrValue.Kind()))
}
outValue := outPtrValue.Elem()
if outValue.Kind() != reflect.Struct {
panic(fmt.Sprintf("output value must be a pointer-to-struct, but got %v", outPtrValue.Kind()))
}
outType := outValue.Type()
type spec struct {
fieldName string
ddbType string
index int
}
fieldSpecs := map[string]spec{}
for i := 0; i < outType.NumField(); i++ {
fieldSpec := outType.Field(i)
shouldInclude, ddbName, ddbType := extractDdbTags(fieldSpec)
if !shouldInclude {
continue
}
fieldSpecs[ddbName] = spec{fieldSpec.Name, ddbType, i}
}
for colName, ddbValue := range am {
fieldSpec, hasField := fieldSpecs[colName]
if !hasField {
panic(fmt.Sprintf("no struct field matching ddb column name %s", colName))
}
var strValue string
switch fieldSpec.ddbType {
case "N":
strValue = ddbValue.(*types.AttributeValueMemberN).Value
case "S":
strValue = ddbValue.(*types.AttributeValueMemberS).Value
}
fieldValue := outValue.Field(fieldSpec.index)
switch {
case fieldValue.CanInt():
finalValue, err := strconv.ParseInt(strValue, 10, 64)
if err != nil {
return fmt.Errorf("unable to parse int value from %q for field %q", strValue, colName)
}
if fieldValue.OverflowInt(finalValue) {
return fmt.Errorf("int value %d is too big for field %q type %v", finalValue, colName, fieldValue.Kind())
}
fieldValue.SetInt(finalValue)
case fieldValue.CanUint():
finalValue, err := strconv.ParseUint(strValue, 10, 64)
if err != nil {
return fmt.Errorf("unable to parse uint value from %q for field %q", strValue, colName)
}
if fieldValue.OverflowUint(finalValue) {
return fmt.Errorf("uint value %d is too big for field %q type %v", finalValue, colName, fieldValue.Kind())
}
fieldValue.SetUint(finalValue)
case fieldValue.CanFloat():
finalValue, err := strconv.ParseFloat(strValue, 64)
if err != nil {
return fmt.Errorf("unable to parse float value from %q for field %q", strValue, colName)
}
if fieldValue.OverflowFloat(finalValue) {
return fmt.Errorf("float value %f is too big for field %q type %v", finalValue, colName, fieldValue.Kind())
}
fieldValue.SetFloat(finalValue)
case fieldValue.Kind() == reflect.String:
finalValue := strValue
fieldValue.SetString(finalValue)
case fieldValue.Kind() == reflect.Bool:
intValue, err := strconv.ParseInt(strValue, 10, 64)
if err != nil {
return fmt.Errorf("unable to parse int (bool) value from %q for field %q", strValue, colName)
}
finalValue := intValue > 0
fieldValue.SetBool(finalValue)
default:
panic(fmt.Sprintf("unsupported kind %v for field %q", fieldValue.Kind(), colName))
}
}
return nil
}