forked from coinbase/step
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_state.go
99 lines (79 loc) · 2.13 KB
/
map_state.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
package machine
import (
"context"
"fmt"
"github.com/coinbase/step/jsonpath"
"github.com/coinbase/step/utils/to"
)
type MapState struct {
stateStr // Include Defaults
Type *string
Comment *string `json:",omitempty"`
Iterator *StateMachine
ItemsPath *jsonpath.Path `json:",omitempty"`
Parameters interface{} `json:",omitempty"`
MaxConcurrency *float64 `json:",omitempty"`
InputPath *jsonpath.Path `json:",omitempty"`
OutputPath *jsonpath.Path `json:",omitempty"`
ResultPath *jsonpath.Path `json:",omitempty"`
Catch []*Catcher `json:",omitempty"`
Retry []*Retrier `json:",omitempty"`
Next *string `json:",omitempty"`
End *bool `json:",omitempty"`
}
func (s *MapState) process(ctx context.Context, input interface{}) (interface{}, *string, error) {
output, err := s.ItemsPath.GetSlice(input)
if err != nil {
return input, nextState(s.Next, s.End), err
}
var res []map[string]interface{}
for _, item := range output {
execution, err := s.Iterator.Execute(item)
if err != nil {
return input, nextState(s.Next, s.End), err
}
res = append(res, execution.Output)
}
return res, nextState(s.Next, s.End), nil
}
func (s *MapState) Execute(ctx context.Context, input interface{}) (output interface{}, next *string, err error) {
return processError(s,
processCatcher(s.Catch,
processRetrier(s.Name(), s.Retry,
inputOutput(
s.InputPath,
s.OutputPath,
withParams(
s.Parameters,
result(s.ResultPath, s.process),
),
),
),
),
)(ctx, input)
}
func (s *MapState) Validate() error {
s.SetType(to.Strp("Map"))
if err := ValidateNameAndType(s); err != nil {
return fmt.Errorf("%v %v", errorPrefix(s), err)
}
if err := endValid(s.Next, s.End); err != nil {
return fmt.Errorf("%v %v", errorPrefix(s), err)
}
if s.Iterator == nil {
return fmt.Errorf("%v Requires Iterator", errorPrefix(s))
}
if err := s.Iterator.Validate(); err != nil {
return fmt.Errorf("%v %v", errorPrefix(s), err)
}
return nil
}
func (s *MapState) SetType(t *string) {
s.Type = t
}
func (s *MapState) GetType() *string {
return s.Type
}
func (s *MapState) GetNext() *string {
return s.Next
}