-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (74 loc) · 1.93 KB
/
main.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
package main
import (
"fmt"
"time"
gosteps "github.com/TanmoySG/go-steps"
"github.com/TanmoySG/go-steps/example/funcs"
)
const (
stepMultiply = "Multiply"
stepDivide = "Divide"
)
// reading/maintaining this is a bit tricky will add
// a functional way to create this in the next version
var steps = gosteps.Step{
Function: funcs.Add,
StepArgs: []interface{}{2},
NextStep: &gosteps.Step{
Function: funcs.Sub,
StepArgs: []interface{}{4},
NextStepResolver: nextStepResolver,
PossibleNextSteps: gosteps.PossibleNextSteps{
{
Name: stepMultiply,
Function: funcs.Multiply,
StepArgs: []interface{}{-5},
NextStep: &gosteps.Step{
Function: funcs.Add,
StepArgs: []interface{}{100},
NextStep: &gosteps.Step{
Function: funcs.StepWillError3Times,
ErrorsToRetry: []error{
fmt.Errorf("error"),
},
NextStep: &gosteps.Step{
Function: funcs.StepWillErrorInfinitely,
ErrorsToRetry: []error{
fmt.Errorf("error"),
},
NextStep: &gosteps.Step{
Function: funcs.Multiply,
},
StrictErrorCheck: false,
MaxAttempts: 5, // use gosteps.MaxMaxAttempts for Maximum Possible reattempts
},
MaxAttempts: 5,
RetrySleep: 1 * time.Second,
},
},
},
{
Name: stepDivide,
Function: funcs.Divide,
StepArgs: []interface{}{-2},
},
},
},
}
func main() {
initArgs := []interface{}{5}
finalOutput, err := steps.Execute(initArgs...)
if err != nil {
fmt.Printf("error executing steps: %s, final output: [%s]\n", err, finalOutput)
}
fmt.Printf("Final Output: [%v]\n", finalOutput)
}
// step resolver
func nextStepResolver(args ...any) string {
if args[0].(int) < 0 {
fmt.Printf("StepResolver [%v]: Arguments is Negative, going with Multiply\n", args)
return stepMultiply
}
fmt.Printf("StepResolver [%v]: Arguments is Positive, going with Divide\n", args)
return stepDivide
}