From c3c1f1a2a616a5748881967096fa6ae70c6cb161 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 27 Dec 2021 12:15:11 +0100 Subject: [PATCH] Change hook type to plain func instead of interface This makes anonymous hooks easier to implement --- examples/hooks_test.go | 10 +++------- pipeline.go | 11 ++++------- pipeline_test.go | 2 +- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/examples/hooks_test.go b/examples/hooks_test.go index fca3ff9..ce1c7da 100644 --- a/examples/hooks_test.go +++ b/examples/hooks_test.go @@ -10,15 +10,11 @@ import ( pipeline "github.com/ccremer/go-command-pipeline" ) -type PipelineLogger struct{} - -func (l *PipelineLogger) Accept(step pipeline.Step) { - fmt.Println(fmt.Sprintf("Executing step: %s", step.Name)) -} - func TestExample_Hooks(t *testing.T) { p := pipeline.NewPipeline() - p.AddBeforeHook(&PipelineLogger{}) + p.AddBeforeHook(func(step pipeline.Step) { + fmt.Println(fmt.Sprintf("Executing step: %s", step.Name)) + }) p.WithSteps( pipeline.NewStep("hook demo", AfterHookAction()), ) diff --git a/pipeline.go b/pipeline.go index e3afef9..4a2afce 100644 --- a/pipeline.go +++ b/pipeline.go @@ -38,11 +38,8 @@ type ( } // Context contains arbitrary data relevant for the pipeline execution. Context interface{} - // Listener is a simple interface that listens to Pipeline events. - Listener interface { - // Accept takes the given Step. - Accept(step Step) - } + // Listener is a simple func that listens to Pipeline events. + Listener func(step Step) // ActionFunc is the func that contains your business logic. // The context is a user-defined arbitrary data of type interface{} that gets provided in every Step, but may be nil if not set. ActionFunc func(ctx Context) Result @@ -132,8 +129,8 @@ func (p *Pipeline) Run() Result { func (p *Pipeline) doRun() Result { for _, step := range p.steps { - for _, listener := range p.beforeHooks { - listener.Accept(step) + for _, hooks := range p.beforeHooks { + hooks(step) } result := step.F(p.context) diff --git a/pipeline_test.go b/pipeline_test.go index ca8ed35..dee0930 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -42,7 +42,7 @@ func TestPipeline_Run(t *testing.T) { return Result{} }), }, - givenBeforeHook: hook, + givenBeforeHook: hook.Accept, expectedCalls: 2, }, "GivenPipelineWithFinalizer_WhenRunning_ThenCallHandler": {