-
Notifications
You must be signed in to change notification settings - Fork 0
/
getitem.go
74 lines (64 loc) · 1.72 KB
/
getitem.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
package dynago
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// ErrItemNotFound is returned when the item is not found.
var ErrItemNotFound = errors.New("item not found")
// GetItem represents a GetItem operation.
type GetItem struct {
item Keyer
input *dynamodb.GetItemInput
dynago *Dynago
}
// GetItem returns a GetItem operation.
func (d *Dynago) GetItem(item Keyer) *GetItem {
return &GetItem{
input: &dynamodb.GetItemInput{
ConsistentRead: &d.config.DefaultConsistentRead,
TableName: &d.config.DefaultTableName,
},
item: item,
dynago: d,
}
}
// TableName sets the table.
func (q *GetItem) TableName(name string) *GetItem {
q.input.TableName = &name
return q
}
// ProjectionExpression sets the ProjectionExpression.
func (q *GetItem) ProjectionExpression(exp string) *GetItem {
q.input.ProjectionExpression = &exp
return q
}
// ExpressionAttributeName sets an ExpressionAttributeName.
func (q *GetItem) ExpressionAttributeName(name string, sub string) *GetItem {
if q.input.ExpressionAttributeNames == nil {
q.input.ExpressionAttributeNames = make(map[string]*string)
}
q.input.ExpressionAttributeNames[name] = &sub
return q
}
// ConsistentRead sets ConsistentRead.
func (q *GetItem) ConsistentRead(consisten bool) *GetItem {
q.input.ConsistentRead = &consisten
return q
}
// Exec exeutes the operation.
func (q *GetItem) Exec() error {
var err error
q.input.Key, err = q.dynago.key(q.item)
if err != nil {
return fmt.Errorf("q.dynago.key: %w", err)
}
output, err := q.dynago.ddb.GetItem(q.input)
if err != nil {
return fmt.Errorf("d.ddb.GetItem: %w", err)
}
if len(output.Item) == 0 {
return ErrItemNotFound
}
return q.dynago.Unmarshal(output.Item, q.item)
}