Skip to content

Commit

Permalink
Merge pull request #11 from ccremer/alternative-context
Browse files Browse the repository at this point in the history
Revert Context: Use arbitrary object instead
  • Loading branch information
ccremer authored Aug 29, 2021
2 parents ffcf398 + c390cee commit 269b6be
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 365 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
)

func main() {
number := 0 // define arbitrary data to pass around in the steps.
p := pipeline.NewPipeline()
p.WithContext(&number)
p.WithSteps(
pipeline.NewStep("define random number", defineNumber),
pipeline.NewStepFromFunc("print number", printNumber),
Expand All @@ -28,14 +30,15 @@ func main() {
}

func defineNumber(ctx pipeline.Context) pipeline.Result {
ctx.SetValue("number", rand.Int())
ctx.(*int) = 10
return pipeline.Result{}
}

// Let's assume this is a business function that can fail.
// You can enable "automatic" fail-on-first-error pipelines by having more small functions that return errors.
func printNumber(ctx pipeline.Context) error {
_, err := fmt.Println(ctx.IntValue("number", 0))
number := ctx.(*int)
_, err := fmt.Println(*number)
return err
}
```
Expand Down Expand Up @@ -66,16 +69,17 @@ We have tons of `if err != nil` that bloats the function with more error handlin
It could be simplified to something like this:
```go
func Persist(data Data) error {
p := pipeline.NewPipeline().WithSteps(
p := pipeline.NewPipeline().WithContext(data).WithSteps(
pipeline.NewStep("prepareTransaction", prepareTransaction()),
pipeline.NewStep("executeQuery", executeQuery(data)),
pipeline.NewStep("executeQuery", executeQuery()),
pipeline.NewStep("commitTransaction", commit()),
)
return p.Run().Err
}

func executeQuery(data Data) pipeline.ActionFunc {
return func(_ pipeline.Context) pipeline.Result {
func executeQuery() pipeline.ActionFunc {
return func(ctx pipeline.Context) pipeline.Result {
data := ctx.(Data)
err := database.executeQuery("SOME QUERY", data)
return pipeline.Result{Err: err}
}
Expand Down
100 changes: 0 additions & 100 deletions context.go

This file was deleted.

164 changes: 0 additions & 164 deletions context_test.go

This file was deleted.

9 changes: 7 additions & 2 deletions examples/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import (
pipeline "github.com/ccremer/go-command-pipeline"
)

type Data struct {
Number int
}

func TestExample_Context(t *testing.T) {
// Create pipeline with defaults
p := pipeline.NewPipeline()
p.WithContext(&Data{})
p.WithSteps(
pipeline.NewStep("define random number", defineNumber),
pipeline.NewStepFromFunc("print number", printNumber),
Expand All @@ -24,11 +29,11 @@ func TestExample_Context(t *testing.T) {
}

func defineNumber(ctx pipeline.Context) pipeline.Result {
ctx.SetValue("number", rand.Int())
ctx.(*Data).Number = rand.Int()
return pipeline.Result{}
}

func printNumber(ctx pipeline.Context) error {
_, err := fmt.Println(ctx.IntValue("number", 0))
_, err := fmt.Println(ctx.(*Data).Number)
return err
}
Loading

0 comments on commit 269b6be

Please sign in to comment.