pinpointemail.SendEmailRequest.ToInput   F
last analyzed

Complexity

Conditions 20

Size

Total Lines 88
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 59
nop 0
dl 0
loc 88
rs 0
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 pinpointemail.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 pinpointemail
2
3
import (
4
	"context"
5
6
	SDK "github.com/aws/aws-sdk-go-v2/service/pinpointemail"
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 *PinpointEmail) 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
	To   []string
29
	Cc   []string
30
	Bcc  []string
31
	From string
32
33
	// for text or html
34
	Subject        string
35
	SubjectCharset string // optional
36
	HTMLBody       string
37
	HTMLCharset    string // optional
38
	TextBody       string
39
	TextCharset    string // optional
40
41
	// for template
42
	TemplateARN  string
43
	TemplateData string
44
45
	// for raw
46
	RawMessageData []byte
47
48
	ConfigurationSetName           string
49
	EmailTags                      []Tag
50
	FeedbackForwardingEmailAddress string
51
	ReplyToAddresses               []string
52
}
53
54
func (r SendEmailRequest) ToInput() *SDK.SendEmailInput {
0 ignored issues
show
introduced by
exported method SendEmailRequest.ToInput should have comment or be unexported
Loading history...
55
	in := &SDK.SendEmailInput{}
56
	switch {
57
	case len(r.To) != 0,
58
		len(r.Cc) != 0,
59
		len(r.Bcc) != 0:
60
		in.Destination = &SDK.Destination{
61
			ToAddresses:  r.To,
62
			CcAddresses:  r.Cc,
63
			BccAddresses: r.Bcc,
64
		}
65
	}
66
	if r.From != "" {
67
		in.FromEmailAddress = pointers.String(r.From)
68
	}
69
70
	if len(r.RawMessageData) != 0 {
71
		in.Content = &SDK.EmailContent{
72
			Raw: &SDK.RawMessage{
73
				Data: r.RawMessageData,
74
			},
75
		}
76
	}
77
78
	switch {
79
	case r.Subject != "",
80
		r.HTMLBody != "":
81
		if in.Content == nil {
82
			in.Content = &SDK.EmailContent{}
83
		}
84
		in.Content.Simple = &SDK.Message{}
85
	}
86
87
	if r.Subject != "" {
88
		content := SDK.Content{
89
			Data: pointers.String(r.Subject),
90
		}
91
		if r.SubjectCharset != "" {
92
			content.Charset = pointers.String(r.SubjectCharset)
93
		}
94
		in.Content.Simple.Subject = &content
95
	}
96
97
	if r.HTMLBody != "" || r.TextBody != "" {
98
		body := SDK.Body{}
99
		if r.HTMLBody != "" {
100
			content := SDK.Content{}
101
			content.Data = pointers.String(r.HTMLBody)
102
			if r.HTMLCharset != "" {
103
				content.Charset = pointers.String(r.HTMLCharset)
104
			}
105
			body.Html = &content
106
		}
107
		if r.TextBody != "" {
108
			content := SDK.Content{}
109
			content.Data = pointers.String(r.TextBody)
110
			if r.TextCharset != "" {
111
				content.Charset = pointers.String(r.TextCharset)
112
			}
113
			body.Text = &content
114
		}
115
		in.Content.Simple.Body = &body
116
	}
117
118
	if r.TemplateARN != "" {
119
		template := SDK.Template{
120
			TemplateArn: pointers.String(r.TemplateARN),
121
		}
122
		if r.TemplateData != "" {
123
			template.TemplateData = pointers.String(r.TemplateData)
124
		}
125
		in.Content.Template = &template
126
	}
127
128
	if r.ConfigurationSetName != "" {
129
		in.ConfigurationSetName = pointers.String(r.ConfigurationSetName)
130
	}
131
	if len(r.EmailTags) != 0 {
132
		list := make([]SDK.MessageTag, len(r.EmailTags))
133
		for i, v := range r.EmailTags {
134
			list[i] = v.ToEmailTag()
135
		}
136
	}
137
	if r.FeedbackForwardingEmailAddress != "" {
138
		in.FeedbackForwardingEmailAddress = pointers.String(r.FeedbackForwardingEmailAddress)
139
	}
140
	in.ReplyToAddresses = r.ReplyToAddresses
141
	return in
142
}
143
144
type SendEmailResult struct {
0 ignored issues
show
introduced by
exported type SendEmailResult should have comment or be unexported
Loading history...
145
	MessageID string
146
}
147
148
func NewSendEmailResult(o *SDK.SendEmailResponse) *SendEmailResult {
0 ignored issues
show
introduced by
exported function NewSendEmailResult should have comment or be unexported
Loading history...
149
	result := &SendEmailResult{}
150
	if o == nil {
151
		return result
152
	}
153
154
	if o.MessageId != nil {
155
		result.MessageID = *o.MessageId
156
	}
157
	return result
158
}
159