/
util.go
executable file
·102 lines (69 loc) · 2.38 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package sqs
import (
"encoding/base64"
"encoding/json"
"errors"
"reflect"
"github.com/aws/aws-sdk-go/service/sqs"
intError "github.com/andream16/aws-sdk-go-bindings/internal/error"
)
// NewCreateQueueInput creates a new queue given its name
func NewCreateQueueInput(queueName string) (*sqs.CreateQueueInput, error) {
if len(queueName) == 0 {
return nil, intError.Format(QueueName, ErrEmptyParameter)
}
out := new(sqs.CreateQueueInput)
out = out.SetQueueName(queueName)
return out, nil
}
// NewGetQueueAttributesInput returns a new *sqs.GetQueueAttributesInput given a queueUrl
func NewGetQueueAttributesInput(queueUrl string) (*sqs.GetQueueAttributesInput, error) {
if len(queueUrl) == 0 {
return nil, intError.Format(QueueUrl, ErrEmptyParameter)
}
out := new(sqs.GetQueueAttributesInput)
out = out.SetQueueUrl(queueUrl)
return out, nil
}
// NewSendMessageInput returns a new *sqs.SendMessageInput initialized with queueUrl and messageBody.
// If base64Encode = true then the messageBody will be encoded in base64
func NewSendMessageInput(input interface{}, queueUrl string, base64Encode bool) (*sqs.SendMessageInput, error) {
if reflect.DeepEqual(reflect.TypeOf(input).Kind(), reflect.Ptr) {
return nil, intError.Format(Input, ErrNoPointerParameterAllowed)
}
if len(queueUrl) == 0 {
return nil, intError.Format(QueueUrl, ErrEmptyParameter)
}
out := new(sqs.SendMessageInput)
msgBody, err := marshalStructToJson(input)
if err != nil {
return nil, err
}
msgStringBody := string(msgBody)
if base64Encode {
msgStringBody = base64.StdEncoding.EncodeToString(msgBody)
}
out = out.SetMessageBody(msgStringBody)
out = out.SetQueueUrl(queueUrl)
return out, nil
}
// NewGetQueueUrlInput returns a new *sqs.GetQueueUrlInput given a queue name
func NewGetQueueUrlInput(queueName string) (*sqs.GetQueueUrlInput, error) {
if len(queueName) == 0 {
return nil, intError.Format(QueueName, ErrEmptyParameter)
}
out := new(sqs.GetQueueUrlInput)
out = out.SetQueueName(queueName)
return out, nil
}
// marshalStructToJson marshals input into a []byte contains its json encoding
func marshalStructToJson(input interface{}) ([]byte, error) {
if reflect.DeepEqual(reflect.TypeOf(input).Kind(), reflect.Ptr) {
return nil, errors.New(ErrNoPointerParameterAllowed)
}
b, marshalErr := json.Marshal(input)
if marshalErr != nil {
return nil, marshalErr
}
return b, nil
}