| Conditions | 24 | 
| Total Lines | 117 | 
| Code Lines | 80 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
Complex classes like ses.XSendRawEmailRequest.ToRequest 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 | ||
| 49 | func (r XSendRawEmailRequest) ToRequest() (SendRawEmailRequest, error) { | ||
| 50 | 	req := SendRawEmailRequest{ | ||
| 51 | ConfigurationSetName: r.ConfigurationSetName, | ||
| 52 | Destinations: r.To, | ||
| 53 | FromArn: r.FromArn, | ||
| 54 | ReturnPathArn: r.ReturnPathArn, | ||
| 55 | Source: r.From, | ||
| 56 | SourceArn: r.SourceArn, | ||
| 57 | Tags: r.Tags, | ||
| 58 | } | ||
| 59 | |||
| 60 | buf := new(bytes.Buffer) | ||
| 61 | writer := multipart.NewWriter(buf) | ||
| 62 | |||
| 63 | // email header part | ||
| 64 | h := make(textproto.MIMEHeader) | ||
| 65 | 	if r.From != "" { | ||
| 66 | 		h.Set("From", r.From) | ||
| 67 | } | ||
| 68 | 	if len(r.To) != 0 { | ||
| 69 | 		h.Set("To", strings.Join(r.To, ", ")) | ||
| 70 | } | ||
| 71 | 	switch { | ||
| 72 | case r.ReturnPath != "": | ||
| 73 | 		h.Set("Return-Path", r.ReturnPath) | ||
| 74 | case r.From != "": | ||
| 75 | 		h.Set("Return-Path", r.From) | ||
| 76 | } | ||
| 77 | |||
| 78 | 	if r.Subject != "" { | ||
| 79 | 		h.Set("Subject", r.Subject) | ||
| 80 | } | ||
| 81 | |||
| 82 | 	if r.ContentLanguage != "" { | ||
| 83 | 		h.Set("Content-Language", r.ContentLanguage) | ||
| 84 | } | ||
| 85 | |||
| 86 | 	if r.MIMEVersion != "" { | ||
| 87 | 		h.Set("MIME-Version", r.MIMEVersion) | ||
| 88 | } | ||
| 89 | |||
| 90 | 	h.Set("Content-Type", "multipart/mixed; boundary=\""+writer.Boundary()+"\"") | ||
| 91 | _, err := writer.CreatePart(h) | ||
| 92 | 	if err != nil { | ||
| 93 | return req, err | ||
| 94 | } | ||
| 95 | |||
| 96 | // text body part | ||
| 97 | 	if r.TextBody != "" { | ||
| 98 | h := make(textproto.MIMEHeader) | ||
| 99 | charset := r.TextCharset | ||
| 100 | 		if charset == "" { | ||
| 101 | charset = "utf-8" | ||
| 102 | } | ||
| 103 | 		h.Set("Content-Type", fmt.Sprintf("text/plain; charset=%s", charset)) | ||
| 104 | part, err := writer.CreatePart(h) | ||
| 105 | 		if err != nil { | ||
| 106 | return req, err | ||
| 107 | } | ||
| 108 | _, err = part.Write([]byte(r.TextBody)) | ||
| 109 | 		if err != nil { | ||
| 110 | return req, err | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | // html body part | ||
| 115 | 	if r.HTMLBody != "" { | ||
| 116 | h := make(textproto.MIMEHeader) | ||
| 117 | charset := r.HTMLCharset | ||
| 118 | 		if charset == "" { | ||
| 119 | charset = "utf-8" | ||
| 120 | } | ||
| 121 | 		h.Set("Content-Type", fmt.Sprintf("text/html; charset=%s", charset)) | ||
| 122 | part, err := writer.CreatePart(h) | ||
| 123 | 		if err != nil { | ||
| 124 | return req, err | ||
| 125 | } | ||
| 126 | _, err = part.Write([]byte(r.HTMLBody)) | ||
| 127 | 		if err != nil { | ||
| 128 | return req, err | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | // attachment part | ||
| 133 | 	for _, v := range r.Attachments { | ||
| 134 | h := make(textproto.MIMEHeader) | ||
| 135 | 		h.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, v.Filename)) | ||
| 136 | mimeType := v.MIMEType | ||
| 137 | 		if mimeType == "" { | ||
| 138 | mimeType = "text/plain" | ||
| 139 | } | ||
| 140 | 		h.Set("Content-Type", fmt.Sprintf(`%s; name="%s"`, mimeType, v.Filename)) | ||
| 141 | 		h.Set("Content-Description", v.Filename) | ||
| 142 | 		if v.ContentTransferEncoding != "" { | ||
| 143 | 			h.Set("Content-Transfer-Encoding", v.ContentTransferEncoding) | ||
| 144 | } | ||
| 145 | part, err := writer.CreatePart(h) | ||
| 146 | 		if err != nil { | ||
| 147 | return req, err | ||
| 148 | } | ||
| 149 | _, err = part.Write(v.Data) | ||
| 150 | 		if err != nil { | ||
| 151 | return req, err | ||
| 152 | } | ||
| 153 | err = writer.Close() | ||
| 154 | 		if err != nil { | ||
| 155 | return req, err | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | s := buf.String() | ||
| 160 | ss := strings.SplitN(s, "\n", 2) | ||
| 161 | 	if len(ss) < 2 { | ||
| 162 | 		return req, fmt.Errorf("invalid e-mail content: [%+v]", ss) | ||
| 163 | } | ||
| 164 | req.RawMessageData = []byte(ss[1]) | ||
| 165 | return req, nil | ||
| 166 | } | ||
| 175 |