-
Notifications
You must be signed in to change notification settings - Fork 2
/
pipeline_test.go
138 lines (130 loc) · 4.25 KB
/
pipeline_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package pipeline_test
import (
"context"
"fmt"
"github.com/saantiaguilera/go-pipeline"
)
// Example basic showcases a simple graph that uses the basic API steps to produce a simple result
// based on a given input.
//
// The input will be mutated across different steps (incrementing or doubling it)
// and finally, print if it's a 3 digit number or not
//
// For showing purposes, all steps and pipeline building are in the same function and use
// basic parameter types and logics (we don't showcase a real life usecase with
// infrastructure / http calls / etc), just note that it's quite similar.
//
// In the examples directory you can find more elaborate samples on how to do this better.
func Example_basic() {
inc := pipeline.NewUnitStep( // int -> int
"increase_number",
func(ctx context.Context, i int) (int, error) {
return i + 20, nil
},
)
double := pipeline.NewUnitStep( // int -> int
"double_number",
func(ctx context.Context, i int) (int, error) {
return i * 2, nil
},
)
toString := pipeline.NewUnitStep( // int -> string
"to_string",
func(ctx context.Context, i int) (string, error) {
return fmt.Sprintf("%d", i), nil
},
)
threeDigit := pipeline.NewUnitStep( // string -> bool
"number_is_three_digit",
func(ctx context.Context, s string) (bool, error) {
return len(s) == 3, nil
},
)
print := pipeline.NewUnitStep( // bool -> bool
"print",
func(ctx context.Context, b bool) (bool, error) {
fmt.Println(b)
return b, nil
},
)
// built from end to start
printThreeDigit := pipeline.NewSequentialStep[string, bool, bool](threeDigit, print)
stringAndEnd := pipeline.NewSequentialStep[int, string, bool](toString, printThreeDigit)
doubleAndEnd := pipeline.NewSequentialStep[int, int, bool](double, stringAndEnd)
graph := pipeline.NewSequentialStep[int, int, bool](inc, doubleAndEnd)
graph.Run(context.Background(), 30)
graph.Run(context.Background(), 20)
// output:
// true
// false
}
// Example complex showcases a complex graph that uses most of the API steps to produce a simple result
// based on a given input.
//
// The input will be mutated across different steps (incrementing or doubling it)
// and finally, print if it's a 3 digit number or not
//
// For showing purposes, all steps and pipeline building are in the same function and use
// basic parameter types and logics (we don't showcase a real life usecase with
// infrastructure / http calls / etc), just note that it's quite similar.
//
// In the examples directory you can find more elaborate samples on how to do this better.
func Example_complex() {
inc := pipeline.NewUnitStep( // int -> int
"increase_number",
func(ctx context.Context, i int) (int, error) {
return i + 1, nil
},
)
double := pipeline.NewUnitStep( // int -> int
"double_number",
func(ctx context.Context, i int) (int, error) {
return i * 2, nil
},
)
toString := pipeline.NewUnitStep( // int -> string
"to_string",
func(ctx context.Context, i int) (string, error) {
return fmt.Sprintf("%d", i), nil
},
)
threeDigit := pipeline.NewUnitStep( // string -> bool
"number_is_three_digit",
func(ctx context.Context, s string) (bool, error) {
return len(s) == 3, nil
},
)
cond := pipeline.NewOptionalStep[int](
pipeline.NewStatement(
"multiply_if_even",
func(ctx context.Context, i int) bool {
return i%2 == 0
},
),
double,
)
concurrentInc := pipeline.NewConcurrentStep( // int -> int
[]pipeline.Step[int, int]{inc, inc, inc, inc, inc, inc, inc, inc, inc, inc},
func(ctx context.Context, i1, i2 int) (int, error) {
return i1 + i2, nil
},
)
print := pipeline.NewUnitStep( // bool -> bool
"print",
func(ctx context.Context, b bool) (bool, error) {
fmt.Println(b)
return b, nil
},
)
// built from end to start
threeDigitAndPrint := pipeline.NewSequentialStep[string, bool, bool](threeDigit, print)
toSringAndEnd := pipeline.NewSequentialStep[int, string, bool](toString, threeDigitAndPrint)
doubleAndEnd := pipeline.NewSequentialStep[int, int, bool](double, toSringAndEnd)
conditionAndEnd := pipeline.NewSequentialStep[int, int, bool](cond, doubleAndEnd)
graph := pipeline.NewSequentialStep[int, int, bool](concurrentInc, conditionAndEnd)
graph.Run(context.Background(), 2)
graph.Run(context.Background(), 1)
// output:
// true
// false
}