-
Notifications
You must be signed in to change notification settings - Fork 0
/
convenience.go
127 lines (108 loc) · 2.94 KB
/
convenience.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
package ddb
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
func DeserializeExclusiveStartKey(key string) (map[string]types.AttributeValue, error) {
if key == "" {
return nil, nil
}
jsonBytes, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return nil, fmt.Errorf("DeserializeExclusiveStartKey: StdEncoding.DecodeString %w", err)
}
outputJSON := map[string]any{}
if err := json.Unmarshal(jsonBytes, &outputJSON); err != nil {
return nil, fmt.Errorf("deserializeExclusiveStartKey: json.Unmarshal %w", err)
}
return attributevalue.MarshalMap(outputJSON)
}
func SerializeExclusiveStartKey(key map[string]types.AttributeValue) (string, error) {
if len(key) == 0 {
return "", nil
}
convertedMap := map[string]any{}
if err := attributevalue.UnmarshalMap(key, &convertedMap); err != nil {
return "", fmt.Errorf("SerializeExclusiveStartKey: attributevalue.UnmarshalMap %w", err)
}
jsonBytes, err := json.Marshal(convertedMap)
if err != nil {
return "", fmt.Errorf("SerializeExclusiveStartKey: json.Marshal %w", err)
}
return base64.StdEncoding.EncodeToString(jsonBytes), nil
}
func marshalMapList(items []any) ([]map[string]types.AttributeValue, error) {
if len(items) == 0 {
return nil, nil
}
var (
out = make([]map[string]types.AttributeValue, len(items))
err error
)
for i := range items {
out[i], err = attributevalue.MarshalMap(items[i])
if err != nil {
return nil, fmt.Errorf("MarshalMapList: MarshalMap: %w", err)
}
}
return out, nil
}
func makePuts(table string, rows ...PutRow) ([]types.Put, error) {
items := make([]types.Put, len(rows))
for i := range rows {
item, err := attributevalue.MarshalMap(rows[i].Row)
if err != nil {
return nil, fmt.Errorf("TransactionPuts: MarshalMap: %w", err)
}
items[i] = types.Put{
Item: item,
ConditionExpression: rows[i].Condition,
TableName: &table,
}
}
return items, nil
}
func makeDeletes(table string, rows ...DeleteRow) []types.Delete {
items := make([]types.Delete, len(rows))
for i := range rows {
items[i] = types.Delete{
Key: map[string]types.AttributeValue{
"PK": &types.AttributeValueMemberS{Value: rows[i].PK},
"SK": &types.AttributeValueMemberS{Value: rows[i].SK},
},
ConditionExpression: rows[i].Condition,
TableName: &table,
}
}
return items
}
func makeTransactionWriteItems(
puts []types.Put,
deletes []types.Delete,
updates []types.Update,
) []types.TransactWriteItem {
out := make([]types.TransactWriteItem, len(puts)+len(deletes)+len(updates))
i := 0
for j := range puts {
out[i] = types.TransactWriteItem{
Put: &puts[j],
}
i++
}
for j := range deletes {
out[i] = types.TransactWriteItem{
Delete: &deletes[j],
}
i++
}
for j := range updates {
out[i] = types.TransactWriteItem{
Update: &updates[j],
}
i++
}
return out
}