From 1ff7c3adaf6b3f28a04e509a5a7204689e24ca06 Mon Sep 17 00:00:00 2001 From: James Harris Date: Wed, 26 Jun 2024 13:36:38 +1000 Subject: [PATCH] Panic if `EnableHandler()` option is passed an unrecognized handler name. --- engine/engine.go | 4 ++-- engine/operationoption.go | 25 +++++++++++++++---------- internal/fixtures/protobuf.pb.go | 8 ++++---- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/engine/engine.go b/engine/engine.go index 9fd5b666..42ac91c0 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -89,7 +89,7 @@ func (e *Engine) Tick( ctx context.Context, options ...OperationOption, ) error { - oo := newOperationOptions(options) + oo := newOperationOptions(e, options) oo.observers.Notify( fact.TickCycleBegun{ @@ -198,7 +198,7 @@ func (e *Engine) Dispatch( )) } - oo := newOperationOptions(options) + oo := newOperationOptions(e, options) t := message.TypeOf(m) if _, ok := e.routes[t]; !ok { diff --git a/engine/operationoption.go b/engine/operationoption.go index 0f5a4f9e..f446fb9e 100644 --- a/engine/operationoption.go +++ b/engine/operationoption.go @@ -1,6 +1,7 @@ package engine import ( + "fmt" "time" "github.com/dogmatiq/configkit" @@ -10,13 +11,13 @@ import ( // OperationOption applies optional settings while dispatching a message or // performing a tick. type OperationOption interface { - applyOperationOption(*operationOptions) + applyOperationOption(*Engine, *operationOptions) } -type operationOptionFunc func(*operationOptions) +type operationOptionFunc func(*Engine, *operationOptions) -func (f operationOptionFunc) applyOperationOption(opts *operationOptions) { - f(opts) +func (f operationOptionFunc) applyOperationOption(e *Engine, opts *operationOptions) { + f(e, opts) } // WithObserver returns an option that registers the given observer for the @@ -28,7 +29,7 @@ func WithObserver(o fact.Observer) OperationOption { panic("observer must not be nil") } - return operationOptionFunc(func(oo *operationOptions) { + return operationOptionFunc(func(_ *Engine, oo *operationOptions) { oo.observers = append(oo.observers, o) }) } @@ -70,7 +71,7 @@ func EnableProjections(enabled bool) OperationOption { func enableHandlerType(t configkit.HandlerType, enabled bool) OperationOption { t.MustValidate() - return operationOptionFunc(func(oo *operationOptions) { + return operationOptionFunc(func(_ *Engine, oo *operationOptions) { oo.enabledHandlerTypes[t] = enabled }) } @@ -85,7 +86,11 @@ func EnableHandler(name string, enabled bool) OperationOption { panic(err) } - return operationOptionFunc(func(oo *operationOptions) { + return operationOptionFunc(func(e *Engine, oo *operationOptions) { + if _, ok := e.controllers[name]; !ok { + panic(fmt.Sprintf("the application does not have a handler named %q", name)) + } + oo.enabledHandlers[name] = enabled }) } @@ -93,7 +98,7 @@ func EnableHandler(name string, enabled bool) OperationOption { // WithCurrentTime returns an operation option that sets the engine's current // time. func WithCurrentTime(t time.Time) OperationOption { - return operationOptionFunc(func(oo *operationOptions) { + return operationOptionFunc(func(_ *Engine, oo *operationOptions) { oo.now = t }) } @@ -108,7 +113,7 @@ type operationOptions struct { } // newOperationOptions returns a new operationOptions with the given options. -func newOperationOptions(options []OperationOption) *operationOptions { +func newOperationOptions(e *Engine, options []OperationOption) *operationOptions { oo := &operationOptions{ now: time.Now(), enabledHandlerTypes: map[configkit.HandlerType]bool{ @@ -121,7 +126,7 @@ func newOperationOptions(options []OperationOption) *operationOptions { } for _, opt := range options { - opt.applyOperationOption(oo) + opt.applyOperationOption(e, oo) } return oo diff --git a/internal/fixtures/protobuf.pb.go b/internal/fixtures/protobuf.pb.go index ff19522e..93d0ea24 100644 --- a/internal/fixtures/protobuf.pb.go +++ b/internal/fixtures/protobuf.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.33.0 -// protoc v4.23.4 +// protoc-gen-go v1.34.2 +// protoc v5.27.2 // source: github.com/dogmatiq/testkit/internal/fixtures/protobuf.proto package fixtures @@ -98,7 +98,7 @@ func file_github_com_dogmatiq_testkit_internal_fixtures_protobuf_proto_rawDescGZ } var file_github_com_dogmatiq_testkit_internal_fixtures_protobuf_proto_msgTypes = make([]protoimpl.MessageInfo, 1) -var file_github_com_dogmatiq_testkit_internal_fixtures_protobuf_proto_goTypes = []interface{}{ +var file_github_com_dogmatiq_testkit_internal_fixtures_protobuf_proto_goTypes = []any{ (*ProtoMessage)(nil), // 0: dogmatiq.testkit.fixtures.ProtoMessage } var file_github_com_dogmatiq_testkit_internal_fixtures_protobuf_proto_depIdxs = []int32{ @@ -115,7 +115,7 @@ func file_github_com_dogmatiq_testkit_internal_fixtures_protobuf_proto_init() { return } if !protoimpl.UnsafeEnabled { - file_github_com_dogmatiq_testkit_internal_fixtures_protobuf_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + file_github_com_dogmatiq_testkit_internal_fixtures_protobuf_proto_msgTypes[0].Exporter = func(v any, i int) any { switch v := v.(*ProtoMessage); i { case 0: return &v.state