From 28a447280855650972e2c53376bc0ea07702170e Mon Sep 17 00:00:00 2001 From: James Harris Date: Tue, 15 Oct 2024 13:24:20 +1000 Subject: [PATCH] Fix filenames. --- config/{handler_aggregate.go => aggregate.go} | 0 ...er_aggregate_test.go => aggregate_test.go} | 0 config/handlertype.go | 117 +++++++++++++++++ ...typeswitch_test.go => handlertype_test.go} | 0 config/handlertypeswitch.go | 122 ------------------ ...{handler_integration.go => integration.go} | 0 ...ntegration_test.go => integration_test.go} | 0 config/{handler_process.go => process.go} | 0 ...andler_process_test.go => process_test.go} | 0 .../{handler_projection.go => projection.go} | 0 ..._projection_test.go => projection_test.go} | 0 11 files changed, 117 insertions(+), 122 deletions(-) rename config/{handler_aggregate.go => aggregate.go} (100%) rename config/{handler_aggregate_test.go => aggregate_test.go} (100%) rename config/{handlertypeswitch_test.go => handlertype_test.go} (100%) delete mode 100644 config/handlertypeswitch.go rename config/{handler_integration.go => integration.go} (100%) rename config/{handler_integration_test.go => integration_test.go} (100%) rename config/{handler_process.go => process.go} (100%) rename config/{handler_process_test.go => process_test.go} (100%) rename config/{handler_projection.go => projection.go} (100%) rename config/{handler_projection_test.go => projection_test.go} (100%) diff --git a/config/handler_aggregate.go b/config/aggregate.go similarity index 100% rename from config/handler_aggregate.go rename to config/aggregate.go diff --git a/config/handler_aggregate_test.go b/config/aggregate_test.go similarity index 100% rename from config/handler_aggregate_test.go rename to config/aggregate_test.go diff --git a/config/handlertype.go b/config/handlertype.go index 5c000fd..c903445 100644 --- a/config/handlertype.go +++ b/config/handlertype.go @@ -61,3 +61,120 @@ func (t HandlerType) RouteCapabilities() RouteCapabilities { ), } } + +// SwitchByHandlerType invokes one of the provided functions based on t. +// +// It provides a compile-time guarantee that all possible values are handled, +// even if new [HandlerType] values are added in the future. +// +// It panics if the function associated with t is nil, or if t is not a valid +// [HandlerType]. +func SwitchByHandlerType( + t HandlerType, + aggregate func(), + process func(), + integration func(), + projection func(), +) { + enum.Switch(t, aggregate, process, integration, projection) +} + +// MapByHandlerType maps t to a value of type T. +// +// It provides a compile-time guarantee that all possible values are handled, +// even if new [HandlerType] values are added in the future. +// +// It panics if t is not a valid [HandlerType]. +func MapByHandlerType[T any](t HandlerType, aggregate, process, integration, projection T) T { + return enum.Map(t, aggregate, process, integration, projection) +} + +// SwitchByHandlerTypeOf invokes one of the provided functions based on the +// [HandlerType] of h. +// +// It provides a compile-time guarantee that all types are handled, even if new +// [HandlerType] values are added in the future. +// +// It panics if the function associated with h's type is nil. +func SwitchByHandlerTypeOf( + h Handler, + aggregate func(*Aggregate), + process func(*Process), + integration func(*Integration), + projection func(*Projection), +) { + switch h := h.(type) { + case *Aggregate: + if aggregate == nil { + panic("no case function was provided for *config.Aggregate") + } + aggregate(h) + case *Process: + if process == nil { + panic("no case function was provided for *config.Process") + } + process(h) + case *Integration: + if integration == nil { + panic("no case function was provided for *config.Integration") + } + integration(h) + case *Projection: + if projection == nil { + panic("no case function was provided for *config.Projection") + } + projection(h) + default: + panic("invalid handler type") + } +} + +// MapByHandlerTypeOf invokes one of the provided functions based on the +// [HandlerType] of h, and returns the result. +// +// It provides a compile-time guarantee that all types are handled, even if new +// [HandlerType] values are added in the future. +// +// It panics if the function associated with h's type is nil. +func MapByHandlerTypeOf[T any]( + h Handler, + aggregate func(*Aggregate) T, + process func(*Process) T, + integration func(*Integration) T, + projection func(*Projection) T, +) (result T) { + SwitchByHandlerTypeOf( + h, + enum.AssignResult(aggregate, &result), + enum.AssignResult(process, &result), + enum.AssignResult(integration, &result), + enum.AssignResult(projection, &result), + ) + + return result +} + +// MapByHandlerTypeOfWithErr invokes one of the provided functions based on the +// [HandlerType] of h, and returns the result and error value. +// +// It provides a compile-time guarantee that all types are handled, even if new +// [HandlerType] values are added in the future. +// +// It panics if the function associated with h's type is nil. +func MapByHandlerTypeOfWithErr[T any]( + h Handler, + aggregate func(*Aggregate) (T, error), + process func(*Process) (T, error), + integration func(*Integration) (T, error), + projection func(*Projection) (T, error), +) (result T, err error) { + SwitchByHandlerTypeOf( + h, + enum.AssignResultErr(aggregate, &result, &err), + enum.AssignResultErr(process, &result, &err), + enum.AssignResultErr(integration, &result, &err), + enum.AssignResultErr(projection, &result, &err), + ) + + return result, err +} diff --git a/config/handlertypeswitch_test.go b/config/handlertype_test.go similarity index 100% rename from config/handlertypeswitch_test.go rename to config/handlertype_test.go diff --git a/config/handlertypeswitch.go b/config/handlertypeswitch.go deleted file mode 100644 index 7638256..0000000 --- a/config/handlertypeswitch.go +++ /dev/null @@ -1,122 +0,0 @@ -package config - -import ( - "github.com/dogmatiq/enginekit/internal/enum" -) - -// SwitchByHandlerType invokes one of the provided functions based on t. -// -// It provides a compile-time guarantee that all possible values are handled, -// even if new [HandlerType] values are added in the future. -// -// It panics if the function associated with t is nil, or if t is not a valid -// [HandlerType]. -func SwitchByHandlerType( - t HandlerType, - aggregate func(), - process func(), - integration func(), - projection func(), -) { - enum.Switch(t, aggregate, process, integration, projection) -} - -// MapByHandlerType maps t to a value of type T. -// -// It provides a compile-time guarantee that all possible values are handled, -// even if new [HandlerType] values are added in the future. -// -// It panics if t is not a valid [HandlerType]. -func MapByHandlerType[T any](t HandlerType, aggregate, process, integration, projection T) T { - return enum.Map(t, aggregate, process, integration, projection) -} - -// SwitchByHandlerTypeOf invokes one of the provided functions based on the -// [HandlerType] of h. -// -// It provides a compile-time guarantee that all types are handled, even if new -// [HandlerType] values are added in the future. -// -// It panics if the function associated with h's type is nil. -func SwitchByHandlerTypeOf( - h Handler, - aggregate func(*Aggregate), - process func(*Process), - integration func(*Integration), - projection func(*Projection), -) { - switch h := h.(type) { - case *Aggregate: - if aggregate == nil { - panic("no case function was provided for *config.Aggregate") - } - aggregate(h) - case *Process: - if process == nil { - panic("no case function was provided for *config.Process") - } - process(h) - case *Integration: - if integration == nil { - panic("no case function was provided for *config.Integration") - } - integration(h) - case *Projection: - if projection == nil { - panic("no case function was provided for *config.Projection") - } - projection(h) - default: - panic("invalid handler type") - } -} - -// MapByHandlerTypeOf invokes one of the provided functions based on the -// [HandlerType] of h, and returns the result. -// -// It provides a compile-time guarantee that all types are handled, even if new -// [HandlerType] values are added in the future. -// -// It panics if the function associated with h's type is nil. -func MapByHandlerTypeOf[T any]( - h Handler, - aggregate func(*Aggregate) T, - process func(*Process) T, - integration func(*Integration) T, - projection func(*Projection) T, -) (result T) { - SwitchByHandlerTypeOf( - h, - enum.AssignResult(aggregate, &result), - enum.AssignResult(process, &result), - enum.AssignResult(integration, &result), - enum.AssignResult(projection, &result), - ) - - return result -} - -// MapByHandlerTypeOfWithErr invokes one of the provided functions based on the -// [HandlerType] of h, and returns the result and error value. -// -// It provides a compile-time guarantee that all types are handled, even if new -// [HandlerType] values are added in the future. -// -// It panics if the function associated with h's type is nil. -func MapByHandlerTypeOfWithErr[T any]( - h Handler, - aggregate func(*Aggregate) (T, error), - process func(*Process) (T, error), - integration func(*Integration) (T, error), - projection func(*Projection) (T, error), -) (result T, err error) { - SwitchByHandlerTypeOf( - h, - enum.AssignResultErr(aggregate, &result, &err), - enum.AssignResultErr(process, &result, &err), - enum.AssignResultErr(integration, &result, &err), - enum.AssignResultErr(projection, &result, &err), - ) - - return result, err -} diff --git a/config/handler_integration.go b/config/integration.go similarity index 100% rename from config/handler_integration.go rename to config/integration.go diff --git a/config/handler_integration_test.go b/config/integration_test.go similarity index 100% rename from config/handler_integration_test.go rename to config/integration_test.go diff --git a/config/handler_process.go b/config/process.go similarity index 100% rename from config/handler_process.go rename to config/process.go diff --git a/config/handler_process_test.go b/config/process_test.go similarity index 100% rename from config/handler_process_test.go rename to config/process_test.go diff --git a/config/handler_projection.go b/config/projection.go similarity index 100% rename from config/handler_projection.go rename to config/projection.go diff --git a/config/handler_projection_test.go b/config/projection_test.go similarity index 100% rename from config/handler_projection_test.go rename to config/projection_test.go