Skip to content

Commit

Permalink
chore: add pre-commit and format with gofumpt
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-svensson committed Feb 1, 2024
1 parent 543c932 commit a8c4add
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 43 deletions.
9 changes: 7 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @argoyle @peter-svensson
* @argoyle @peter-svensson
17 changes: 7 additions & 10 deletions _integration/amqp_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
15 changes: 9 additions & 6 deletions _integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ func (suite *IntegrationTestSuite) Test_RequestResponse() {
DestinationType: "queue",
RoutingKey: routingKey,
Arguments: struct{}{},
}}, requestBinding)
},
}, requestBinding)

require.Equal(suite.T(), &Queue{
Arguments: QueueArguments{
Expand All @@ -224,8 +225,8 @@ func (suite *IntegrationTestSuite) Test_RequestResponse() {
DestinationType: "queue",
RoutingKey: routingKey,
Arguments: struct{}{},
}}, responseBinding)

},
}, responseBinding)
}

func (suite *IntegrationTestSuite) Test_EventStream_MultipleConsumers() {
Expand Down Expand Up @@ -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)
Expand All @@ -330,8 +332,8 @@ func (suite *IntegrationTestSuite) Test_EventStream_MultipleConsumers() {
DestinationType: "queue",
RoutingKey: routingKey,
Arguments: struct{}{},
}}, client2Binding)

},
}, client2Binding)
}

func (suite *IntegrationTestSuite) Test_EventStream() {
Expand Down Expand Up @@ -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.#"
Expand Down
20 changes: 13 additions & 7 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -401,7 +403,6 @@ func TestResponseWrapper(t *testing.T) {
}
})
}

}

func Test_DivertToMessageHandler(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion docs/message_processing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 0 additions & 1 deletion docs/naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

9 changes: 5 additions & 4 deletions examples/event-stream/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion examples/request-response/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 0 additions & 1 deletion internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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{})
Expand Down
6 changes: 2 additions & 4 deletions publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a8c4add

Please sign in to comment.