kms.newKeyMetadata   F
last analyzed

Complexity

Conditions 16

Size

Total Lines 64
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 41
nop 1
dl 0
loc 64
rs 2.4
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 kms.newKeyMetadata 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 kms
2
3
import (
4
	"time"
5
6
	SDK "github.com/aws/aws-sdk-go-v2/service/kms"
7
8
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/private/pointers"
9
)
10
11
type KeyMetadata struct {
0 ignored issues
show
introduced by
exported type KeyMetadata should have comment or be unexported
Loading history...
12
	KeyID string
13
14
	// optional
15
	AWSAccountID          string
16
	ARN                   string
17
	CloudHsmClusterID     string
18
	CreationDate          time.Time
19
	CustomKeyStoreID      string
20
	CustomerMasterKeySpec CustomerMasterKeySpec
21
	DeletionDate          time.Time
22
	Description           string
23
	Enabled               bool
24
	EncryptionAlgorithms  []EncryptionAlgorithmSpec
25
	ExpirationModel       ExpirationModelType
26
	KeyManager            KeyManagerType
27
	KeyState              KeyState
28
	KeyUsage              KeyUsageType
29
	Origin                OriginType
30
	SigningAlgorithms     []SigningAlgorithmSpec
31
	ValidTo               time.Time
32
}
33
34
func newKeyMetadata(o *SDK.KeyMetadata) KeyMetadata {
35
	result := KeyMetadata{}
36
	if o == nil {
37
		return result
38
	}
39
40
	if o.KeyId != nil {
41
		result.KeyID = *o.KeyId
42
	}
43
44
	if o.AWSAccountId != nil {
45
		result.AWSAccountID = *o.AWSAccountId
46
	}
47
	if o.Arn != nil {
48
		result.ARN = *o.Arn
49
	}
50
	if o.CloudHsmClusterId != nil {
51
		result.CloudHsmClusterID = *o.CloudHsmClusterId
52
	}
53
	if o.CreationDate != nil {
54
		result.CreationDate = *o.CreationDate
55
	}
56
	if o.CustomKeyStoreId != nil {
57
		result.CustomKeyStoreID = *o.CustomKeyStoreId
58
	}
59
60
	o.CustomerMasterKeySpec = SDK.CustomerMasterKeySpec(result.CustomerMasterKeySpec)
61
62
	if o.DeletionDate != nil {
63
		result.DeletionDate = *o.DeletionDate
64
	}
65
	if o.Description != nil {
66
		result.Description = *o.Description
67
	}
68
	if o.Enabled != nil {
69
		result.Enabled = *o.Enabled
70
	}
71
72
	if len(o.EncryptionAlgorithms) != 0 {
73
		list := make([]EncryptionAlgorithmSpec, len(o.EncryptionAlgorithms))
74
		for i, v := range o.EncryptionAlgorithms {
75
			list[i] = EncryptionAlgorithmSpec(v)
76
		}
77
		result.EncryptionAlgorithms = list
78
	}
79
80
	o.ExpirationModel = SDK.ExpirationModelType(result.ExpirationModel)
81
	o.KeyManager = SDK.KeyManagerType(result.KeyManager)
82
	o.KeyState = SDK.KeyState(result.KeyState)
83
	o.KeyUsage = SDK.KeyUsageType(result.KeyUsage)
84
	o.Origin = SDK.OriginType(result.Origin)
85
86
	if len(o.SigningAlgorithms) != 0 {
87
		list := make([]SigningAlgorithmSpec, len(o.SigningAlgorithms))
88
		for i, v := range o.SigningAlgorithms {
89
			list[i] = SigningAlgorithmSpec(v)
90
		}
91
		result.SigningAlgorithms = list
92
	}
93
94
	if o.ValidTo != nil {
95
		result.ValidTo = *o.ValidTo
96
	}
97
	return result
98
}
99
100
type Tag struct {
0 ignored issues
show
introduced by
exported type Tag should have comment or be unexported
Loading history...
101
	Key   string
102
	Value string
103
}
104
105
func (r Tag) ToSDK() SDK.Tag {
0 ignored issues
show
introduced by
exported method Tag.ToSDK should have comment or be unexported
Loading history...
106
	return SDK.Tag{
107
		TagKey:   pointers.String(r.Key),
108
		TagValue: pointers.String(r.Value),
109
	}
110
}
111