Skip to content

Commit f068907

Browse files
authored
Merge pull request #25 from ccremer/hooks
Change pipeline hook type to plain func instead of interface
2 parents 9c041d1 + c3c1f1a commit f068907

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

examples/hooks_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ import (
1010
pipeline "github.com/ccremer/go-command-pipeline"
1111
)
1212

13-
type PipelineLogger struct{}
14-
15-
func (l *PipelineLogger) Accept(step pipeline.Step) {
16-
fmt.Println(fmt.Sprintf("Executing step: %s", step.Name))
17-
}
18-
1913
func TestExample_Hooks(t *testing.T) {
2014
p := pipeline.NewPipeline()
21-
p.AddBeforeHook(&PipelineLogger{})
15+
p.AddBeforeHook(func(step pipeline.Step) {
16+
fmt.Println(fmt.Sprintf("Executing step: %s", step.Name))
17+
})
2218
p.WithSteps(
2319
pipeline.NewStep("hook demo", AfterHookAction()),
2420
)

pipeline.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ type (
3838
}
3939
// Context contains arbitrary data relevant for the pipeline execution.
4040
Context interface{}
41-
// Listener is a simple interface that listens to Pipeline events.
42-
Listener interface {
43-
// Accept takes the given Step.
44-
Accept(step Step)
45-
}
41+
// Listener is a simple func that listens to Pipeline events.
42+
Listener func(step Step)
4643
// ActionFunc is the func that contains your business logic.
4744
// The context is a user-defined arbitrary data of type interface{} that gets provided in every Step, but may be nil if not set.
4845
ActionFunc func(ctx Context) Result
@@ -132,8 +129,8 @@ func (p *Pipeline) Run() Result {
132129

133130
func (p *Pipeline) doRun() Result {
134131
for _, step := range p.steps {
135-
for _, listener := range p.beforeHooks {
136-
listener.Accept(step)
132+
for _, hooks := range p.beforeHooks {
133+
hooks(step)
137134
}
138135

139136
result := step.F(p.context)

pipeline_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestPipeline_Run(t *testing.T) {
4242
return Result{}
4343
}),
4444
},
45-
givenBeforeHook: hook,
45+
givenBeforeHook: hook.Accept,
4646
expectedCalls: 2,
4747
},
4848
"GivenPipelineWithFinalizer_WhenRunning_ThenCallHandler": {

0 commit comments

Comments
 (0)