From e2e38146380f2e8c3fc02fc8152106b0a474e66f Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Sat, 16 Dec 2023 22:08:01 +0100 Subject: [PATCH] feat: add option for ignoring missing mapped types --- connection.go | 10 +++++++--- connection_options.go | 8 ++++++++ connection_options_test.go | 8 ++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/connection.go b/connection.go index 8c081ea..97ed81f 100644 --- a/connection.go +++ b/connection.go @@ -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 @@ -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)) diff --git a/connection_options.go b/connection_options.go index 7db481e..2a8071f 100644 --- a/connection_options.go +++ b/connection_options.go @@ -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 { diff --git a/connection_options_test.go b/connection_options_test.go index 43fed22..42c6b2c 100644 --- a/connection_options_test.go +++ b/connection_options_test.go @@ -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) +}