github.com/mweagle/Sparta@v1.15.0/aws/step/state_parallel.go (about) 1 package step 2 3 import ( 4 "math/rand" 5 ) 6 7 //////////////////////////////////////////////////////////////////////////////// 8 // ParallelState 9 //////////////////////////////////////////////////////////////////////////////// 10 11 // ParallelState is a synthetic state that executes a lot of independent 12 // branches in parallel 13 type ParallelState struct { 14 baseInnerState 15 Branches []*StateMachine 16 Parameters map[string]interface{} 17 ResultPath string 18 Retriers []*TaskRetry 19 Catchers []*TaskCatch 20 } 21 22 // WithResultPath is the fluent builder for the result path 23 func (ps *ParallelState) WithResultPath(resultPath string) *ParallelState { 24 ps.ResultPath = resultPath 25 return ps 26 } 27 28 // WithRetriers is the fluent builder for TaskState 29 func (ps *ParallelState) WithRetriers(retries ...*TaskRetry) *ParallelState { 30 if ps.Retriers == nil { 31 ps.Retriers = make([]*TaskRetry, 0) 32 } 33 ps.Retriers = append(ps.Retriers, retries...) 34 return ps 35 } 36 37 // WithCatchers is the fluent builder for TaskState 38 func (ps *ParallelState) WithCatchers(catch ...*TaskCatch) *ParallelState { 39 if ps.Catchers == nil { 40 ps.Catchers = make([]*TaskCatch, 0) 41 } 42 ps.Catchers = append(ps.Catchers, catch...) 43 return ps 44 } 45 46 // Next returns the next state 47 func (ps *ParallelState) Next(nextState MachineState) MachineState { 48 ps.next = nextState 49 return nextState 50 } 51 52 // AdjacentStates returns nodes reachable from this node 53 func (ps *ParallelState) AdjacentStates() []MachineState { 54 if ps.next == nil { 55 return nil 56 } 57 return []MachineState{ps.next} 58 } 59 60 // Name returns the name of this Task state 61 func (ps *ParallelState) Name() string { 62 return ps.name 63 } 64 65 // WithComment returns the TaskState comment 66 func (ps *ParallelState) WithComment(comment string) TransitionState { 67 ps.comment = comment 68 return ps 69 } 70 71 // WithInputPath returns the TaskState input data selector 72 func (ps *ParallelState) WithInputPath(inputPath string) TransitionState { 73 ps.inputPath = inputPath 74 return ps 75 } 76 77 // WithOutputPath returns the TaskState output data selector 78 func (ps *ParallelState) WithOutputPath(outputPath string) TransitionState { 79 ps.outputPath = outputPath 80 return ps 81 } 82 83 // MarshalJSON for custom marshalling 84 func (ps *ParallelState) MarshalJSON() ([]byte, error) { 85 /* 86 A state in a Parallel state branch “States” field MUST NOT have a “Next” field that targets a field outside of that “States” field. A state MUST NOT have a “Next” field which matches a state name inside a Parallel state branch’s “States” field unless it is also inside the same “States” field. 87 88 Put another way, states in a branch’s “States” field can transition only to each other, and no state outside of that “States” field can transition into it. 89 */ 90 additionalParams := map[string]interface{}{ 91 "Branches": ps.Branches, 92 } 93 if ps.ResultPath != "" { 94 additionalParams["ResultPath"] = ps.ResultPath 95 } 96 if len(ps.Retriers) != 0 { 97 additionalParams["Retry"] = ps.Retriers 98 } 99 if ps.Catchers != nil { 100 additionalParams["Catch"] = ps.Catchers 101 } 102 if ps.Parameters != nil { 103 additionalParams["Parameters"] = ps.Parameters 104 } 105 return ps.marshalStateJSON("Parallel", additionalParams) 106 } 107 108 // NewParallelState returns a "ParallelState" with the supplied 109 // information 110 func NewParallelState(parallelStateName string, branches ...*StateMachine) *ParallelState { 111 return &ParallelState{ 112 baseInnerState: baseInnerState{ 113 name: parallelStateName, 114 id: rand.Int63(), 115 }, 116 Branches: branches, 117 } 118 }