-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Step Type and Execution Flow [#5]
Merge pull request #5 from TanmoySG/dynamic-steps
- Loading branch information
Showing
7 changed files
with
210 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
gosteps "github.com/TanmoySG/go-steps" | ||
"github.com/TanmoySG/go-steps/example/funcs" | ||
) | ||
|
||
func main() { | ||
|
||
intsToAdd := []int{1, 4, 7, 10} | ||
|
||
var step *gosteps.Step | ||
for _, val := range intsToAdd { | ||
step = addStepToChain(step, funcs.Add, []interface{}{val}) | ||
} | ||
|
||
finalOutput, err := step.Execute(1) | ||
if err != nil { | ||
fmt.Printf("error executing steps: %s, final output: [%s]\n", err, finalOutput) | ||
} | ||
|
||
fmt.Printf("Final Output: [%v]\n", finalOutput) | ||
} | ||
|
||
// step to add new next step to step-chain; basically a linked-list insertion | ||
func addStepToChain(step *gosteps.Step, stepFunc interface{}, additionalArgs []interface{}) *gosteps.Step { | ||
temp := gosteps.Step{ | ||
Function: stepFunc, | ||
AdditionalArgs: additionalArgs, | ||
} | ||
|
||
if step == nil { | ||
step = &temp | ||
return step | ||
} | ||
|
||
curr := step | ||
for curr.NextStep != nil { | ||
curr = curr.NextStep | ||
} | ||
|
||
curr.NextStep = &temp | ||
return step | ||
} |
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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, | ||
AdditionalArgs: []interface{}{2}, | ||
NextStep: &gosteps.Step{ | ||
Function: funcs.Sub, | ||
AdditionalArgs: []interface{}{4}, | ||
NextStepResolver: nextStepResolver, | ||
PossibleNextSteps: gosteps.PossibleNextSteps{ | ||
{ | ||
Name: stepMultiply, | ||
Function: funcs.Multiply, | ||
AdditionalArgs: []interface{}{-5}, | ||
NextStep: &gosteps.Step{ | ||
Function: funcs.Add, | ||
AdditionalArgs: []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, | ||
AdditionalArgs: []interface{}{-2}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func main() { | ||
initArgs := []interface{}{1} | ||
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 | ||
} |
Oops, something went wrong.