improve condition evaluate
This commit is contained in:
@@ -98,7 +98,9 @@ func (w *workflowInvoker) processNode(ctx context.Context, node *domain.Workflow
|
||||
|
||||
procErr = processor.Process(ctx)
|
||||
if procErr != nil {
|
||||
processor.GetLogger().Error(procErr.Error())
|
||||
if current.Type != domain.WorkflowNodeTypeCondition {
|
||||
processor.GetLogger().Error(procErr.Error())
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
@@ -110,9 +112,12 @@ func (w *workflowInvoker) processNode(ctx context.Context, node *domain.Workflow
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
// TODO: 优化可读性
|
||||
if procErr != nil && current.Next != nil && current.Next.Type != domain.WorkflowNodeTypeExecuteResultBranch {
|
||||
if procErr != nil && current.Type == domain.WorkflowNodeTypeCondition {
|
||||
current = nil
|
||||
procErr = nil
|
||||
return nil
|
||||
} else if procErr != nil && current.Next != nil && current.Next.Type != domain.WorkflowNodeTypeExecuteResultBranch {
|
||||
return procErr
|
||||
} else if procErr != nil && current.Next != nil && current.Next.Type == domain.WorkflowNodeTypeExecuteResultBranch {
|
||||
current = w.getBranchByType(current.Next.Branches, domain.WorkflowNodeTypeExecuteFailure)
|
||||
|
||||
@@ -26,27 +26,26 @@ func (n *conditionNode) Process(ctx context.Context) error {
|
||||
|
||||
nodeConfig := n.node.GetConfigForCondition()
|
||||
if nodeConfig.Expression == nil {
|
||||
n.logger.Info("no condition found, continue to next node")
|
||||
return nil
|
||||
}
|
||||
|
||||
rs, err := n.eval(ctx, nodeConfig.Expression)
|
||||
if err != nil {
|
||||
n.logger.Warn("failed to eval expression: " + err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
if rs.Value == false {
|
||||
n.logger.Info("condition not met, skip this branch")
|
||||
return errors.New("condition not met")
|
||||
}
|
||||
|
||||
n.logger.Info("condition met, continue to next node")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *conditionNode) eval(ctx context.Context, expression domain.Expr) (any, error) {
|
||||
switch expr:=expression.(type) {
|
||||
case domain.CompareExpr:
|
||||
left,err:= n.eval(ctx, expr.Left)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
right,err:= n.eval(ctx, expr.Right)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
case domain.LogicalExpr:
|
||||
case domain.NotExpr:
|
||||
case domain.VarExpr:
|
||||
case domain.ConstExpr:
|
||||
}
|
||||
return false, errors.New("unknown expression type")
|
||||
func (n *conditionNode) eval(ctx context.Context, expression domain.Expr) (*domain.EvalResult, error) {
|
||||
variables := GetNodeOutputs(ctx)
|
||||
return expression.Eval(variables)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user