-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapping.go
198 lines (180 loc) · 5.2 KB
/
mapping.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
package dynamodb
import (
"encoding/base64"
"errors"
"fmt"
"log"
"net/url"
"reflect"
"strconv"
)
var urlType = reflect.TypeOf(&url.URL{})
type Mapping interface {
Register(tableName string, i interface{}) (*TableDescription, error)
ToItem(s interface{}) Item
ToKey(s interface{}) Key
FromItem(tableName string, item Item) interface{}
}
type mapping map[string]struct {
TableDescription *TableDescription
TableType reflect.Type
}
func (m mapping) Register(tableName string, i interface{}) (*TableDescription, error) {
tableType := reflect.TypeOf(i).Elem()
if t, err := m.tableFor(tableName, tableType); err == nil {
m[tableName] = struct {
TableDescription *TableDescription
TableType reflect.Type
}{TableDescription: t, TableType: tableType}
return t, nil
} else {
return nil, err
}
}
func (m mapping) tableFor(tableName string, tableType reflect.Type) (*TableDescription, error) {
var primaryHash, primaryRange *KeySchemaElement
var attributeDefinitions []AttributeDefinition
var keySchema []KeySchemaElement
provisionedThroughput := ProvisionedThroughputDescription{ReadCapacityUnits: 1, WriteCapacityUnits: 1}
for i := 0; i < tableType.NumField(); i++ {
f := tableType.Field(i)
attributeType := ""
switch f.Type {
case urlType:
attributeType = "S"
default:
switch f.Type.Kind() {
case reflect.String:
attributeType = "S"
case reflect.Int, reflect.Int64:
attributeType = "N"
case reflect.Slice:
attributeType = "B"
default:
return nil, errors.New("attribute type not supported")
}
}
name := tableType.Field(i).Name
tag := f.Tag.Get("db")
if tag == "HASH" {
attributeDefinitions = append(attributeDefinitions, AttributeDefinition{name, attributeType})
primaryHash = &KeySchemaElement{name, "HASH"}
}
if tag == "RANGE" {
attributeDefinitions = append(attributeDefinitions, AttributeDefinition{name, attributeType})
primaryRange = &KeySchemaElement{name, "RANGE"}
}
}
if primaryHash == nil {
return nil, errors.New("no primary key hash specified")
} else {
keySchema = append(keySchema, *primaryHash)
}
if primaryRange != nil {
keySchema = append(keySchema, *primaryRange)
}
return &TableDescription{TableName: tableName, KeySchema: keySchema, AttributeDefinitions: attributeDefinitions, ProvisionedThroughput: &provisionedThroughput}, nil
}
func (m mapping) ToItem(s interface{}) Item {
var it Item = make(map[string]AttributeValue)
sValue := reflect.ValueOf(s).Elem()
typeOfItem := sValue.Type()
for i := 0; i < sValue.NumField(); i++ {
f := sValue.Field(i)
name := typeOfItem.Field(i).Name
switch f.Type().Kind() {
case reflect.String:
v := f.Interface().(string)
if v != "" {
it[name] = map[string]string{"S": v}
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s := strconv.FormatInt(f.Int(), 10)
it[name] = map[string]string{"N": s}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
s := strconv.FormatUint(f.Uint(), 10)
it[name] = map[string]string{"N": s}
case reflect.Slice:
s := base64.StdEncoding.EncodeToString(f.Bytes())
it[name] = map[string]string{"B": s}
default:
panic("attribute type not supported")
}
}
return it
}
func (m mapping) ToKey(s interface{}) Key {
key := make(Key)
sType := reflect.TypeOf(s).Elem()
sValue := reflect.ValueOf(s).Elem()
for i := 0; i < sValue.NumField(); i++ {
sf := sType.Field(i)
tag := sf.Tag.Get("db")
if tag == "HASH" || tag == "RANGE" {
fv := sValue.Field(i)
switch sf.Type {
case urlType:
u := fv.Interface().(*url.URL)
key[sf.Name] = AttributeValue{"S": u.String()}
default:
switch sf.Type.Kind() {
case reflect.String:
key[sf.Name] = AttributeValue{"S": fv.Interface().(string)}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
key[sf.Name] = AttributeValue{"N": strconv.FormatInt(fv.Int(), 10)}
case reflect.Slice:
s := base64.StdEncoding.EncodeToString(fv.Bytes())
key[sf.Name] = AttributeValue{"B": s}
default:
panic("attribute type not supported")
}
}
}
}
return key
}
func (m mapping) FromItem(tableName string, item Item) interface{} {
et := m[tableName].TableType
v := reflect.New(et)
v = v.Elem()
switch v.Kind() {
case reflect.Struct:
for kk, vv := range item {
if value, ok := vv["S"]; ok {
f := v.FieldByName(kk)
switch f.Type() {
case urlType:
log.Print("We have a URL!")
if u, err := url.Parse(value); err == nil {
f.Set(reflect.ValueOf(u))
}
default:
if f.CanSet() {
f.SetString(value)
} else {
log.Println("Warning: can't set:", kk)
}
}
}
if value, ok := vv["N"]; ok {
f := v.FieldByName(kk)
n, err := strconv.ParseInt(value, 10, 64)
if err != nil || f.OverflowInt(n) {
panic(fmt.Sprintf("%v %v\n", value, v.Type()))
}
f.SetInt(n)
}
if value, ok := vv["B"]; ok {
f := v.FieldByName(kk)
bytes, err := base64.StdEncoding.DecodeString(value)
if err != nil {
panic(fmt.Sprintf("%v %v\n", value, v.Type()))
}
f.SetBytes(bytes)
}
}
default:
panic("Unsupported item type error")
}
return v.Addr().Interface()
}