Repository navigation
feat(v2): typed Step[I, O] built on Go 1.27 generic methods - #5
Merged
Merged
Conversation
v1 modelled a step as `Step[T]`: one type carried the whole pipeline.
That single decision caused three defects, each reproduced before this
change and each now covered by a regression test:
- Pipeline.Run and series.Run rewrote their own step slice while they
ran, so every run added another middleware layer (1, 2, then 3
invocations of the same middleware).
- Parallel + Merge dropped a branch. mergo refuses to overwrite a
non-empty destination field, so two branches that each added 1
produced 1, not 2.
- A panic in a parallel branch was logged, the goroutine returned nil,
and mergo then panicked on the nil pointer, crashing the caller.
v2 gives a step its own input type and its own output type. The compiler
now does the work that reflection did.
Step is a concrete struct, not an interface, because Go 1.27 allows type
parameters on a method only when the receiver is a concrete type ("interface
method must have no type parameters"). Generic methods are what let a chain
change type: Then[P], Par[B, R] and Map[P]. Runner stays an interface for
types that carry state.
Each cannot be a method: Step[I,O].Each returning Step[[]I,[]O] makes the
compiler instantiate Step[[][]I,[][]O] without end, which it rejects as an
instantiation cycle. It is a package-level function.
A step is now a value. Every method returns a new step, so middleware is
applied once at build time and a step is safe to share and to reuse.
Also:
- Par, Fan and Each join every branch error with errors.Join and turn a
panic into a *PanicError.
- CircuitBreaker is an explicit value, so sharing is deliberate. v1 kept
it in a closure, silently shared by every step the middleware wrapped.
- Timeout starts no goroutine; v1 raced one and leaked it.
- Log records the name, the duration and the error, never the payload.
- No dependency outside the standard library. v1 needed mergo,
google/uuid and x/sync.
CI runs a matrix over both modules, so v2 is tested as well.
98.2% coverage, clean under -race, go vet, modernize, go fix and
golangci-lint.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
v1 models a step as
Step[T]: it reads aTand returns aT, so one type has to carry the whole pipeline. That single decision causes three defects. I reproduced each one against v1 before writing this, and each now has a regression test in v2.1. Middleware collects a new layer on every run.
Pipeline.Runandseries.Runwrite to their own step slice while they run (workflow.go:46,workflow.go:281):A
Retry(3)becomes 9 attempts on the second run. Two concurrent runs also race.2.
Parallel+Mergedrops a branch. Two branches each didCounter++and appended a message:mergo does not overwrite a non-empty destination field. The second branch is lost in silence. This is the exact pattern in the current README.
3. A panic in a parallel branch crashes the caller.
CapturePaniclogs it and lets the goroutine returnnil. errgroup sees success,resps[i]staysnil, and mergo then panics on the nil pointer:One panic becomes two, and the second is not recovered.
These are not bugs in the implementation. They follow from the shape of
Step[T], which forces a merge function, forces a deep copy, and gives no typed place to put a branch result.What changed
A step now declares its own input type and its own output type. The compiler does the work that reflection did.
Two constraints the compiler imposed
Stepis a concrete struct, not an interface. Go 1.27 allows type parameters on a method only when the receiver is a concrete type:Generic methods are the whole point — they let a chain change type.
Runner[I, O]stays a plain interface, for a type that carries state; wrap it withOf.Eachis a package-level function, not a method. A method returningStep[[]I,[]O]makes the compiler instantiateStep[[][]I,[][]O], and so on without end:The rule: a new type parameter on a method is fine, a type expression built from the receiver's own parameters is not.
How each defect becomes impossible
TestMiddlewareIsAppliedOnceTestParKeepsEveryResultPar,FanandEachturn a branch panic into a*PanicError.TestParTurnsPanicIntoErrorAlso fixed
CircuitBreakeris an explicit value. v1 kept its state in the middleware closure, so every step the middleware wrapped shared one circuit without saying so. Covered byTestBreakersAreIndependentUnlessShared.Timeoutstarts no goroutine. v1 raced one and leaked it. The step must honour the context, which is the only thing Go can actually guarantee.Lognever records the payload. v1 wrotefmt.Sprintf("%v", resp)of the whole struct, which can hold a secret and costs more than a short step.Par,FanandEachrun every branch to the end and return every error witherrors.Join. v1's errgroup returned the first one only.Removed
mergo,google/uuid,x/sync,reflect,MergeRequest,Merge,MergeTransform,SafeCopy,DeepCopyInterface,StepValidator,SafeRun, and the four near-identical tree printers inPipeline,series,parallelandselector.v2 has no dependency outside the standard library.
Layout
v2 is a separate module in the
v2/subdirectory, the standard Go major-version layout. v1 keeps working and keeps its import path.v2/README.mdholds a full migration table.ci.ymlnow runs a matrix over both modules. Before this, CI ran at the repo root only and would not have tested v2 at all.Verification
Benchmarks, Apple M4 Max, Go 1.27. The sequential path allocates nothing:
Deliberate trade-offs, open to discussion
Par,FanandEachdo not fail fast. Every branch runs to the end. This avoids confusingcontext cancelednoise from a sibling, at the cost of work on a doomed run.Timeoutcannot abandon a step that ignores its context. Go cannot stop a goroutine from outside. v1 appeared to, but only by leaking the goroutine.v2/README.mdsays so plainly under "What v2 is not".Note on v1
This PR does not patch v1. The three defects above still affect anyone on the current release. Happy to follow up with a v1 patch branch.
🤖 Generated with Claude Code