演示 2: 复杂 Workflow
(ctx context.Context)
| 74 | |
| 75 | // 演示 2: 复杂 Workflow |
| 76 | func demoComplexWorkflow(ctx context.Context) { |
| 77 | // 创建一个数据处理 workflow |
| 78 | wf := workflow.New("DataPipeline"). |
| 79 | WithStream(). |
| 80 | WithDebug() |
| 81 | |
| 82 | // 步骤 1: 数据收集 |
| 83 | wf.AddStep(workflow.NewFunctionStep("collect", |
| 84 | func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 85 | fmt.Println(" 📥 收集数据...") |
| 86 | return &workflow.StepOutput{ |
| 87 | Content: map[string]any{ |
| 88 | "data": []int{1, 2, 3, 4, 5}, |
| 89 | "source": "api", |
| 90 | "quality": "high", |
| 91 | }, |
| 92 | Metadata: make(map[string]any), |
| 93 | }, nil |
| 94 | }, |
| 95 | )) |
| 96 | |
| 97 | // 步骤 2: 条件分支 - 根据质量选择处理方式 |
| 98 | highQualityStep := workflow.NewFunctionStep("high_quality", |
| 99 | func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 100 | fmt.Println(" ⚡ 使用高级算法处理...") |
| 101 | data := input.PreviousStepContent.(map[string]any) |
| 102 | return &workflow.StepOutput{ |
| 103 | Content: fmt.Sprintf("高级处理: %v", data["data"]), |
| 104 | Metadata: make(map[string]any), |
| 105 | }, nil |
| 106 | }, |
| 107 | ) |
| 108 | |
| 109 | lowQualityStep := workflow.NewFunctionStep("low_quality", |
| 110 | func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 111 | fmt.Println(" 🔧 使用基础算法处理...") |
| 112 | data := input.PreviousStepContent.(map[string]any) |
| 113 | return &workflow.StepOutput{ |
| 114 | Content: fmt.Sprintf("基础处理: %v", data["data"]), |
| 115 | Metadata: make(map[string]any), |
| 116 | }, nil |
| 117 | }, |
| 118 | ) |
| 119 | |
| 120 | wf.AddStep(workflow.NewConditionStep("quality_check", |
| 121 | func(input *workflow.StepInput) bool { |
| 122 | data := input.PreviousStepContent.(map[string]any) |
| 123 | return data["quality"] == "high" |
| 124 | }, |
| 125 | highQualityStep, |
| 126 | lowQualityStep, |
| 127 | )) |
| 128 | |
| 129 | // 步骤 3: 并行任务 |
| 130 | task1 := workflow.NewFunctionStep("validate", |
| 131 | func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 132 | fmt.Println(" ✓ 验证结果...") |
| 133 | return &workflow.StepOutput{Content: "验证通过", Metadata: make(map[string]any)}, nil |
no test coverage detected