From a8c4add2c61c3b99b78cd929f840805b9598bd2f Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Thu, 1 Feb 2024 13:57:19 +0100 Subject: [PATCH] chore: add pre-commit and format with gofumpt --- .editorconfig | 9 ++++++-- .pre-commit-config.yaml | 26 +++++++++++++++++++++++ CODEOWNERS | 2 +- _integration/amqp_admin.go | 17 ++++++--------- _integration/integration_test.go | 15 +++++++------ connection.go | 20 +++++++++++------ connection_test.go | 7 +++--- docs/message_processing.md | 1 - docs/naming.md | 1 - examples/event-stream/example_test.go | 9 ++++---- examples/request-response/example_test.go | 7 +++++- internal/handlers/handlers.go | 1 - mocks_test.go | 12 +++++++++-- publish.go | 6 ++---- 14 files changed, 90 insertions(+), 43 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.editorconfig b/.editorconfig index 06f4bc7..af5bbf1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,11 +1,16 @@ +# editorconfig.org root = true [*] +indent_style = space +indent_size = 2 end_of_line = lf -insert_final_newline = true charset = utf-8 trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false [*.go] indent_style = tab -indent_size = 4 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..a5ae938 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,26 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + args: + - --allow-multiple-documents + - id: check-added-large-files + - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook + rev: v9.11.0 + hooks: + - id: commitlint + stages: [ commit-msg ] + additional_dependencies: [ '@commitlint/config-conventional' ] + - repo: https://github.com/TekWizely/pre-commit-golang + rev: v1.0.0-rc.1 + hooks: + - id: go-mod-tidy + - id: go-imports + - id: go-test-mod + - id: go-fumpt + - id: golangci-lint-mod diff --git a/CODEOWNERS b/CODEOWNERS index bfbccfe..55e58dd 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1 +1 @@ -* @argoyle @peter-svensson \ No newline at end of file +* @argoyle @peter-svensson diff --git a/_integration/amqp_admin.go b/_integration/amqp_admin.go index e260a3f..fc87930 100644 --- a/_integration/amqp_admin.go +++ b/_integration/amqp_admin.go @@ -186,19 +186,16 @@ type Queue struct { } type Binding struct { - Source string `json:"source"` - Vhost string `json:"vhost"` - Destination string `json:"destination"` - DestinationType string `json:"destination_type"` - RoutingKey string `json:"routing_key"` - Arguments struct { - } `json:"arguments"` + Source string `json:"source"` + Vhost string `json:"vhost"` + Destination string `json:"destination"` + DestinationType string `json:"destination_type"` + RoutingKey string `json:"routing_key"` + Arguments struct{} `json:"arguments"` } func (q Queue) Named() string { return q.Name } -var ( - defaultExchanges = []string{"", "amq.direct", "amq.fanout", "amq.headers", "amq.match", "amq.rabbitmq.trace", "amq.topic"} -) +var defaultExchanges = []string{"", "amq.direct", "amq.fanout", "amq.headers", "amq.match", "amq.rabbitmq.trace", "amq.topic"} diff --git a/_integration/integration_test.go b/_integration/integration_test.go index e1aafde..b3b237b 100644 --- a/_integration/integration_test.go +++ b/_integration/integration_test.go @@ -200,7 +200,8 @@ func (suite *IntegrationTestSuite) Test_RequestResponse() { DestinationType: "queue", RoutingKey: routingKey, Arguments: struct{}{}, - }}, requestBinding) + }, + }, requestBinding) require.Equal(suite.T(), &Queue{ Arguments: QueueArguments{ @@ -224,8 +225,8 @@ func (suite *IntegrationTestSuite) Test_RequestResponse() { DestinationType: "queue", RoutingKey: routingKey, Arguments: struct{}{}, - }}, responseBinding) - + }, + }, responseBinding) } func (suite *IntegrationTestSuite) Test_EventStream_MultipleConsumers() { @@ -303,7 +304,8 @@ func (suite *IntegrationTestSuite) Test_EventStream_MultipleConsumers() { DestinationType: "queue", RoutingKey: routingKey, Arguments: struct{}{}, - }}, client1Binding) + }, + }, client1Binding) client2Queue, err := suite.admin.GetQueue("events.topic.exchange.queue.client2") require.NoError(suite.T(), err) @@ -330,8 +332,8 @@ func (suite *IntegrationTestSuite) Test_EventStream_MultipleConsumers() { DestinationType: "queue", RoutingKey: routingKey, Arguments: struct{}{}, - }}, client2Binding) - + }, + }, client2Binding) } func (suite *IntegrationTestSuite) Test_EventStream() { @@ -449,6 +451,7 @@ func (suite *IntegrationTestSuite) Test_EventStream() { require.Equal(suite.T(), 1, len(queuesAfterClose)) require.Equal(suite.T(), "events.topic.exchange.queue.client1", queuesAfterClose[0].Name) } + func (suite *IntegrationTestSuite) Test_WildcardRoutingKeys() { closer := make(chan bool, 2) wildcardRoutingKey := "test.#" diff --git a/connection.go b/connection.go index 24f82de..18b59bc 100644 --- a/connection.go +++ b/connection.go @@ -430,17 +430,23 @@ func (c *Connection) messageHandlerBindQueueToExchange(cfg *QueueBindingConfig) type kind string -const kindDirect = "direct" -const kindHeaders = "headers" -const kindTopic = "topic" +const ( + kindDirect = "direct" + kindHeaders = "headers" + kindTopic = "topic" +) -const headerService = "service" -const headerExpires = "x-expires" +const ( + headerService = "service" + headerExpires = "x-expires" +) const contentType = "application/json" -var deleteQueueAfter = 5 * 24 * time.Hour -var queueDeclareExpiration = amqp.Table{headerExpires: int(deleteQueueAfter.Seconds() * 1000)} +var ( + deleteQueueAfter = 5 * 24 * time.Hour + queueDeclareExpiration = amqp.Table{headerExpires: int(deleteQueueAfter.Seconds() * 1000)} +) func newConnection(serviceName string, uri amqp.URI) *Connection { return &Connection{ diff --git a/connection_test.go b/connection_test.go index 269b960..50bca5c 100644 --- a/connection_test.go +++ b/connection_test.go @@ -282,8 +282,10 @@ func Test_Consume(t *testing.T) { _, err := consume(channel, "q") require.NoError(t, err) require.Equal(t, 1, len(channel.Consumers)) - require.Equal(t, Consumer{queue: "q", - consumer: "", autoAck: false, exclusive: false, noLocal: false, noWait: false, args: amqp.Table{}}, channel.Consumers[0]) + require.Equal(t, Consumer{ + queue: "q", + consumer: "", autoAck: false, exclusive: false, noLocal: false, noWait: false, args: amqp.Table{}, + }, channel.Consumers[0]) } func Test_Publish(t *testing.T) { @@ -401,7 +403,6 @@ func TestResponseWrapper(t *testing.T) { } }) } - } func Test_DivertToMessageHandler(t *testing.T) { diff --git a/docs/message_processing.md b/docs/message_processing.md index ff436bb..ce2d118 100644 --- a/docs/message_processing.md +++ b/docs/message_processing.md @@ -44,4 +44,3 @@ If anything but `nil` is returned from `HandlerFunc` the message will be rejecte be processed again). If goamqp fails to unmarshal the JSON content in the message, the message will be rejected and **not** requeued again. - diff --git a/docs/naming.md b/docs/naming.md index ad3199d..b5408f8 100644 --- a/docs/naming.md +++ b/docs/naming.md @@ -47,4 +47,3 @@ A service that is listening for incoming *responses* will consume messages from ## References For full reference take a look at the [code](../naming.go) and [tests](../naming_test.go) - diff --git a/examples/event-stream/example_test.go b/examples/event-stream/example_test.go index 32537a2..2e675da 100644 --- a/examples/event-stream/example_test.go +++ b/examples/event-stream/example_test.go @@ -22,18 +22,19 @@ package event_stream import ( "context" "fmt" + "os" "time" . "github.com/sparetimecoders/goamqp" ) -var ( - amqpURL = "amqp://user:password@localhost:5672/test" -) +var amqpURL = "amqp://user:password@localhost:5672/test" func ExampleEventStream() { ctx := context.Background() - + if urlFromEnv := os.Getenv("AMQP_URL"); urlFromEnv != "" { + amqpURL = urlFromEnv + } orderServiceConnection := Must(NewFromURL("order-service", amqpURL)) orderPublisher := NewPublisher() err := orderServiceConnection.Start(ctx, diff --git a/examples/request-response/example_test.go b/examples/request-response/example_test.go index 0acb03c..70b34df 100644 --- a/examples/request-response/example_test.go +++ b/examples/request-response/example_test.go @@ -22,13 +22,18 @@ package request_response import ( "context" "fmt" + "os" "time" . "github.com/sparetimecoders/goamqp" ) +var amqpURL = "amqp://user:password@localhost:5672/test" + func ExampleRequestResponse() { - amqpURL := "amqp://user:password@localhost:5672/test" + if urlFromEnv := os.Getenv("AMQP_URL"); urlFromEnv != "" { + amqpURL = urlFromEnv + } routingKey := "key" ctx := context.Background() serviceConnection := Must(NewFromURL("service", amqpURL)) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index cca8187..797acfb 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -69,7 +69,6 @@ func (h *QueueHandlers[T]) Add(queueName, routingKey string, handler *T) error { if mappedRoutingKey, exists := queueHandlers.exists(routingKey); exists { return fmt.Errorf("routingkey %s overlaps %s for queue %s, consider using AddQueueNameSuffix", routingKey, mappedRoutingKey, queueName) - } queueHandlers.add(routingKey, handler) return nil diff --git a/mocks_test.go b/mocks_test.go index 1641d33..4f34ce6 100644 --- a/mocks_test.go +++ b/mocks_test.go @@ -109,10 +109,12 @@ func (a *MockAcknowledger) Ack(tag uint64, multiple bool) error { a.Acks <- Ack{tag, multiple} return nil } + func (a *MockAcknowledger) Nack(tag uint64, multiple bool, requeue bool) error { a.Nacks <- Nack{tag, multiple, requeue} return nil } + func (a *MockAcknowledger) Reject(tag uint64, requeue bool) error { a.Rejects <- Reject{tag, requeue} return nil @@ -170,6 +172,7 @@ func (m *MockAmqpChannel) Consume(queue, consumer string, autoAck, exclusive, no m.Consumers = append(m.Consumers, Consumer{queue, consumer, autoAck, exclusive, noLocal, noWait, args}) return m.Delivery, nil } + func (m *MockAmqpChannel) ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error { if m.ExchangeDeclarationError != nil { return *m.ExchangeDeclarationError @@ -178,9 +181,11 @@ func (m *MockAmqpChannel) ExchangeDeclare(name, kind string, durable, autoDelete m.ExchangeDeclarations = append(m.ExchangeDeclarations, ExchangeDeclaration{name, kind, durable, autoDelete, internal, noWait, args}) return nil } + func (m *MockAmqpChannel) Publish(exchange, key string, mandatory, immediate bool, msg amqp.Publishing) error { return m.PublishWithContext(context.Background(), exchange, key, mandatory, immediate, msg) } + func (m *MockAmqpChannel) PublishWithContext(ctx context.Context, exchange, key string, mandatory, immediate bool, msg amqp.Publishing) error { if key == "failed" { return errors.New("failed") @@ -194,6 +199,7 @@ func (m *MockAmqpChannel) PublishWithContext(ctx context.Context, exchange, key } return nil } + func (m *MockAmqpChannel) QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error) { if m.QueueDeclarationError != nil { return amqp.Queue{}, *m.QueueDeclarationError @@ -239,8 +245,10 @@ func NewMockAcknowledger() MockAcknowledger { } } -var _ amqpConnection = &MockAmqpConnection{} -var _ AmqpChannel = &MockAmqpChannel{} +var ( + _ amqpConnection = &MockAmqpConnection{} + _ AmqpChannel = &MockAmqpChannel{} +) func mockConnection(channel *MockAmqpChannel) *Connection { c := newConnection("svc", amqp.URI{}) diff --git a/publish.go b/publish.go index 2b3f17e..fef1c2c 100644 --- a/publish.go +++ b/publish.go @@ -37,10 +37,8 @@ type Publisher struct { defaultHeaders []Header } -var ( - // ErrNoRouteForMessageType when the published message cannot be routed. - ErrNoRouteForMessageType = fmt.Errorf("no routingkey configured for message of type") -) +// ErrNoRouteForMessageType when the published message cannot be routed. +var ErrNoRouteForMessageType = fmt.Errorf("no routingkey configured for message of type") // NewPublisher returns a publisher that can be used to send messages func NewPublisher() *Publisher {