Skip to content

Commit

Permalink
Panic if EnableHandler() option is passed an unrecognized handler n…
Browse files Browse the repository at this point in the history
…ame.
  • Loading branch information
jmalloc committed Jun 26, 2024
1 parent d90923f commit 1ff7c3a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 15 additions & 10 deletions engine/operationoption.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

import (
"fmt"
"time"

"github.com/dogmatiq/configkit"
Expand All @@ -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
Expand All @@ -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)
})
}
Expand Down Expand Up @@ -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
})
}
Expand All @@ -85,15 +86,19 @@ 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
})
}

// 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
})
}
Expand All @@ -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{
Expand All @@ -121,7 +126,7 @@ func newOperationOptions(options []OperationOption) *operationOptions {
}

for _, opt := range options {
opt.applyOperationOption(oo)
opt.applyOperationOption(e, oo)
}

return oo
Expand Down
8 changes: 4 additions & 4 deletions internal/fixtures/protobuf.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ff7c3a

Please sign in to comment.