-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_step_types.go
88 lines (73 loc) · 2.49 KB
/
go_step_types.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
package gosteps
import (
"encoding/json"
"regexp"
"time"
)
// StepName type defined the name of the step
type StepName string
// BranchName type defined the name of the branch
type BranchName string
// StepFn defines the Step's Function
type StepFn func(ctx GoStepsCtx) StepResult
// ResolverFn defines the Resolver Function
// to determine the branch to execute
type ResolverFn func(ctx GoStepsCtx) BranchName
// Step type defines a step with all configurations for the step
type Step struct {
Name StepName `json:"name"`
Function StepFn `json:"-"`
StepOpts StepOpts `json:"stepConfig"`
Branches *Branches `json:"branches"`
StepArgs map[string]interface{} `json:"stepArgs"`
stepResult *StepResult `json:"-"`
stepRunProgress StepRunProgress `json:"-"`
}
// stepRunProgress type defines the progress of the step
// it contains the run/execution count of each step
type StepRunProgress struct {
runCount int `json:"-"`
}
// Branch type defines a unique step-chain, of the step-tree
// Branches can be used to define different steps to be executed
// based on a resolver function
type Branch struct {
BranchName BranchName `json:"branchName"`
Steps Steps `json:"steps"`
}
// Steps type defines a list of steps
type Steps []Step
// Branches type defines a list of branches
// with a resolver function to determine the branch to execute
type Branches struct {
Branches []Branch `json:"branches"`
Resolver ResolverFn `json:"-"`
}
// StepOpts type defines the configuration for the step
type StepOpts struct {
ErrorsToRetry []error `json:"errorsToRetry"`
ErrorPatternsToRetry []regexp.Regexp `json:"errorPatternsToRetry"`
RetryAllErrors bool `json:"retryAllErrors"`
MaxRunAttempts int `json:"maxAttempts"`
RetrySleep time.Duration `json:"retrySleep"`
}
// ToJson converts the step-tree to JSON-string
func (branch *Branch) ToJson() (string, error) {
stepsBytes, err := json.Marshal(branch)
if err != nil {
return "", err
}
return string(stepsBytes), nil
}
// NewStepChain creates a new root branch of the step-chain
// Soon to be deprecated in favor of NewStepsProcessor
func NewStepChain(steps Steps) *Branch {
return NewStepsProcessor(steps)
}
// NewStepsProcessor creates a new root branch of the step-chain
func NewStepsProcessor(steps Steps) *Branch {
return &Branch{
BranchName: "root",
Steps: steps,
}
}