Passed
Push — master ( 2f1af3...a8e5e6 )
by eval
03:46 queued 02:09
created

dynamodb.*DynamoDB.BatchGetItem   A

Complexity

Conditions 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 2
dl 0
loc 12
rs 9.95
c 0
b 0
f 0
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())
0 ignored issues
show
introduced by
can't check non-constant format in call to Errorf
Loading history...
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 {
0 ignored issues
show
introduced by
exported method BatchGetItemRequest.ToInput should have comment or be unexported
Loading history...
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 {
0 ignored issues
show
introduced by
exported function NewBatchGetItemResult should have comment or be unexported
Loading history...
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) {
0 ignored issues
show
introduced by
exported method BatchGetItemResult.ToSliceMap should have comment or be unexported
Loading history...
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 {
0 ignored issues
show
introduced by
exported method BatchGetItemResult.Unmarshal should have comment or be unexported
Loading history...
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