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

dynamodb.CreateTableRequest.ToInput   F

Complexity

Conditions 15

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 40
nop 0
dl 0
loc 59
rs 2.9998
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.CreateTableRequest.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
// CreateTable executes `CreateTable` operation.
13
func (svc *DynamoDB) CreateTable(ctx context.Context, r CreateTableRequest) (*CreateTableResult, error) {
14
	out, err := svc.RawCreateTable(ctx, r.ToInput())
15
	if err == nil {
16
		return NewCreateTableResult(out), nil
17
	}
18
19
	err = svc.errWrap(errors.ErrorData{
20
		Err:          err,
21
		AWSOperation: "CreateTable",
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
// CreateTableRequest has parameters for `CreateTable` operation.
28
type CreateTableRequest struct {
29
	TableName            string
30
	AttributeDefinitions []AttributeDefinition
31
	KeySchema            []KeySchemaElement
32
33
	// optional
34
	BillingMode            BillingMode
35
	GlobalSecondaryIndexes []GlobalSecondaryIndex
36
	LocalSecondaryIndexes  []LocalSecondaryIndex
37
	ProvisionedThroughput  ProvisionedThroughput
38
	SSESpecification       SSESpecification
39
	StreamSpecification    StreamSpecification
40
	Tags                   []Tag
41
}
42
43
func (r CreateTableRequest) ToInput() *SDK.CreateTableInput {
0 ignored issues
show
introduced by
exported method CreateTableRequest.ToInput should have comment or be unexported
Loading history...
44
	in := &SDK.CreateTableInput{}
45
46
	if r.TableName != "" {
47
		in.TableName = pointers.String(r.TableName)
48
	}
49
	if len(r.AttributeDefinitions) != 0 {
50
		list := make([]SDK.AttributeDefinition, len(r.AttributeDefinitions))
51
		for i, v := range r.AttributeDefinitions {
52
			list[i] = v.ToSDK()
53
		}
54
		in.AttributeDefinitions = list
55
	}
56
	if len(r.KeySchema) != 0 {
57
		list := make([]SDK.KeySchemaElement, len(r.KeySchema))
58
		for i, v := range r.KeySchema {
59
			list[i] = v.ToSDK()
60
		}
61
		in.KeySchema = list
62
	}
63
64
	in.BillingMode = SDK.BillingMode(r.BillingMode)
65
66
	if len(r.GlobalSecondaryIndexes) != 0 {
67
		list := make([]SDK.GlobalSecondaryIndex, len(r.GlobalSecondaryIndexes))
68
		for i, v := range r.GlobalSecondaryIndexes {
69
			list[i] = v.ToSDK()
70
		}
71
		in.GlobalSecondaryIndexes = list
72
	}
73
	if len(r.LocalSecondaryIndexes) != 0 {
74
		list := make([]SDK.LocalSecondaryIndex, len(r.LocalSecondaryIndexes))
75
		for i, v := range r.LocalSecondaryIndexes {
76
			list[i] = v.ToSDK()
77
		}
78
		in.LocalSecondaryIndexes = list
79
	}
80
81
	if r.ProvisionedThroughput.hasValue() {
82
		v := r.ProvisionedThroughput.ToSDK()
83
		in.ProvisionedThroughput = &v
84
	}
85
	if r.SSESpecification.hasValue() {
86
		v := r.SSESpecification.ToSDK()
87
		in.SSESpecification = &v
88
	}
89
	if r.StreamSpecification.hasValue() {
90
		v := r.StreamSpecification.ToSDK()
91
		in.StreamSpecification = &v
92
	}
93
94
	if len(r.Tags) != 0 {
95
		list := make([]SDK.Tag, len(r.Tags))
96
		for i, v := range r.Tags {
97
			list[i] = v.ToSDK()
98
		}
99
		in.Tags = list
100
	}
101
	return in
102
}
103
104
// CreateTableResult contains results from `CreateTable` operation.
105
type CreateTableResult struct {
106
	TableDescription TableDescription
107
}
108
109
func NewCreateTableResult(output *SDK.CreateTableResponse) *CreateTableResult {
0 ignored issues
show
introduced by
exported function NewCreateTableResult should have comment or be unexported
Loading history...
110
	r := &CreateTableResult{}
111
	if output == nil {
112
		return r
113
	}
114
115
	if output.TableDescription != nil {
116
		r.TableDescription = newTableDescription(*output.TableDescription)
117
	}
118
	return r
119
}
120