Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR completely rewrites the library to make use of generics.
One of problems of this pipeline of this sort is how to access or "pass" data through the pipeline, possibly with intermediate data that get added throughout the steps while executing them.
One approach is to have them in the
context.Context
that is passed through every function. However, native contexts are not only read-only, they are also cumbersome to use since they're so generic by using a key-value store. Users of the context approach would need to create structs or other identifiers as keys, and usepipeline.MutableContext
to get a context that allows creating new entries or updating them.Generics (as introduced in Go 1.18) can eliminate this problem. The context passed through the steps is now extensible. If every step is an instance of
T
, then users can come up with their own implementation ofcontext.Context
and this allows a type-safe access to read and write intermediate values in the steps.The new usage looks roughly as following:
A number of other breaking changes are as following:
Pipeline.Run()
is removed, usePipeline.RunWithContext()
insteadNewStepFromFunc()
is renamed toNewStep()
, the old signature ofNewStep
is gone.Step.F
renamed toStep.Action
.Step.WithResultHandler()
is removed, useStep.WithErrorHandler()
insteadStep.Handler
is now always called if set, independent of the step's error return value. Users should now check for non-nil errors in their error handlers.Result
is now a custom type that implements theerror
interface. Users can now check if this is non-nil and useerrors.As
to get additional information about the step that threw the error.Pipeline.AddBeforeHook()
removed, usePipeline.WithBeforeHooks()
insteadNewAnonymous()
is removed. I don't think there's really a use case for it.Pipeline.WithOptions()
now accepts a structpipeline.Options
with simple fields instead of function pointers, to avoid "generify" these functions as well(there may be more I lost track of)
Checklist
fix
,enhancement
,documentation
,change
,breaking
,dependency
as they show up in the changelog