Passed
Push — master ( 70e599...9a4686 )
by eval
01:38
created

dynamodb.NewScanResult   A

Complexity

Conditions 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nop 1
dl 0
loc 21
rs 9.2833
c 0
b 0
f 0
1
package dynamodb
2
3
import (
4
	"context"
5
6
	SDK "github.com/aws/aws-sdk-go-v2/service/dynamodb"
7
8
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors"
9
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/private/pointers"
10
)
11
12
// Scan executes `Scan` operation.
13
func (svc *DynamoDB) Scan(ctx context.Context, r ScanRequest) (*ScanResult, error) {
14
	in, err := r.ToInput()
15
	if err != nil {
16
		return nil, err
17
	}
18
19
	out, err := svc.RawScan(ctx, in)
20
	if err == nil {
21
		return NewScanResult(out), nil
22
	}
23
24
	err = svc.errWrap(errors.ErrorData{
25
		Err:          err,
26
		AWSOperation: "Scan",
27
	})
28
	svc.Errorf(err.Error())
0 ignored issues
show
introduced by
can't check non-constant format in call to Errorf
Loading history...
29
	return nil, err
30
}
31
32
// ScanRequest has parameters for `Scan` operation.
33
type ScanRequest struct {
34
	TableName string
35
36
	// optional
37
	ConsistentRead            bool
38
	ExclusiveStartKey         map[string]AttributeValue
39
	ExpressionAttributeNames  map[string]string
40
	ExpressionAttributeValues map[string]AttributeValue
41
	FilterExpression          string
42
	IndexName                 string
43
	Limit                     int64
44
	ProjectionExpression      string
45
	ReturnConsumedCapacity    ReturnConsumedCapacity
46
	Segment                   int64
47
	Select                    Select
48
	TotalSegments             int64
49
50
	XConditions XConditions
51
}
52
53
func (r ScanRequest) ToInput() (*SDK.ScanInput, error) {
0 ignored issues
show
introduced by
exported method ScanRequest.ToInput should have comment or be unexported
Loading history...
54
	in := &SDK.ScanInput{}
55
56
	if r.TableName != "" {
57
		in.TableName = pointers.String(r.TableName)
58
	}
59
60
	if r.ConsistentRead {
61
		in.ConsistentRead = pointers.Bool(r.ConsistentRead)
62
	}
63
64
	if len(r.ExclusiveStartKey) != 0 {
65
		m := make(map[string]SDK.AttributeValue, len(r.ExclusiveStartKey))
66
		for key, val := range r.ExclusiveStartKey {
67
			m[key] = val.ToSDK()
68
		}
69
		in.ExclusiveStartKey = m
70
	}
71
72
	in.ExpressionAttributeNames = r.ExpressionAttributeNames
73
74
	if len(r.ExpressionAttributeValues) != 0 {
75
		m := make(map[string]SDK.AttributeValue, len(r.ExpressionAttributeValues))
76
		for key, val := range r.ExpressionAttributeValues {
77
			m[key] = val.ToSDK()
78
		}
79
		in.ExpressionAttributeValues = m
80
	}
81
82
	if r.FilterExpression != "" {
83
		in.FilterExpression = pointers.String(r.FilterExpression)
84
	}
85
	if r.IndexName != "" {
86
		in.IndexName = pointers.String(r.IndexName)
87
	}
88
89
	if r.Limit != 0 {
90
		in.Limit = pointers.Long64(r.Limit)
91
	}
92
	if r.ProjectionExpression != "" {
93
		in.ProjectionExpression = pointers.String(r.ProjectionExpression)
94
	}
95
96
	in.ReturnConsumedCapacity = SDK.ReturnConsumedCapacity(r.ReturnConsumedCapacity)
97
98
	in.Select = SDK.Select(r.Select)
99
100
	if r.XConditions.hasValue() {
101
		expr, err := r.XConditions.Build()
102
		if err != nil {
103
			return nil, err
104
		}
105
		in.ExpressionAttributeNames = expr.Names()
106
		in.ExpressionAttributeValues = expr.Values()
107
		in.FilterExpression = expr.Filter()
108
		in.ProjectionExpression = expr.Projection()
109
	}
110
	return in, nil
111
}
112
113
// ScanResult contains results from `Scan` operation.
114
type ScanResult struct {
115
	ConsumedCapacity ConsumedCapacity
116
	Count            int64
117
	Items            []map[string]SDK.AttributeValue // keep original type to reduce unmarshal cost
118
	LastEvaluatedKey map[string]AttributeValue
119
	ScannedCount     int64
120
}
121
122
func NewScanResult(output *SDK.ScanResponse) *ScanResult {
0 ignored issues
show
introduced by
exported function NewScanResult should have comment or be unexported
Loading history...
123
	r := &ScanResult{}
124
	if output == nil {
125
		return r
126
	}
127
128
	if output.ConsumedCapacity != nil {
129
		r.ConsumedCapacity = newConsumedCapacity(*output.ConsumedCapacity)
130
	}
131
132
	if output.Count != nil {
133
		r.Count = *output.Count
134
	}
135
136
	r.Items = output.Items
137
	r.LastEvaluatedKey = newAttributeValueMap(output.LastEvaluatedKey)
138
139
	if output.ScannedCount != nil {
140
		r.ScannedCount = *output.ScannedCount
141
	}
142
	return r
143
}
144
145
func (r ScanResult) ToSliceMap() ([]map[string]interface{}, error) {
0 ignored issues
show
introduced by
exported method ScanResult.ToSliceMap should have comment or be unexported
Loading history...
146
	return ToSliceMapValues(r.Items), nil
147
}
148
149
func (r ScanResult) Unmarshal(out interface{}) error {
0 ignored issues
show
introduced by
exported method ScanResult.Unmarshal should have comment or be unexported
Loading history...
150
	return RawUnmarshalAttributeValues(r.Items, out)
151
}
152