/
client_op_item_batch_get_item.go
88 lines (71 loc) · 2.36 KB
/
client_op_item_batch_get_item.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
package dynamodb
import (
"context"
"fmt"
SDK "github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors"
)
// BatchGetItem executes `BatchGetItem` operation.
func (svc *DynamoDB) BatchGetItem(ctx context.Context, r BatchGetItemRequest) (*BatchGetItemResult, error) {
out, err := svc.RawBatchGetItem(ctx, r.ToInput())
if err == nil {
return NewBatchGetItemResult(out), nil
}
err = svc.errWrap(errors.ErrorData{
Err: err,
AWSOperation: "BatchGetItem",
})
svc.Errorf(err.Error())
return nil, err
}
// BatchGetItemRequest has parameters for `BatchGetItem` operation.
type BatchGetItemRequest struct {
RequestItems map[string]KeysAndAttributes
// optional
ReturnConsumedCapacity ReturnConsumedCapacity
}
func (r BatchGetItemRequest) ToInput() *SDK.BatchGetItemInput {
in := &SDK.BatchGetItemInput{}
if len(r.RequestItems) != 0 {
m := make(map[string]SDK.KeysAndAttributes)
for key, val := range r.RequestItems {
m[key] = val.ToSDK()
}
in.RequestItems = m
}
in.ReturnConsumedCapacity = SDK.ReturnConsumedCapacity(r.ReturnConsumedCapacity)
return in
}
// BatchGetItemResult contains results from `BatchGetItem` operation.
type BatchGetItemResult struct {
ConsumedCapacity []ConsumedCapacity
Responses map[string][]map[string]SDK.AttributeValue // keep original type to reduce unmarshal cost
UnprocessedKeys map[string]KeysAndAttributes
}
func NewBatchGetItemResult(output *SDK.BatchGetItemResponse) *BatchGetItemResult {
r := &BatchGetItemResult{}
if output == nil {
return r
}
r.ConsumedCapacity = newConsumedCapacities(output.ConsumedCapacity)
r.Responses = output.Responses
r.UnprocessedKeys = make(map[string]KeysAndAttributes, len(output.UnprocessedKeys))
for key, val := range output.UnprocessedKeys {
r.UnprocessedKeys[key] = newKeysAndAttributes(val)
}
return r
}
func (r BatchGetItemResult) ToSliceMap(tableName string) ([]map[string]interface{}, error) {
resp, ok := r.Responses[tableName]
if !ok {
return nil, fmt.Errorf("table name [%s] does not exists in the response", tableName)
}
return ToSliceMapValues(resp), nil
}
func (r BatchGetItemResult) Unmarshal(tableName string, out interface{}) error {
resp, ok := r.Responses[tableName]
if !ok {
return fmt.Errorf("table name [%s] does not exists in the response", tableName)
}
return RawUnmarshalAttributeValues(resp, out)
}