Skip to content

Commit 503de6f

Browse files
authored
Merge pull request #28 from ccremer/options
Refactoring: Improve pipeline options with struct
2 parents 328317f + 975f8ac commit 503de6f

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

options.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package pipeline
22

3+
type options struct {
4+
disableErrorWrapping bool
5+
}
6+
37
// Option configures the given Pipeline with a behaviour-altering setting.
48
type Option func(pipeline *Pipeline)
59

@@ -17,5 +21,5 @@ func (p *Pipeline) WithOptions(options ...Option) *Pipeline {
1721
// DisableErrorWrapping disables the wrapping of errors that are emitted from pipeline steps.
1822
// This effectively causes Result.Err to be exactly the error as returned from a step.
1923
var DisableErrorWrapping Option = func(pipeline *Pipeline) {
20-
pipeline.disableErrorWrapping = true
24+
pipeline.options.disableErrorWrapping = true
2125
}

options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestPipeline_WithOptions(t *testing.T) {
1515
return errors.New("some error")
1616
}),
1717
)
18-
assert.True(t, p.disableErrorWrapping)
18+
assert.True(t, p.options.disableErrorWrapping)
1919
result := p.Run()
2020
assert.Equal(t, "some error", result.Err.Error())
2121
})

pipeline.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
type (
99
// Pipeline holds and runs intermediate actions, called "steps".
1010
Pipeline struct {
11-
steps []Step
12-
context Context
13-
beforeHooks []Listener
14-
finalizer ResultHandler
15-
disableErrorWrapping bool
11+
steps []Step
12+
context Context
13+
beforeHooks []Listener
14+
finalizer ResultHandler
15+
options options
1616
}
1717
// Result is the object that is returned after each step and after running a pipeline.
1818
Result struct {
@@ -91,7 +91,7 @@ func (p *Pipeline) WithSteps(steps ...Step) *Pipeline {
9191
// WithNestedSteps is similar to AsNestedStep, but it accepts the steps given directly as parameters.
9292
func (p *Pipeline) WithNestedSteps(name string, steps ...Step) Step {
9393
return NewStep(name, func(_ Context) Result {
94-
nested := &Pipeline{beforeHooks: p.beforeHooks, steps: steps, context: p.context, disableErrorWrapping: p.disableErrorWrapping}
94+
nested := &Pipeline{beforeHooks: p.beforeHooks, steps: steps, context: p.context, options: p.options}
9595
return nested.Run()
9696
})
9797
}
@@ -100,7 +100,7 @@ func (p *Pipeline) WithNestedSteps(name string, steps ...Step) Step {
100100
// The properties are passed to the nested pipeline.
101101
func (p *Pipeline) AsNestedStep(name string) Step {
102102
return NewStep(name, func(_ Context) Result {
103-
nested := &Pipeline{beforeHooks: p.beforeHooks, steps: p.steps, context: p.context}
103+
nested := &Pipeline{beforeHooks: p.beforeHooks, steps: p.steps, context: p.context, options: p.options}
104104
return nested.Run()
105105
})
106106
}
@@ -149,7 +149,7 @@ func (p *Pipeline) doRun() Result {
149149
// Abort pipeline without error
150150
return Result{aborted: true}
151151
}
152-
if p.disableErrorWrapping {
152+
if p.options.disableErrorWrapping {
153153
return Result{Err: err}
154154
}
155155
return Result{Err: fmt.Errorf("step '%s' failed: %w", step.Name, err)}

0 commit comments

Comments
 (0)