-
Notifications
You must be signed in to change notification settings - Fork 0
/
putitem.go
102 lines (92 loc) · 2.48 KB
/
putitem.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
package dynago
import (
"fmt"
"reflect"
"time"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// PutItem represents a PutItem operation.
type PutItem struct {
item Keyer
input *dynamodb.PutItemInput
dynago *Dynago
err error
}
// PutItem returns a PutItem operation.
func (d *Dynago) PutItem(item Keyer) *PutItem {
return &PutItem{
input: &dynamodb.PutItemInput{TableName: &d.config.DefaultTableName},
dynago: d,
item: item,
}
}
// TableName sets the table.
func (q *PutItem) TableName(table string) *PutItem {
q.input.TableName = &table
return q
}
// ConditionExpression sets the ConditionExpression.
func (q *PutItem) ConditionExpression(exp string) *PutItem {
q.input.ConditionExpression = &exp
return q
}
// ExpressionAttributeValue sets an ExpressionAttributeValue.
func (q *PutItem) ExpressionAttributeValue(key string, val interface{}, layout ...string) *PutItem {
if q.input.ExpressionAttributeValues == nil {
q.input.ExpressionAttributeValues = make(map[string]*dynamodb.AttributeValue)
}
lay := time.RFC3339
if len(layout) > 0 {
lay = layout[0]
}
av, err := q.dynago.simpleMarshal(reflect.ValueOf(val), lay)
if err != nil {
q.err = err
}
q.input.ExpressionAttributeValues[key] = av
return q
}
// ExpressionAttributeName sets an ExpressionAttributeName.
func (q *PutItem) ExpressionAttributeName(name string, sub string) *PutItem {
if q.input.ExpressionAttributeNames == nil {
q.input.ExpressionAttributeNames = make(map[string]*string)
}
q.input.ExpressionAttributeNames[name] = &sub
return q
}
// Exec exeutes the operation.
func (q *PutItem) Exec() error {
if q.err != nil {
return q.err
}
var err error
q.input.Item, err = q.dynago.Marshal(q.item)
if err != nil {
return fmt.Errorf("q.dynago.Marshal: %w", err)
}
_, err = q.dynago.ddb.PutItem(q.input)
if err != nil {
return fmt.Errorf("d.ddb.PutItem: %w", err)
}
return err
}
// TransactionWriteItem implements the TransactionWriteItemer
// interface.
func (q *PutItem) TransactionWriteItem() (*dynamodb.TransactWriteItem, error) {
if q.err != nil {
return nil, q.err
}
item, err := q.dynago.Marshal(q.item)
if err != nil {
return nil, err
}
return &dynamodb.TransactWriteItem{
Put: &dynamodb.Put{
Item: item,
TableName: q.input.TableName,
ConditionExpression: q.input.ConditionExpression,
ExpressionAttributeNames: q.input.ExpressionAttributeNames,
ExpressionAttributeValues: q.input.ExpressionAttributeValues,
},
}, nil
}