@@ -11,35 +11,39 @@ Small Go utility that executes business actions in a pipeline.
1111
1212``` go
1313import (
14+ " context"
1415 pipeline " github.com/ccremer/go-command-pipeline"
1516 " github.com/ccremer/go-command-pipeline/predicate"
1617)
1718
19+ type Data struct {
20+ Number int
21+ }
22+
1823func main () {
19- number := 0 // define arbitrary data to pass around in the steps.
24+ data := &Data // define arbitrary data to pass around in the steps.
2025 p := pipeline.NewPipeline ()
21- p.WithContext (&number)
2226 p.WithSteps (
2327 pipeline.NewStep (" define random number" , defineNumber),
2428 pipeline.NewStepFromFunc (" print number" , printNumber),
2529 )
26- result := p.Run ( )
30+ result := p.RunWithContext (context. WithValue (context. Background , " data " , data) )
2731 if !result.IsSuccessful () {
2832 log.Fatal (result.Err )
2933 }
3034}
3135
32- func defineNumber (ctx pipeline .Context ) pipeline .Result {
33- ctx.(* int ) = 10
36+ func defineNumber (ctx context .Context ) pipeline .Result {
37+ ctx.Value ( " data " ).(*Data). Number = 10
3438 return pipeline.Result {}
3539}
3640
3741// Let's assume this is a business function that can fail.
3842// You can enable "automatic" fail-on-first-error pipelines by having more small functions that return errors.
39- func printNumber (ctx pipeline .Context ) error {
40- number := ctx.(* int )
41- _ , err := fmt.Println (* number)
42- return err
43+ func printNumber (ctx context .Context ) error {
44+ number := ctx.Value ( " data " ).(*Data). Number
45+ fmt.Println (number)
46+ return nil
4347}
4448```
4549
@@ -70,7 +74,7 @@ We have tons of `if err != nil` that bloats the function with more error handlin
7074
7175It could be simplified to something like this:
7276``` go
73- func Persist (data Data ) error {
77+ func Persist (data * Data ) error {
7478 p := pipeline.NewPipeline ().WithSteps (
7579 pipeline.NewStep (" prepareTransaction" , prepareTransaction ()),
7680 pipeline.NewStep (" executeQuery" , executeQuery ()),
0 commit comments