Skip to content

Commit

Permalink
feat: add option for ignoring missing mapped types
Browse files Browse the repository at this point in the history
  • Loading branch information
argoyle committed Dec 16, 2023
1 parent b6473ce commit e2e3814
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
10 changes: 7 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ type Connection struct {
// messageLogger defaults to noOpMessageLogger, can be overridden with UseMessageLogger
messageLogger MessageLogger
// errorLogF defaults to noOpLogger, can be overridden with UseLogger
errorLogF errorLogf
typeToKey map[reflect.Type]string
keyToType map[string]reflect.Type
errorLogF errorLogf
typeToKey map[reflect.Type]string
keyToType map[string]reflect.Type
ignoreMissingMappedTypes bool
}

// ServiceResponsePublisher represents the function that is called to publish a response
Expand Down Expand Up @@ -175,6 +176,9 @@ func (c *Connection) TypeMappingHandler(handler HandlerFunc) HandlerFunc {
routingKey := headers["routing-key"].(string)
typ, exists := c.keyToType[routingKey]
if !exists {
if c.ignoreMissingMappedTypes {
return nil, nil
}
return nil, fmt.Errorf("no mapped type found for routing key '%s'", routingKey)
}
body := []byte(*msg.(*json.RawMessage))
Expand Down
8 changes: 8 additions & 0 deletions connection_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func WithTypeMapping(routingKey string, msgType any) Setup {
}
}

// IgnoreMissingMappedTypes tells the system to ignore any missing TypeMappings.
func IgnoreMissingMappedTypes(ignore bool) Setup {
return func(conn *Connection) error {
conn.ignoreMissingMappedTypes = ignore
return nil
}
}

// CloseListener receives a callback when the AMQP Channel gets closed
func CloseListener(e chan error) Setup {
return func(c *Connection) error {
Expand Down
8 changes: 8 additions & 0 deletions connection_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,11 @@ func Test_WithTypeMapping_TypeAlreadyExist(t *testing.T) {
err = WithTypeMapping("other", TestMessage{})(conn)
assert.EqualError(t, err, "mapping for type 'goamqp.TestMessage' already registered to routing key 'key'")
}

func Test_IgnoreMissingMappedTypes(t *testing.T) {
channel := NewMockAmqpChannel()
conn := mockConnection(channel)
err := IgnoreMissingMappedTypes(true)(conn)
assert.NoError(t, err)
assert.True(t, conn.ignoreMissingMappedTypes)
}

0 comments on commit e2e3814

Please sign in to comment.