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