-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistent_dictionary.go
More file actions
208 lines (181 loc) · 6.12 KB
/
persistent_dictionary.go
File metadata and controls
208 lines (181 loc) · 6.12 KB
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
package nutty
import (
"github.com/crowdmob/goamz/dynamodb"
"strconv"
"time"
)
type PersistentModelWithDictionaryKey interface {
DictionaryKey() (string, string) // name and value
ModelName() string
SetCreatedAt(time.Time)
SetUpdatedAt(time.Time)
}
func (nuttyApp *App) AddToDynamoDB(m PersistentModelWithDictionaryKey, optionalAppName string) error {
m.SetCreatedAt(time.Now())
marshalledAttrs, err := dynamodb.MarshalAttributes(m)
if err != nil {
return err
}
_, hashKeyValue := m.DictionaryKey()
_, err = nuttyApp.DynamoDBTableForModel(m, optionalAppName).PutItem(
hashKeyValue,
"",
marshalledAttrs,
)
return err
}
func (nuttyApp *App) GetFromDynamoDB(key string, dest PersistentModelWithDictionaryKey, optionalAppName string) error {
attrs, err := nuttyApp.DynamoDBTableForModel(dest, optionalAppName).GetItem(&dynamodb.Key{HashKey: key})
if err != nil {
return err
}
err = dynamodb.UnmarshalAttributes(&attrs, dest)
return err
}
func (nuttyApp *App) ExistsInDynamoDB(key string, m PersistentModelWithDictionaryKey, optionalAppName string) (bool, error) {
attrs, err := nuttyApp.DynamoDBTableForModel(m, optionalAppName).GetItem(&dynamodb.Key{HashKey: key})
if err != nil {
return false, nil
} // treat an erroneous response as empty
hashKeyName, _ := m.DictionaryKey()
if attrs[hashKeyName] != nil {
return true, nil
}
return false, nil
}
func (nuttyApp *App) UpdateInDynamoDB(m PersistentModelWithDictionaryKey, optionalAppName string) error {
m.SetUpdatedAt(time.Now())
marshalledAttrs, err := dynamodb.MarshalAttributes(m)
if err != nil {
return err
}
// TODO once dynamodb has a proper UpdateItem we should use that instead of PutItem
_, hashKeyValue := m.DictionaryKey()
_, err = nuttyApp.DynamoDBTableForModel(m, optionalAppName).PutItem(
hashKeyValue,
"",
marshalledAttrs,
)
return err
}
func stringSetsToDynamoAttrs(attributeAppends map[string]string) []dynamodb.Attribute {
attrs := make([]dynamodb.Attribute, len(attributeAppends))
i := 0
for key, val := range attributeAppends {
attrs[i] = dynamodb.Attribute{
Type: dynamodb.TYPE_STRING_SET,
Name: key,
SetValues: []string{val},
}
i++
}
return attrs
}
func (nuttyApp *App) AppendToStringSetsInDynamoDB(m PersistentModelWithDictionaryKey, attributeAppends map[string]string, optionalAppName string) error {
_, hashKeyValue := m.DictionaryKey()
_, err := nuttyApp.DynamoDBTableForModel(m, optionalAppName).AddAttributes(
&dynamodb.Key{HashKey: hashKeyValue},
stringSetsToDynamoAttrs(attributeAppends),
)
return err
}
func (nuttyApp *App) SubtractFromStringSetsInDynamoDB(m PersistentModelWithDictionaryKey, attributeAppends map[string]string, optionalAppName string) error {
_, hashKeyValue := m.DictionaryKey()
_, err := nuttyApp.DynamoDBTableForModel(m, optionalAppName).DeleteAttributes(
&dynamodb.Key{HashKey: hashKeyValue},
stringSetsToDynamoAttrs(attributeAppends),
)
return err
}
func intsToDynamoAttrs(attributeIncrements map[string]int64) []dynamodb.Attribute {
attrs := make([]dynamodb.Attribute, len(attributeIncrements))
i := 0
for key, val := range attributeIncrements {
attrs[i] = dynamodb.Attribute{
Type: dynamodb.TYPE_NUMBER,
Name: key,
Value: strconv.FormatInt(val, 10),
}
i++
}
return attrs
}
func (nuttyApp *App) SetIntsInDynamoDB(m PersistentModelWithDictionaryKey, attributeIncrements map[string]int64, optionalAppName string) error {
_, hashKeyValue := m.DictionaryKey()
_, err := nuttyApp.DynamoDBTableForModel(m, optionalAppName).UpdateAttributes(
&dynamodb.Key{HashKey: hashKeyValue},
intsToDynamoAttrs(attributeIncrements),
)
return err
}
func (nuttyApp *App) IncrementIntsInDynamoDB(m PersistentModelWithDictionaryKey, attributeIncrements map[string]int64, optionalAppName string) error {
_, hashKeyValue := m.DictionaryKey()
_, err := nuttyApp.DynamoDBTableForModel(m, optionalAppName).AddAttributes(
&dynamodb.Key{HashKey: hashKeyValue},
intsToDynamoAttrs(attributeIncrements),
)
return err
}
func (nuttyApp *App) IncrementNumericsInDynamoDB(m PersistentModelWithDictionaryKey, intAttributeIncrements map[string]int64, floatAttributeIncrements map[string]float64, optionalAppName string) error {
attrs := make([]dynamodb.Attribute, len(intAttributeIncrements)+len(floatAttributeIncrements))
i := 0
for key, val := range floatAttributeIncrements {
attrs[i] = dynamodb.Attribute{
Type: dynamodb.TYPE_NUMBER,
Name: key,
Value: strconv.FormatFloat(val, 'f', -1, 64),
}
i++
}
for key, val := range intAttributeIncrements {
attrs[i] = dynamodb.Attribute{
Type: dynamodb.TYPE_NUMBER,
Name: key,
Value: strconv.FormatInt(val, 10),
}
i++
}
_, hashKeyValue := m.DictionaryKey()
_, err := nuttyApp.DynamoDBTableForModel(m, optionalAppName).AddAttributes(
&dynamodb.Key{HashKey: hashKeyValue},
attrs,
)
return err
}
func (nuttyApp *App) IncrementFloatsInDynamoDB(m PersistentModelWithDictionaryKey, attributeIncrements map[string]float64, optionalAppName string) error {
attrs := make([]dynamodb.Attribute, len(attributeIncrements))
i := 0
for key, val := range attributeIncrements {
attrs[i] = dynamodb.Attribute{
Type: dynamodb.TYPE_NUMBER,
Name: key,
Value: strconv.FormatFloat(val, 'f', -1, 64),
}
i++
}
_, hashKeyValue := m.DictionaryKey()
_, err := nuttyApp.DynamoDBTableForModel(m, optionalAppName).AddAttributes(
&dynamodb.Key{HashKey: hashKeyValue},
attrs,
)
return err
}
func (nuttyApp *App) RemoveFromDynamoDB(m PersistentModelWithDictionaryKey) error {
panic("RemoveFromDynamoDB Not Yet Implemented")
}
func (nuttyApp *App) DynamoDBTableForModel(m PersistentModelWithDictionaryKey, optionalAppName string) *dynamodb.Table {
hashKeyName, _ := m.DictionaryKey()
return (&dynamodb.Server{nuttyApp.AwsAuth, nuttyApp.AwsRegion}).NewTable(
nuttyApp.DynamoDBTableNameForModel(m, optionalAppName),
dynamodb.PrimaryKey{
dynamodb.NewStringAttribute(hashKeyName, ""),
nil,
},
)
}
func (nuttyApp *App) DynamoDBTableNameForModel(m PersistentModelWithDictionaryKey, optionalAppName string) string {
if optionalAppName == "" {
optionalAppName = nuttyApp.Name
}
return m.ModelName() + "s-" + optionalAppName + "-" + nuttyApp.Env
}