| 307 | func (s *ConditionStep) Config() *StepConfig { return s.config } |
| 308 | |
| 309 | func (s *ConditionStep) Execute(ctx context.Context, input *StepInput) *stream.Reader[*StepOutput] { |
| 310 | reader, writer := stream.Pipe[*StepOutput](1) |
| 311 | |
| 312 | go func() { |
| 313 | defer writer.Close() |
| 314 | startTime := time.Now() |
| 315 | |
| 316 | conditionResult := s.condition(input) |
| 317 | |
| 318 | var branch Step |
| 319 | var branchName string |
| 320 | if conditionResult { |
| 321 | branch = s.ifTrue |
| 322 | branchName = "true" |
| 323 | } else { |
| 324 | branch = s.ifFalse |
| 325 | branchName = "false" |
| 326 | } |
| 327 | |
| 328 | var branchOutput *StepOutput |
| 329 | branchReader := branch.Execute(ctx, input) |
| 330 | for { |
| 331 | output, err := branchReader.Recv() |
| 332 | if err != nil { |
| 333 | if errors.Is(err, io.EOF) { |
| 334 | break |
| 335 | } |
| 336 | errorOutput := &StepOutput{ |
| 337 | StepID: s.id, |
| 338 | StepName: s.name, |
| 339 | StepType: StepTypeCondition, |
| 340 | Error: err, |
| 341 | StartTime: startTime, |
| 342 | EndTime: time.Now(), |
| 343 | Metadata: map[string]any{"condition": conditionResult, "branch": branchName}, |
| 344 | } |
| 345 | errorOutput.Duration = errorOutput.EndTime.Sub(errorOutput.StartTime).Seconds() |
| 346 | writer.Send(errorOutput, err) |
| 347 | return |
| 348 | } |
| 349 | branchOutput = output |
| 350 | } |
| 351 | |
| 352 | output := &StepOutput{ |
| 353 | StepID: s.id, |
| 354 | StepName: s.name, |
| 355 | StepType: StepTypeCondition, |
| 356 | Content: branchOutput.Content, |
| 357 | StartTime: startTime, |
| 358 | EndTime: time.Now(), |
| 359 | NestedSteps: []*StepOutput{branchOutput}, |
| 360 | Metadata: map[string]any{"condition": conditionResult, "branch": branchName}, |
| 361 | Metrics: &StepMetrics{ExecutionTime: time.Since(startTime).Seconds()}, |
| 362 | } |
| 363 | output.Duration = output.EndTime.Sub(output.StartTime).Seconds() |
| 364 | writer.Send(output, nil) |
| 365 | }() |
| 366 | |