-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow.go
More file actions
72 lines (62 loc) · 2.39 KB
/
workflow.go
File metadata and controls
72 lines (62 loc) · 2.39 KB
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
package conditional
import (
"fmt"
"github.com/sicko7947/gorkflow"
)
// NewConditionalWorkflow demonstrates conditional step execution using ThenStepIf
// This workflow conditionally executes steps based on input flags
func NewConditionalWorkflow() (*gorkflow.Workflow, error) {
// Condition: Only double if EnableDoubling flag is true in state
shouldDouble := func(ctx *gorkflow.StepContext) (bool, error) {
var enableDoubling bool
if err := ctx.State.Get("enable_doubling", &enableDoubling); err != nil {
ctx.Logger.Warn().Err(err).Msg("Failed to get enable_doubling from state, defaulting to false")
return false, nil
}
ctx.Logger.Info().Bool("enable_doubling", enableDoubling).Msg("Evaluating doubling condition")
return enableDoubling, nil
}
// Condition: Only format if the value from the previous step is > 10
// This demonstrates checking the output of a previous step in the condition
shouldFormat := func(ctx *gorkflow.StepContext) (bool, error) {
// "double" is the ID of the NewDoubleStep()
doubleOut, err := gorkflow.GetOutput[DoubleOutput](ctx, "double")
if err != nil {
ctx.Logger.Warn().Err(err).Msg("Failed to get output from 'double' step")
return false, nil
}
// Only run if value is greater than 10
shouldRun := doubleOut.Value > 10
ctx.Logger.Info().
Int("value", doubleOut.Value).
Bool("should_run", shouldRun).
Msg("Evaluating formatting condition based on previous step output")
return shouldRun, nil
}
// Default output when doubling is skipped
doubleDefault := &DoubleOutput{
Value: 0,
Doubled: false,
Message: "Doubling was skipped",
}
// Build workflow with conditional steps using ThenStepIf
wf, err := gorkflow.NewWorkflow("conditional_example", "Conditional Execution Example").
WithDescription("Demonstrates conditional step execution with ThenStepIf").
WithVersion("1.0").
WithConfig(gorkflow.ExecutionConfig{
MaxRetries: 2,
RetryDelayMs: 1000,
TimeoutSeconds: 10,
}).
// Step 1: Setup - extract flags from input
ThenStep(NewSetupStep()).
// Step 2: Conditionally double the value (using ThenStepIf)
ThenStepIf(NewDoubleStep(), shouldDouble, doubleDefault).
// Step 3: Conditionally format the result (using ThenStepIf)
ThenStepIf(NewConditionalFormatStep(), shouldFormat, nil).
Build()
if err != nil {
return nil, fmt.Errorf("failed to build conditional workflow: %w", err)
}
return wf, nil
}