1
|
|
|
package dynamodb |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"fmt" |
6
|
|
|
|
7
|
|
|
SDK "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
8
|
|
|
|
9
|
|
|
"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
// BatchGetItem executes `BatchGetItem` operation. |
13
|
|
|
func (svc *DynamoDB) BatchGetItem(ctx context.Context, r BatchGetItemRequest) (*BatchGetItemResult, error) { |
14
|
|
|
out, err := svc.RawBatchGetItem(ctx, r.ToInput()) |
15
|
|
|
if err == nil { |
16
|
|
|
return NewBatchGetItemResult(out), nil |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
err = svc.errWrap(errors.ErrorData{ |
20
|
|
|
Err: err, |
21
|
|
|
AWSOperation: "BatchGetItem", |
22
|
|
|
}) |
23
|
|
|
svc.Errorf(err.Error()) |
|
|
|
|
24
|
|
|
return nil, err |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// BatchGetItemRequest has parameters for `BatchGetItem` operation. |
28
|
|
|
type BatchGetItemRequest struct { |
29
|
|
|
RequestItems map[string]KeysAndAttributes |
30
|
|
|
|
31
|
|
|
// optional |
32
|
|
|
ReturnConsumedCapacity ReturnConsumedCapacity |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
func (r BatchGetItemRequest) ToInput() *SDK.BatchGetItemInput { |
|
|
|
|
36
|
|
|
in := &SDK.BatchGetItemInput{} |
37
|
|
|
if len(r.RequestItems) != 0 { |
38
|
|
|
m := make(map[string]SDK.KeysAndAttributes) |
39
|
|
|
for key, val := range r.RequestItems { |
40
|
|
|
m[key] = val.ToSDK() |
41
|
|
|
} |
42
|
|
|
in.RequestItems = m |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
in.ReturnConsumedCapacity = SDK.ReturnConsumedCapacity(r.ReturnConsumedCapacity) |
46
|
|
|
return in |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// BatchGetItemResult contains results from `BatchGetItem` operation. |
50
|
|
|
type BatchGetItemResult struct { |
51
|
|
|
ConsumedCapacity []ConsumedCapacity |
52
|
|
|
Responses map[string][]map[string]SDK.AttributeValue // keep original type to reduce unmarshal cost |
53
|
|
|
UnprocessedKeys map[string]KeysAndAttributes |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
func NewBatchGetItemResult(output *SDK.BatchGetItemResponse) *BatchGetItemResult { |
|
|
|
|
57
|
|
|
r := &BatchGetItemResult{} |
58
|
|
|
if output == nil { |
59
|
|
|
return r |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
r.ConsumedCapacity = newConsumedCapacities(output.ConsumedCapacity) |
63
|
|
|
r.Responses = output.Responses |
64
|
|
|
|
65
|
|
|
r.UnprocessedKeys = make(map[string]KeysAndAttributes, len(output.UnprocessedKeys)) |
66
|
|
|
for key, val := range output.UnprocessedKeys { |
67
|
|
|
r.UnprocessedKeys[key] = newKeysAndAttributes(val) |
68
|
|
|
} |
69
|
|
|
return r |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
func (r BatchGetItemResult) ToSliceMap(tableName string) ([]map[string]interface{}, error) { |
|
|
|
|
73
|
|
|
resp, ok := r.Responses[tableName] |
74
|
|
|
if !ok { |
75
|
|
|
return nil, fmt.Errorf("table name [%s] does not exists in the response", tableName) |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return ToSliceMapValues(resp), nil |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
func (r BatchGetItemResult) Unmarshal(tableName string, out interface{}) error { |
|
|
|
|
82
|
|
|
resp, ok := r.Responses[tableName] |
83
|
|
|
if !ok { |
84
|
|
|
return fmt.Errorf("table name [%s] does not exists in the response", tableName) |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return RawUnmarshalAttributeValues(resp, out) |
88
|
|
|
} |
89
|
|
|
|