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

dynamodb.QueryRequest.ToInput   F

Complexity

Conditions 19

Size

Total Lines 85
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 53
nop 0
dl 0
loc 85
rs 0.5999
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like dynamodb.QueryRequest.ToInput often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
// Query executes `Query` operation.
13
func (svc *DynamoDB) Query(ctx context.Context, r QueryRequest) (*QueryResult, error) {
14
	in, err := r.ToInput()
15
	if err != nil {
16
		return nil, err
17
	}
18
19
	out, err := svc.RawQuery(ctx, in)
20
	if err == nil {
21
		return NewQueryResult(out), nil
22
	}
23
24
	err = svc.errWrap(errors.ErrorData{
25
		Err:          err,
26
		AWSOperation: "Query",
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
// QueryRequest has parameters for `Query` operation.
33
type QueryRequest struct {
34
	TableName string
35
36
	// optional
37
	AttributesToGet           []string
38
	ConditionalOperator       ConditionalOperator
39
	ConsistentRead            bool
40
	ExclusiveStartKey         map[string]AttributeValue
41
	ExpressionAttributeNames  map[string]string
42
	ExpressionAttributeValues map[string]AttributeValue
43
	FilterExpression          string
44
	IndexName                 string
45
	KeyConditionExpression    string
46
	KeyConditions             map[string]Condition
47
	Limit                     int64
48
	ProjectionExpression      string
49
	QueryFilter               map[string]Condition
50
	ReturnConsumedCapacity    ReturnConsumedCapacity
51
	ScanIndexForward          bool
52
	Select                    Select
53
54
	XConditions XConditions
55
}
56
57
func (r QueryRequest) ToInput() (*SDK.QueryInput, error) {
0 ignored issues
show
introduced by
exported method QueryRequest.ToInput should have comment or be unexported
Loading history...
58
	in := &SDK.QueryInput{}
59
60
	if r.TableName != "" {
61
		in.TableName = pointers.String(r.TableName)
62
	}
63
64
	in.AttributesToGet = r.AttributesToGet
65
	in.ConditionalOperator = SDK.ConditionalOperator(r.ConditionalOperator)
66
67
	if r.ConsistentRead {
68
		in.ConsistentRead = pointers.Bool(r.ConsistentRead)
69
	}
70
71
	if len(r.ExclusiveStartKey) != 0 {
72
		m := make(map[string]SDK.AttributeValue, len(r.ExclusiveStartKey))
73
		for key, val := range r.ExclusiveStartKey {
74
			m[key] = val.ToSDK()
75
		}
76
		in.ExclusiveStartKey = m
77
	}
78
79
	in.ExpressionAttributeNames = r.ExpressionAttributeNames
80
81
	if len(r.ExpressionAttributeValues) != 0 {
82
		m := make(map[string]SDK.AttributeValue, len(r.ExpressionAttributeValues))
83
		for key, val := range r.ExpressionAttributeValues {
84
			m[key] = val.ToSDK()
85
		}
86
		in.ExpressionAttributeValues = m
87
	}
88
89
	if r.FilterExpression != "" {
90
		in.FilterExpression = pointers.String(r.FilterExpression)
91
	}
92
	if r.IndexName != "" {
93
		in.IndexName = pointers.String(r.IndexName)
94
	}
95
	if r.KeyConditionExpression != "" {
96
		in.KeyConditionExpression = pointers.String(r.KeyConditionExpression)
97
	}
98
99
	if len(r.KeyConditions) != 0 {
100
		m := make(map[string]SDK.Condition, len(r.KeyConditions))
101
		for key, val := range r.KeyConditions {
102
			m[key] = val.ToSDK()
103
		}
104
		in.KeyConditions = m
105
	}
106
107
	if r.Limit != 0 {
108
		in.Limit = pointers.Long64(r.Limit)
109
	}
110
	if r.ProjectionExpression != "" {
111
		in.ProjectionExpression = pointers.String(r.ProjectionExpression)
112
	}
113
114
	if len(r.QueryFilter) != 0 {
115
		m := make(map[string]SDK.Condition, len(r.QueryFilter))
116
		for key, val := range r.QueryFilter {
117
			m[key] = val.ToSDK()
118
		}
119
		in.QueryFilter = m
120
	}
121
122
	in.ReturnConsumedCapacity = SDK.ReturnConsumedCapacity(r.ReturnConsumedCapacity)
123
124
	if r.ScanIndexForward {
125
		in.ScanIndexForward = pointers.Bool(r.ScanIndexForward)
126
	}
127
128
	in.Select = SDK.Select(r.Select)
129
130
	if r.XConditions.hasValue() {
131
		expr, err := r.XConditions.Build()
132
		if err != nil {
133
			return nil, err
134
		}
135
		in.ExpressionAttributeNames = expr.Names()
136
		in.ExpressionAttributeValues = expr.Values()
137
		in.FilterExpression = expr.Filter()
138
		in.KeyConditionExpression = expr.KeyCondition()
139
		in.ProjectionExpression = expr.Projection()
140
	}
141
	return in, nil
142
}
143
144
// QueryResult contains results from `Query` operation.
145
type QueryResult struct {
146
	ConsumedCapacity ConsumedCapacity
147
	Count            int64
148
	Items            []map[string]SDK.AttributeValue // keep original type to reduce unmarshal cost
149
	LastEvaluatedKey map[string]AttributeValue
150
	ScannedCount     int64
151
}
152
153
func NewQueryResult(output *SDK.QueryResponse) *QueryResult {
0 ignored issues
show
introduced by
exported function NewQueryResult should have comment or be unexported
Loading history...
154
	r := &QueryResult{}
155
	if output == nil {
156
		return r
157
	}
158
159
	if output.ConsumedCapacity != nil {
160
		r.ConsumedCapacity = newConsumedCapacity(*output.ConsumedCapacity)
161
	}
162
163
	if output.Count != nil {
164
		r.Count = *output.Count
165
	}
166
167
	r.Items = output.Items
168
	r.LastEvaluatedKey = newAttributeValueMap(output.LastEvaluatedKey)
169
170
	if output.ScannedCount != nil {
171
		r.ScannedCount = *output.ScannedCount
172
	}
173
	return r
174
}
175
176
func (r QueryResult) ToSliceMap() ([]map[string]interface{}, error) {
0 ignored issues
show
introduced by
exported method QueryResult.ToSliceMap should have comment or be unexported
Loading history...
177
	return ToSliceMapValues(r.Items), nil
178
}
179
180
func (r QueryResult) Unmarshal(out interface{}) error {
0 ignored issues
show
introduced by
exported method QueryResult.Unmarshal should have comment or be unexported
Loading history...
181
	return RawUnmarshalAttributeValues(r.Items, out)
182
}
183