-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_get.go
120 lines (111 loc) · 2.92 KB
/
op_get.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
package rotor
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
// GetOptions GetOptions
type GetOptions struct {
builder *expression.Builder
consistentRead *bool
}
func defaultGetOptions() *GetOptions {
return &GetOptions{}
}
// GetOption GetOption
type GetOption func(options *GetOptions)
// GetProjection GetProjection
func GetProjection(projection expression.ProjectionBuilder) GetOption {
return func(options *GetOptions) {
if options.builder == nil {
builder := expression.NewBuilder()
options.builder = &builder
}
*options.builder = options.builder.WithProjection(projection)
}
}
// GetConsistent GetConsistent
func GetConsistent(strong bool) GetOption {
return func(options *GetOptions) {
options.consistentRead = aws.Bool(strong)
}
}
// Get get item
// Item not found will return error
func (rs *Service) Get(ctx context.Context, key PrimaryKeyType, out interface{}, opts ...GetOption) error {
options := defaultGetOptions()
for _, opt := range opts {
opt(options)
}
var expr expression.Expression
var err error
if options.builder != nil {
expr, err = options.builder.Build()
if err != nil {
return err
}
}
input := &dynamodb.GetItemInput{
TableName: rs.tableName,
Key: key,
ConsistentRead: options.consistentRead,
ProjectionExpression: expr.Projection(),
ExpressionAttributeNames: expr.Names(),
}
ret, err := rs.dynamo.GetItemWithContext(ctx, input)
if err != nil {
return err
}
if ret.Item == nil {
return ErrItemNotFound
}
return rs.codec.UnmarshalMap(ret.Item, out)
}
// GetBatch get items
func (rs *Service) GetBatch(ctx context.Context, keys []PrimaryKeyType, out interface{}, opts ...GetOption) error {
if len(keys) == 0 || len(keys) > maxReadNum {
return ErrInput
}
options := defaultGetOptions()
for _, opt := range opts {
opt(options)
}
var expr expression.Expression
var err error
if options.builder != nil {
expr, err = options.builder.Build()
if err != nil {
return err
}
}
input := &dynamodb.BatchGetItemInput{
RequestItems: map[string]*dynamodb.KeysAndAttributes{
rs.TableName(): {
Keys: keys,
ProjectionExpression: expr.Projection(),
ExpressionAttributeNames: expr.Names(),
ConsistentRead: options.consistentRead,
},
},
}
var pageErr error
allItems := []map[string]*dynamodb.AttributeValue{}
err = rs.dynamo.BatchGetItemPagesWithContext(ctx, input, func(output *dynamodb.BatchGetItemOutput, b bool) bool {
if len(output.UnprocessedKeys) > 0 {
pageErr = ErrBatchGetPage
return false
}
if items, ok := output.Responses[rs.TableName()]; ok {
allItems = append(allItems, items...)
}
return true
})
if err != nil {
return err
}
if pageErr != nil {
return pageErr
}
return rs.codec.UnmarshalListOfMaps(allItems, out)
}