Skip to content

Commit 9fa005b

Browse files
committed
Update docs
1 parent 84d1c22 commit 9fa005b

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,39 @@ Small Go utility that executes business actions in a pipeline.
1111

1212
```go
1313
import (
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+
1823
func 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

7175
It 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()),

examples/hooks_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ func TestExample_Hooks(t *testing.T) {
1717
fmt.Println(fmt.Sprintf("Executing step: %s", step.Name))
1818
})
1919
p.WithSteps(
20-
pipeline.NewStep("hook demo", AfterHookAction()),
20+
pipeline.NewStepFromFunc("hook demo", AfterHookAction),
2121
)
2222
result := p.Run()
2323
if !result.IsSuccessful() {
2424
t.Fatal(result.Err)
2525
}
2626
}
2727

28-
func AfterHookAction() pipeline.ActionFunc {
29-
return func(ctx context.Context) pipeline.Result {
30-
fmt.Println("I am called in an action after the hooks")
31-
return pipeline.Result{}
32-
}
28+
func AfterHookAction(_ context.Context) error {
29+
fmt.Println("I am called in an action after the hooks")
30+
return nil
3331
}

0 commit comments

Comments
 (0)