-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gabriel Cataldo
committed
Dec 15, 2023
1 parent
bb0f821
commit 19d2891
Showing
15 changed files
with
834 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/GabrielHCataldo/go-aws-sqs/sqs" | ||
"github.com/GabrielHCataldo/go-aws-sqs/sqs/option" | ||
"github.com/GabrielHCataldo/go-logger/logger" | ||
"os" | ||
"os/signal" | ||
"time" | ||
) | ||
|
||
type test struct { | ||
Name string `json:"name,omitempty"` | ||
BirthDate time.Time `json:"birthDate,omitempty"` | ||
Emails []string `json:"emails,omitempty"` | ||
Bank bank `json:"bank,omitempty"` | ||
Map map[string]any | ||
} | ||
|
||
type bank struct { | ||
Account string `json:"account,omitempty"` | ||
Digits string `json:"digits,omitempty"` | ||
Balance float64 `json:"balance,omitempty"` | ||
} | ||
|
||
type messageAttTest struct { | ||
Name string `json:"account,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
Balance float64 `json:"balance,omitempty"` | ||
Bool bool `json:"bool"` | ||
Int int `json:"int"` | ||
SubStruct test `json:"subStruct,omitempty"` | ||
PointerTest *test `json:"pointerBank,omitempty"` | ||
Map map[string]any | ||
Any any | ||
EmptyString string `json:"emptyString,omitempty"` | ||
HideString string `json:"-"` | ||
} | ||
|
||
func main() { | ||
simpleReceiveMessage() | ||
simpleReceiveMessageStruct() | ||
receiveMessage() | ||
completeOptions() | ||
simpleReceiveMessageAsync() | ||
} | ||
|
||
func simpleReceiveMessage() { | ||
sqs.SimpleReceiveMessage(os.Getenv("SQS_QUEUE_TEST_STRING_URL"), handlerSimple) | ||
} | ||
|
||
func simpleReceiveMessageStruct() { | ||
sqs.SimpleReceiveMessage(os.Getenv("SQS_QUEUE_TEST_URL"), handler) | ||
} | ||
|
||
func receiveMessage() { | ||
sqs.ReceiveMessage(os.Getenv("SQS_QUEUE_TEST_STRING_URL"), handlerReceiveMessage) | ||
} | ||
|
||
func completeOptions() { | ||
opt := option.NewConsumer(). | ||
// HTTP communication customization options with AWS SQS | ||
SetHttpClient(option.HttpClient{}). | ||
// print logs (default: false) | ||
SetDebugMode(true). | ||
// If true remove the message from the queue after successfully processed (handler error return is null) (default: false) | ||
SetDeleteMessageProcessedSuccess(true). | ||
// Duration time to process the message, timeout applied in the past context. (default: 5 seconds) | ||
SetConsumerMessageTimeout(5 * time.Second). | ||
// Delay to run the next search for messages in the queue (default: 0) | ||
SetDelayQueryLoop(5 * time.Second). | ||
// The maximum number of messages to return. 1 a 10 (default: 10) | ||
SetMaxNumberOfMessages(10). | ||
// The maximum number of messages to return. 1 a 10 (default: 0) | ||
SetVisibilityTimeout(5 * time.Second). | ||
// The duration that the received messages are hidden from subsequent | ||
// retrieve requests after being retrieved by a ReceiveMessage request. | ||
SetReceiveRequestAttemptId(""). | ||
// The duration for which the call waits for a message to arrive in | ||
// the queue before returning (default: 0) | ||
SetWaitTimeSeconds(1 * time.Second) | ||
sqs.SimpleReceiveMessage(os.Getenv("SQS_QUEUE_TEST_URL"), handler, opt) | ||
} | ||
|
||
func simpleReceiveMessageAsync() { | ||
sqs.SimpleReceiveMessageAsync(os.Getenv("SQS_QUEUE_TEST_URL"), handler, option.NewConsumer().SetDebugMode(true)) | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt) | ||
select { | ||
case <-c: | ||
logger.Info("Stopped application!") | ||
} | ||
} | ||
|
||
func handler(ctx *sqs.SimpleContext[test]) error { | ||
logger.Debug("ctx simple body struct to process message:", ctx) | ||
return nil | ||
} | ||
|
||
func handlerSimple(ctx *sqs.SimpleContext[string]) error { | ||
logger.Debug("ctx simple to process message:", ctx) | ||
return nil | ||
} | ||
|
||
func handlerReceiveMessage(ctx *sqs.Context[string, messageAttTest]) error { | ||
logger.Debug("ctx to process message:", ctx) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/GabrielHCataldo/go-aws-sqs/sqs" | ||
"github.com/GabrielHCataldo/go-aws-sqs/sqs/option" | ||
"github.com/GabrielHCataldo/go-logger/logger" | ||
"os" | ||
"time" | ||
) | ||
|
||
type test struct { | ||
Name string `json:"name,omitempty"` | ||
BirthDate time.Time `json:"birthDate,omitempty"` | ||
Emails []string `json:"emails,omitempty"` | ||
Bank bank `json:"bank,omitempty"` | ||
Map map[string]any | ||
} | ||
|
||
type bank struct { | ||
Account string `json:"account,omitempty"` | ||
Digits string `json:"digits,omitempty"` | ||
Balance float64 `json:"balance,omitempty"` | ||
} | ||
|
||
type messageAttTest struct { | ||
Name string `json:"account,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
Balance float64 `json:"balance,omitempty"` | ||
Bool bool `json:"bool"` | ||
Int int `json:"int"` | ||
SubStruct test `json:"subStruct,omitempty"` | ||
PointerTest *test `json:"pointerBank,omitempty"` | ||
Map map[string]any | ||
Any any | ||
EmptyString string `json:"emptyString,omitempty"` | ||
HideString string `json:"-"` | ||
} | ||
|
||
func main() { | ||
simple() | ||
simpleAsync() | ||
structBody() | ||
mapBody() | ||
completeOptions() | ||
} | ||
|
||
func simple() { | ||
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) | ||
defer cancel() | ||
body := "body test" | ||
message, err := sqs.SendMessage(ctx, os.Getenv("SQS_QUEUE_TEST_STRING_URL"), body) | ||
if err != nil { | ||
logger.Error("error send message:", err) | ||
} else { | ||
logger.Info("message sent successfully:", message) | ||
} | ||
} | ||
|
||
func simpleAsync() { | ||
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) | ||
defer cancel() | ||
body := "body test" | ||
sqs.SendMessageAsync(ctx, os.Getenv("SQS_QUEUE_TEST_STRING_URL"), body) | ||
} | ||
|
||
func structBody() { | ||
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) | ||
defer cancel() | ||
body := initTestStruct() | ||
message, err := sqs.SendMessage(ctx, os.Getenv("SQS_QUEUE_TEST_URL"), body) | ||
if err != nil { | ||
logger.Error("error send message:", err) | ||
} else { | ||
logger.Info("message sent successfully:", message) | ||
} | ||
} | ||
|
||
func mapBody() { | ||
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) | ||
defer cancel() | ||
body := initTestMap() | ||
message, err := sqs.SendMessage(ctx, os.Getenv("SQS_QUEUE_TEST_STRING_URL"), body) | ||
if err != nil { | ||
logger.Error("error send message:", err) | ||
} else { | ||
logger.Info("message sent successfully:", message) | ||
} | ||
} | ||
|
||
func completeOptions() { | ||
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) | ||
defer cancel() | ||
body := initTestStruct() | ||
opt := option.NewProducer(). | ||
// HTTP communication customization options with AWS SQS | ||
SetHttpClient(option.HttpClient{}). | ||
// print logs (default: false) | ||
SetDebugMode(true). | ||
// delay to delay the availability of message processing (default: 0) | ||
SetDelaySeconds(5 * time.Second). | ||
// Message attributes, must be of type Map or Struct, other types are not acceptable. | ||
SetMessageAttributes(initMessageAttTest()). | ||
// The message system attribute to send | ||
SetMessageSystemAttributes(option.MessageSystemAttributes{}). | ||
// This parameter applies only to FIFO (first-in-first-out) queues. The token used for deduplication of sent messages. | ||
SetMessageDeduplicationId(""). | ||
// This parameter applies only to FIFO (first-in-first-out) queues. The tag that specifies that a message belongs to a specific message group. | ||
SetMessageGroupId("") | ||
message, err := sqs.SendMessage(ctx, os.Getenv("SQS_QUEUE_TEST_URL"), body, opt) | ||
if err != nil { | ||
logger.Error("error send message:", err) | ||
} else { | ||
logger.Info("message sent successfully:", message) | ||
} | ||
} | ||
|
||
func initTestStruct() test { | ||
b := bank{ | ||
Account: "123456", | ||
Digits: "2", | ||
Balance: 200.12, | ||
} | ||
return test{ | ||
Name: "Test Name", | ||
BirthDate: time.Now(), | ||
Emails: []string{"[email protected]", "[email protected]", "[email protected]"}, | ||
Bank: b, | ||
Map: map[string]any{"int": 1, "bool": true, "float": 1.23, "string": "text test"}, | ||
} | ||
} | ||
|
||
func initTestMap() map[string]any { | ||
return map[string]any{"int": 1, "bool": true, "float": 1.23, "string": "text test"} | ||
} | ||
|
||
func initMessageAttTest() messageAttTest { | ||
t := initTestStruct() | ||
return messageAttTest{ | ||
Name: "Name test producer", | ||
Text: "Text field", | ||
Balance: 10.23, | ||
Bool: true, | ||
Int: 3, | ||
SubStruct: t, | ||
PointerTest: &t, | ||
Map: initTestMap(), | ||
HideString: "hide test", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.