From 9123284318d9075e9af705b9c48b601798f101aa Mon Sep 17 00:00:00 2001 From: James Harris Date: Wed, 10 Jul 2024 07:40:46 +1000 Subject: [PATCH] Implement `Disable()` on handler configurers. --- aggregate.go | 9 ++++++--- aggregate_test.go | 24 +++++++++++++++++++++++- go.mod | 2 +- go.sum | 4 ++-- handler.go | 15 +++++++++++++++ handlerconfigurer.go | 5 +++++ integration.go | 9 ++++++--- integration_test.go | 18 +++++++++++++++++- internal/entity/entity.go | 6 ++++++ process.go | 9 ++++++--- process_test.go | 18 +++++++++++++++++- projection.go | 9 ++++++--- projection_test.go | 18 +++++++++++++++++- 13 files changed, 127 insertions(+), 19 deletions(-) diff --git a/aggregate.go b/aggregate.go index 2bf345fa..2c4105d7 100644 --- a/aggregate.go +++ b/aggregate.go @@ -29,8 +29,10 @@ type RichAggregate interface { // configuration related panic values to errors. func FromAggregate(h dogma.AggregateMessageHandler) RichAggregate { cfg := &aggregate{ - entity: entity{ - rt: reflect.TypeOf(h), + handler: handler{ + entity: entity{ + rt: reflect.TypeOf(h), + }, }, impl: h, } @@ -40,6 +42,7 @@ func FromAggregate(h dogma.AggregateMessageHandler) RichAggregate { entityConfigurer: entityConfigurer{ entity: &cfg.entity, }, + handler: &cfg.handler, }, } @@ -54,7 +57,7 @@ func FromAggregate(h dogma.AggregateMessageHandler) RichAggregate { // aggregate is an implementation of RichAggregate. type aggregate struct { - entity + handler impl dogma.AggregateMessageHandler } diff --git a/aggregate_test.go b/aggregate_test.go index aeb8e8d2..26a931ae 100644 --- a/aggregate_test.go +++ b/aggregate_test.go @@ -34,7 +34,7 @@ var _ = Describe("func FromAggregate()", func() { When("the configuration is valid", func() { var cfg RichAggregate - BeforeEach(func() { + JustBeforeEach(func() { cfg = FromAggregate(handler) }) @@ -124,11 +124,33 @@ var _ = Describe("func FromAggregate()", func() { }) }) + Describe("func IsDisabled()", func() { + It("returns false", func() { + Expect(cfg.IsDisabled()).To(BeFalse()) + }) + }) + Describe("func Handler()", func() { It("returns the underlying handler", func() { Expect(cfg.Handler()).To(BeIdenticalTo(handler)) }) }) + + When("the handler is disabled", func() { + BeforeEach(func() { + configure := handler.ConfigureFunc + handler.ConfigureFunc = func(c dogma.AggregateConfigurer) { + configure(c) + c.Disable() + } + }) + + Describe("func IsDisabled()", func() { + It("returns true", func() { + Expect(cfg.IsDisabled()).To(BeTrue()) + }) + }) + }) }) DescribeTable( diff --git a/go.mod b/go.mod index f3256078..c6a07734 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/dogmatiq/configkit go 1.21 require ( - github.com/dogmatiq/dogma v0.13.0 + github.com/dogmatiq/dogma v0.13.1-0.20240708211023-af9214272677 github.com/dogmatiq/iago v0.4.0 github.com/dogmatiq/interopspec v0.5.3 github.com/emicklei/dot v1.6.2 diff --git a/go.sum b/go.sum index ee91c24e..7501045c 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dogmatiq/dogma v0.13.0 h1:MKk9MHErGKD53Y+43I4fcoPZMQjX0N2DUZEc4rLp+Hk= -github.com/dogmatiq/dogma v0.13.0/go.mod h1:9lyVA+6V2+E/exV0IrBOrkUiyFwIATEhv+b0vnB2umQ= +github.com/dogmatiq/dogma v0.13.1-0.20240708211023-af9214272677 h1:2DDD3+89VQre3EOvkJL1syN5EUXNU06uAkXE5/fsaBU= +github.com/dogmatiq/dogma v0.13.1-0.20240708211023-af9214272677/go.mod h1:9lyVA+6V2+E/exV0IrBOrkUiyFwIATEhv+b0vnB2umQ= github.com/dogmatiq/iago v0.4.0 h1:57nZqVT34IZxtCZEW/RFif7DNUEjMXgevfr/Mmd0N8I= github.com/dogmatiq/iago v0.4.0/go.mod h1:fishMWBtzYcjgis6d873VTv9kFm/wHYLOzOyO9ECBDc= github.com/dogmatiq/interopspec v0.5.3 h1:AES184nLWcek8/vafykZJLmm59NlRiYiC40Tll8hEeg= diff --git a/handler.go b/handler.go index b722c115..49dbd71c 100644 --- a/handler.go +++ b/handler.go @@ -6,6 +6,9 @@ type Handler interface { // HandlerType returns the type of handler. HandlerType() HandlerType + + // IsDisabled returns true if the handler is disabled. + IsDisabled() bool } // RichHandler is a specialization of the Handler interface that exposes @@ -15,6 +18,9 @@ type RichHandler interface { // HandlerType returns the type of handler. HandlerType() HandlerType + + // IsDisabled returns true if the handler is disabled. + IsDisabled() bool } // IsHandlerEqual compares two handlers for equality. @@ -38,3 +44,12 @@ func IsHandlerEqual(a, b Handler) bool { a.HandlerType() == b.HandlerType() && a.MessageNames().IsEqual(b.MessageNames()) } + +type handler struct { + entity + isDisabled bool +} + +func (h *handler) IsDisabled() bool { + return h.isDisabled +} diff --git a/handlerconfigurer.go b/handlerconfigurer.go index d2493a98..044b68d5 100644 --- a/handlerconfigurer.go +++ b/handlerconfigurer.go @@ -19,6 +19,7 @@ import ( // - [dogma.ProjectionConfigurer] type handlerConfigurer struct { entityConfigurer + handler *handler } func (c *handlerConfigurer) route(r dogma.Route) { @@ -155,3 +156,7 @@ func (c *handlerConfigurer) mustProduce(r message.Role) { route, ) } + +func (c *handlerConfigurer) Disable(...dogma.DisableOption) { + c.handler.isDisabled = true +} diff --git a/integration.go b/integration.go index 16d68538..7fe85858 100644 --- a/integration.go +++ b/integration.go @@ -29,8 +29,10 @@ type RichIntegration interface { // configuration related panic values to errors. func FromIntegration(h dogma.IntegrationMessageHandler) RichIntegration { cfg := &integration{ - entity: entity{ - rt: reflect.TypeOf(h), + handler: handler{ + entity: entity{ + rt: reflect.TypeOf(h), + }, }, impl: h, } @@ -40,6 +42,7 @@ func FromIntegration(h dogma.IntegrationMessageHandler) RichIntegration { entityConfigurer: entityConfigurer{ entity: &cfg.entity, }, + handler: &cfg.handler, }, } @@ -53,7 +56,7 @@ func FromIntegration(h dogma.IntegrationMessageHandler) RichIntegration { // integration is an implementation of RichIntegration. type integration struct { - entity + handler impl dogma.IntegrationMessageHandler } diff --git a/integration_test.go b/integration_test.go index b6c9d4d8..25ca3e59 100644 --- a/integration_test.go +++ b/integration_test.go @@ -34,7 +34,7 @@ var _ = Describe("func FromIntegration()", func() { When("the configuration is valid", func() { var cfg RichIntegration - BeforeEach(func() { + JustBeforeEach(func() { cfg = FromIntegration(handler) }) @@ -130,6 +130,22 @@ var _ = Describe("func FromIntegration()", func() { }) }) + When("the handler is disabled", func() { + BeforeEach(func() { + configure := handler.ConfigureFunc + handler.ConfigureFunc = func(c dogma.IntegrationConfigurer) { + configure(c) + c.Disable() + } + }) + + Describe("func IsDisabled()", func() { + It("returns true", func() { + Expect(cfg.IsDisabled()).To(BeTrue()) + }) + }) + }) + When("the handler does not configure any event routes", func() { BeforeEach(func() { handler.ConfigureFunc = func(c dogma.IntegrationConfigurer) { diff --git a/internal/entity/entity.go b/internal/entity/entity.go index 04fe1456..8132bae7 100644 --- a/internal/entity/entity.go +++ b/internal/entity/entity.go @@ -45,6 +45,7 @@ type Handler struct { TypeNameValue string MessageNamesValue configkit.EntityMessageNames HandlerTypeValue configkit.HandlerType + IsDisabledValue bool } // Identity returns the identity of the entity. @@ -67,6 +68,11 @@ func (h *Handler) HandlerType() configkit.HandlerType { return h.HandlerTypeValue } +// IsDisabled returns true if the handler is disabled. +func (h *Handler) IsDisabled() bool { + return h.IsDisabledValue +} + // AcceptVisitor calls the appropriate method on v for this entity type. func (h *Handler) AcceptVisitor(ctx context.Context, v configkit.Visitor) error { h.HandlerTypeValue.MustValidate() diff --git a/process.go b/process.go index 849b0d0b..fd33f1f2 100644 --- a/process.go +++ b/process.go @@ -29,8 +29,10 @@ type RichProcess interface { // configuration related panic values to errors. func FromProcess(h dogma.ProcessMessageHandler) RichProcess { cfg := &process{ - entity: entity{ - rt: reflect.TypeOf(h), + handler: handler{ + entity: entity{ + rt: reflect.TypeOf(h), + }, }, impl: h, } @@ -40,6 +42,7 @@ func FromProcess(h dogma.ProcessMessageHandler) RichProcess { entityConfigurer: entityConfigurer{ entity: &cfg.entity, }, + handler: &cfg.handler, }, } @@ -54,7 +57,7 @@ func FromProcess(h dogma.ProcessMessageHandler) RichProcess { // process is an implementation of RichProcess. type process struct { - entity + handler impl dogma.ProcessMessageHandler } diff --git a/process_test.go b/process_test.go index b6bfe865..d9d642e2 100644 --- a/process_test.go +++ b/process_test.go @@ -35,7 +35,7 @@ var _ = Describe("func FromProcess()", func() { When("the configuration is valid", func() { var cfg RichProcess - BeforeEach(func() { + JustBeforeEach(func() { cfg = FromProcess(handler) }) @@ -134,6 +134,22 @@ var _ = Describe("func FromProcess()", func() { Expect(cfg.Handler()).To(BeIdenticalTo(handler)) }) }) + + When("the handler is disabled", func() { + BeforeEach(func() { + configure := handler.ConfigureFunc + handler.ConfigureFunc = func(c dogma.ProcessConfigurer) { + configure(c) + c.Disable() + } + }) + + Describe("func IsDisabled()", func() { + It("returns true", func() { + Expect(cfg.IsDisabled()).To(BeTrue()) + }) + }) + }) }) DescribeTable( diff --git a/projection.go b/projection.go index 70869732..87c4d8f7 100644 --- a/projection.go +++ b/projection.go @@ -32,8 +32,10 @@ type RichProjection interface { // configuration related panic values to errors. func FromProjection(h dogma.ProjectionMessageHandler) RichProjection { cfg := &projection{ - entity: entity{ - rt: reflect.TypeOf(h), + handler: handler{ + entity: entity{ + rt: reflect.TypeOf(h), + }, }, impl: h, deliveryPolicy: dogma.UnicastProjectionDeliveryPolicy{}, @@ -44,6 +46,7 @@ func FromProjection(h dogma.ProjectionMessageHandler) RichProjection { entityConfigurer: entityConfigurer{ entity: &cfg.entity, }, + handler: &cfg.handler, }, } @@ -61,7 +64,7 @@ func FromProjection(h dogma.ProjectionMessageHandler) RichProjection { // projection is an implementation of RichProjection. type projection struct { - entity + handler impl dogma.ProjectionMessageHandler deliveryPolicy dogma.ProjectionDeliveryPolicy diff --git a/projection_test.go b/projection_test.go index 3dab23d3..db1c73ac 100644 --- a/projection_test.go +++ b/projection_test.go @@ -36,7 +36,7 @@ var _ = Describe("func FromProjection()", func() { When("the configuration is valid", func() { var cfg RichProjection - BeforeEach(func() { + JustBeforeEach(func() { cfg = FromProjection(handler) }) @@ -137,6 +137,22 @@ var _ = Describe("func FromProjection()", func() { Expect(cfg.Handler()).To(BeIdenticalTo(handler)) }) }) + + When("the handler is disabled", func() { + BeforeEach(func() { + configure := handler.ConfigureFunc + handler.ConfigureFunc = func(c dogma.ProjectionConfigurer) { + configure(c) + c.Disable() + } + }) + + Describe("func IsDisabled()", func() { + It("returns true", func() { + Expect(cfg.IsDisabled()).To(BeTrue()) + }) + }) + }) }) DescribeTable(