ses.SendEmailRequest.ToInput   F
last analyzed

Complexity

Conditions 16

Size

Total Lines 66
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 42
nop 0
dl 0
loc 66
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 ses.SendEmailRequest.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 ses
2
3
import (
4
	"context"
5
6
	SDK "github.com/aws/aws-sdk-go-v2/service/ses"
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
// SendEmail executes `SendEmail` operation.
13
func (svc *SES) SendEmail(ctx context.Context, r SendEmailRequest) (*SendEmailResult, error) {
14
	out, err := svc.RawSendEmail(ctx, r.ToInput())
15
	if err != nil {
16
		err = svc.errWrap(errors.ErrorData{
17
			Err:          err,
18
			AWSOperation: "SendEmail",
19
		})
20
		svc.Errorf(err.Error())
0 ignored issues
show
introduced by
can't check non-constant format in call to Errorf
Loading history...
21
		return nil, err
22
	}
23
	return NewSendEmailResult(out), nil
24
}
25
26
// SendEmailRequest has parameters for `SendEmail` operation.
27
type SendEmailRequest struct {
28
	Destination Destination
29
	Source      string
30
31
	Subject        string
32
	SubjectCharset string // optional
33
34
	// Either HTMLBody or TextBody is required
35
	HTMLBody    string
36
	HTMLCharset string
37
	TextBody    string
38
	TextCharset string
39
40
	// optional
41
	ConfigurationSetName string
42
	ReplyToAddresses     []string
43
	ReturnPath           string
44
	ReturnPathARN        string
45
	SourceARN            string
46
	Tags                 []MessageTag
47
}
48
49
func (r SendEmailRequest) ToInput() *SDK.SendEmailInput {
0 ignored issues
show
introduced by
exported method SendEmailRequest.ToInput should have comment or be unexported
Loading history...
50
	in := &SDK.SendEmailInput{
51
		Message: &SDK.Message{},
52
	}
53
54
	in.Destination = r.Destination.ToSDK()
55
56
	if r.Source != "" {
57
		in.Source = pointers.String(r.Source)
58
	}
59
60
	if r.Subject != "" {
61
		content := SDK.Content{
62
			Data: pointers.String(r.Subject),
63
		}
64
		if r.SubjectCharset != "" {
65
			content.Charset = pointers.String(r.SubjectCharset)
66
		}
67
		in.Message.Subject = &content
68
	}
69
70
	if r.HTMLBody != "" || r.TextBody != "" {
71
		body := SDK.Body{}
72
		if r.HTMLBody != "" {
73
			content := SDK.Content{}
74
			content.Data = pointers.String(r.HTMLBody)
75
			if r.HTMLCharset != "" {
76
				content.Charset = pointers.String(r.HTMLCharset)
77
			}
78
			body.Html = &content
79
		}
80
		if r.TextBody != "" {
81
			content := SDK.Content{}
82
			content.Data = pointers.String(r.TextBody)
83
			if r.TextCharset != "" {
84
				content.Charset = pointers.String(r.TextCharset)
85
			}
86
			body.Text = &content
87
		}
88
		in.Message.Body = &body
89
	}
90
91
	if r.ConfigurationSetName != "" {
92
		in.ConfigurationSetName = pointers.String(r.ConfigurationSetName)
93
	}
94
95
	in.ReplyToAddresses = r.ReplyToAddresses
96
97
	if r.ReturnPath != "" {
98
		in.ReturnPath = pointers.String(r.ReturnPath)
99
	}
100
	if r.ReturnPathARN != "" {
101
		in.ReturnPathArn = pointers.String(r.ReturnPathARN)
102
	}
103
	if r.SourceARN != "" {
104
		in.SourceArn = pointers.String(r.SourceARN)
105
	}
106
107
	if len(r.Tags) != 0 {
108
		list := make([]SDK.MessageTag, len(r.Tags))
109
		for i, v := range r.Tags {
110
			list[i] = v.ToSDK()
111
		}
112
		in.Tags = list
113
	}
114
	return in
115
}
116
117
type SendEmailResult struct {
0 ignored issues
show
introduced by
exported type SendEmailResult should have comment or be unexported
Loading history...
118
	MessageID string
119
}
120
121
func NewSendEmailResult(o *SDK.SendEmailResponse) *SendEmailResult {
0 ignored issues
show
introduced by
exported function NewSendEmailResult should have comment or be unexported
Loading history...
122
	result := &SendEmailResult{}
123
	if o == nil {
124
		return result
125
	}
126
127
	if o.MessageId != nil {
128
		result.MessageID = *o.MessageId
129
	}
130
	return result
131
}
132