Passed
Push — master ( 44e67e...3437b8 )
by eval
01:37
created

ssm.PutParameterRequest.ToInput   C

Complexity

Conditions 11

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 26
nop 0
dl 0
loc 37
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like ssm.PutParameterRequest.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 ssm
2
3
import (
4
	"context"
5
6
	SDK "github.com/aws/aws-sdk-go-v2/service/ssm"
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
// PutParameter executes `PutParameter` operation.
13
func (svc *SSM) PutParameter(ctx context.Context, r PutParameterRequest) (*PutParameterResult, error) {
14
	out, err := svc.RawPutParameter(ctx, r.ToInput())
15
	if err == nil {
16
		return NewPutParameterResult(out), err
17
	}
18
19
	if errors.GetAWSErrorCode(err) == "ParameterAlreadyExists" {
20
		return &PutParameterResult{
21
			AlreadyExists: true,
22
		}, nil
23
	}
24
25
	err = svc.errWrap(errors.ErrorData{
26
		Err:          err,
27
		AWSOperation: "PutParameter",
28
	})
29
	svc.Errorf(err.Error())
0 ignored issues
show
introduced by
can't check non-constant format in call to Errorf
Loading history...
30
	return nil, err
31
}
32
33
// PutParameterRequest has parameters for `PutParameter` operation.
34
type PutParameterRequest struct {
35
	Name  string
36
	Value string
37
38
	// optional
39
	AllowedPattern string
40
	DataType       string
41
	Description    string
42
	KeyID          string
43
	Overwrite      bool
44
	Policies       string
45
	Tags           []Tag
46
	Tier           ParameterTier
47
	Type           ParameterType
48
}
49
50
func (r PutParameterRequest) ToInput() *SDK.PutParameterInput {
0 ignored issues
show
introduced by
exported method PutParameterRequest.ToInput should have comment or be unexported
Loading history...
51
	in := &SDK.PutParameterInput{}
52
	if r.Name != "" {
53
		in.Name = pointers.String(r.Name)
54
	}
55
	if r.Value != "" {
56
		in.Value = pointers.String(r.Value)
57
	}
58
	if r.AllowedPattern != "" {
59
		in.AllowedPattern = pointers.String(r.AllowedPattern)
60
	}
61
	if r.DataType != "" {
62
		in.DataType = pointers.String(r.DataType)
63
	}
64
	if r.Description != "" {
65
		in.Description = pointers.String(r.Description)
66
	}
67
	if r.KeyID != "" {
68
		in.KeyId = pointers.String(r.KeyID)
69
	}
70
	if r.Overwrite {
71
		in.Overwrite = pointers.Bool(r.Overwrite)
72
	}
73
	if r.Policies != "" {
74
		in.Policies = pointers.String(r.Policies)
75
	}
76
77
	if len(r.Tags) != 0 {
78
		list := make([]SDK.Tag, len(r.Tags))
79
		for i, v := range r.Tags {
80
			list[i] = v.ToSDK()
81
		}
82
		in.Tags = list
83
	}
84
	in.Tier = SDK.ParameterTier(r.Tier)
85
	in.Type = SDK.ParameterType(r.Type)
86
	return in
87
}
88
89
type PutParameterResult struct {
0 ignored issues
show
introduced by
exported type PutParameterResult should have comment or be unexported
Loading history...
90
	AlreadyExists bool
91
92
	Tier    ParameterTier
93
	Version int64
94
}
95
96
func NewPutParameterResult(o *SDK.PutParameterResponse) *PutParameterResult {
0 ignored issues
show
introduced by
exported function NewPutParameterResult should have comment or be unexported
Loading history...
97
	result := &PutParameterResult{}
98
	if o == nil {
99
		return result
100
	}
101
102
	result.Tier = ParameterTier(o.Tier)
103
	if o.Version != nil {
104
		result.Version = *o.Version
105
	}
106
	return result
107
}
108