s3.GetObjectRequest.ToInput   F
last analyzed

Complexity

Conditions 19

Size

Total Lines 61
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 40
nop 0
dl 0
loc 61
rs 0.5999
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 s3.GetObjectRequest.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 s3
2
3
import (
4
	"bytes"
5
	"context"
6
	"io"
7
	"time"
8
9
	"github.com/aws/aws-sdk-go-v2/aws/awserr"
10
	SDK "github.com/aws/aws-sdk-go-v2/service/s3"
11
12
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors"
13
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/private/pointers"
14
)
15
16
// GetObject executes `GetObject` operation.
17
func (svc *S3) GetObject(ctx context.Context, r GetObjectRequest) (*GetObjectResult, error) {
18
	out, err := svc.RawGetObject(ctx, r.ToInput())
19
	if err == nil {
20
		return NewGetObjectResult(out), nil
21
	}
22
	if aerr, ok := err.(awserr.Error); ok {
23
		if aerr.Code() == "NoSuchKey" {
24
			return NewGetObjectResult(out), nil
25
		}
26
	}
27
28
	err = svc.errWrap(errors.ErrorData{
29
		Err:          err,
30
		AWSOperation: "GetObject",
31
	})
32
	svc.Errorf(err.Error())
0 ignored issues
show
introduced by
can't check non-constant format in call to Errorf
Loading history...
33
	return nil, err
34
}
35
36
// GetObjectRequest has parameters for `GetObject` operation.
37
type GetObjectRequest struct {
38
	Bucket string
39
	Key    string
40
41
	// optional
42
	IfMatch                    string
43
	IfModifiedSince            time.Time
44
	IfNoneMatch                string
45
	IfUnmodifiedSince          time.Time
46
	PartNumber                 int64
47
	Range                      string
48
	RequestPayer               RequestPayer
49
	ResponseCacheControl       string
50
	ResponseContentDisposition string
51
	ResponseContentEncoding    string
52
	ResponseContentLanguage    string
53
	ResponseContentType        string
54
	ResponseExpires            time.Time
55
	SSECustomerAlgorithm       string
56
	SSECustomerKey             string
57
	SSECustomerKeyMD5          string
58
	VersionID                  string
59
}
60
61
func (r GetObjectRequest) ToInput() *SDK.GetObjectInput {
0 ignored issues
show
introduced by
exported method GetObjectRequest.ToInput should have comment or be unexported
Loading history...
62
	in := &SDK.GetObjectInput{}
63
	if r.Bucket != "" {
64
		in.Bucket = pointers.String(r.Bucket)
65
	}
66
	if r.Key != "" {
67
		in.Key = pointers.String(r.Key)
68
	}
69
70
	if r.IfMatch != "" {
71
		in.IfMatch = pointers.String(r.IfMatch)
72
	}
73
	if !r.IfModifiedSince.IsZero() {
74
		in.IfModifiedSince = &r.IfModifiedSince
75
	}
76
	if r.IfNoneMatch != "" {
77
		in.IfNoneMatch = pointers.String(r.IfNoneMatch)
78
	}
79
	if !r.IfUnmodifiedSince.IsZero() {
80
		in.IfUnmodifiedSince = &r.IfUnmodifiedSince
81
	}
82
	if r.PartNumber != 0 {
83
		in.PartNumber = pointers.Long64(r.PartNumber)
84
	}
85
	if r.Range != "" {
86
		in.Range = pointers.String(r.Range)
87
	}
88
89
	in.RequestPayer = SDK.RequestPayer(r.RequestPayer)
90
91
	if r.ResponseCacheControl != "" {
92
		in.ResponseCacheControl = pointers.String(r.ResponseCacheControl)
93
	}
94
	if r.ResponseContentDisposition != "" {
95
		in.ResponseContentDisposition = pointers.String(r.ResponseContentDisposition)
96
	}
97
	if r.ResponseContentEncoding != "" {
98
		in.ResponseContentEncoding = pointers.String(r.ResponseContentEncoding)
99
	}
100
	if r.ResponseContentLanguage != "" {
101
		in.ResponseContentLanguage = pointers.String(r.ResponseContentLanguage)
102
	}
103
	if r.ResponseContentType != "" {
104
		in.ResponseContentType = pointers.String(r.ResponseContentType)
105
	}
106
	if !r.ResponseExpires.IsZero() {
107
		in.ResponseExpires = &r.ResponseExpires
108
	}
109
	if r.SSECustomerAlgorithm != "" {
110
		in.SSECustomerAlgorithm = pointers.String(r.SSECustomerAlgorithm)
111
	}
112
	if r.SSECustomerKey != "" {
113
		in.SSECustomerKey = pointers.String(r.SSECustomerKey)
114
	}
115
	if r.SSECustomerKeyMD5 != "" {
116
		in.SSECustomerKeyMD5 = pointers.String(r.SSECustomerKeyMD5)
117
	}
118
	if r.VersionID != "" {
119
		in.VersionId = pointers.String(r.VersionID)
120
	}
121
	return in
122
}
123
124
// GetObjectResult contains results from `GetObject` operation.
125
type GetObjectResult struct {
126
	Exists bool
127
128
	AcceptRanges              string
129
	Body                      io.ReadCloser
130
	CacheControl              string
131
	ContentDisposition        string
132
	ContentEncoding           string
133
	ContentLanguage           string
134
	ContentLength             int64
135
	ContentRange              string
136
	ContentType               string
137
	DeleteMarker              bool
138
	ETag                      string
139
	Expiration                string
140
	Expires                   string
141
	LastModified              time.Time
142
	Metadata                  map[string]string
143
	MissingMeta               int64
144
	ObjectLockLegalHoldStatus ObjectLockLegalHoldStatus
145
	ObjectLockMode            ObjectLockMode
146
	ObjectLockRetainUntilDate time.Time
147
	PartsCount                int64
148
	ReplicationStatus         ReplicationStatus
149
	RequestCharged            RequestCharged
150
	Restore                   string
151
	SSECustomerAlgorithm      string
152
	SSECustomerKeyMD5         string
153
	SSEKMSKeyID               string
154
	ServerSideEncryption      ServerSideEncryption
155
	StorageClass              StorageClass
156
	TagCount                  int64
157
	VersionID                 string
158
	WebsiteRedirectLocation   string
159
}
160
161
func NewGetObjectResult(output *SDK.GetObjectResponse) *GetObjectResult {
0 ignored issues
show
introduced by
exported function NewGetObjectResult should have comment or be unexported
Loading history...
162
	r := &GetObjectResult{}
163
	if output == nil {
164
		return r
165
	}
166
167
	r.Exists = true
168
169
	if output.AcceptRanges != nil {
170
		r.AcceptRanges = *output.AcceptRanges
171
	}
172
	if output.Body != nil {
173
		r.Body = output.Body
174
	}
175
	if output.CacheControl != nil {
176
		r.CacheControl = *output.CacheControl
177
	}
178
	if output.ContentDisposition != nil {
179
		r.ContentDisposition = *output.ContentDisposition
180
	}
181
	if output.ContentEncoding != nil {
182
		r.ContentEncoding = *output.ContentEncoding
183
	}
184
	if output.ContentLanguage != nil {
185
		r.ContentLanguage = *output.ContentLanguage
186
	}
187
	if output.ContentLength != nil {
188
		r.ContentLength = *output.ContentLength
189
	}
190
	if output.ContentRange != nil {
191
		r.ContentRange = *output.ContentRange
192
	}
193
	if output.ContentType != nil {
194
		r.ContentType = *output.ContentType
195
	}
196
	if output.DeleteMarker != nil {
197
		r.DeleteMarker = *output.DeleteMarker
198
	}
199
	if output.ETag != nil {
200
		r.ETag = *output.ETag
201
	}
202
	if output.Expiration != nil {
203
		r.Expiration = *output.Expiration
204
	}
205
	if output.Expires != nil {
206
		r.Expires = *output.Expires
207
	}
208
	if output.LastModified != nil {
209
		r.LastModified = *output.LastModified
210
	}
211
212
	r.Metadata = output.Metadata
213
214
	if output.MissingMeta != nil {
215
		r.MissingMeta = *output.MissingMeta
216
	}
217
	if r.ObjectLockLegalHoldStatus != "" {
218
		r.ObjectLockLegalHoldStatus = ObjectLockLegalHoldStatus(output.ObjectLockLegalHoldStatus)
219
	}
220
	if r.ObjectLockMode != "" {
221
		r.ObjectLockMode = ObjectLockMode(output.ObjectLockMode)
222
	}
223
	if output.PartsCount != nil {
224
		r.PartsCount = *output.PartsCount
225
	}
226
	if r.ReplicationStatus != "" {
227
		r.ReplicationStatus = ReplicationStatus(output.ReplicationStatus)
228
	}
229
	if r.RequestCharged != "" {
230
		r.RequestCharged = RequestCharged(output.RequestCharged)
231
	}
232
	if output.Restore != nil {
233
		r.Restore = *output.Restore
234
	}
235
	if output.SSECustomerAlgorithm != nil {
236
		r.SSECustomerAlgorithm = *output.SSECustomerAlgorithm
237
	}
238
	if output.SSECustomerKeyMD5 != nil {
239
		r.SSECustomerKeyMD5 = *output.SSECustomerKeyMD5
240
	}
241
	if output.SSEKMSKeyId != nil {
242
		r.SSEKMSKeyID = *output.SSEKMSKeyId
243
	}
244
	if r.ServerSideEncryption != "" {
245
		r.ServerSideEncryption = ServerSideEncryption(output.ServerSideEncryption)
246
	}
247
	if r.StorageClass != "" {
248
		r.StorageClass = StorageClass(output.StorageClass)
249
	}
250
	if output.TagCount != nil {
251
		r.TagCount = *output.TagCount
252
	}
253
	if output.VersionId != nil {
254
		r.VersionID = *output.VersionId
255
	}
256
	if output.WebsiteRedirectLocation != nil {
257
		r.WebsiteRedirectLocation = *output.WebsiteRedirectLocation
258
	}
259
	return r
260
}
261
262
func (r GetObjectResult) ToBytes() ([]byte, error) {
0 ignored issues
show
introduced by
exported method GetObjectResult.ToBytes should have comment or be unexported
Loading history...
263
	buf := new(bytes.Buffer)
264
	_, err := buf.ReadFrom(r.Body)
265
	defer r.Body.Close()
266
	if err != nil {
267
		return nil, err
268
	}
269
	return buf.Bytes(), err
270
}
271