123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- package flow
- import (
- "errors"
- "fmt"
- "git.sxidc.com/go-tools/utils/pipeline/component"
- "github.com/mitchellh/mapstructure"
- )
- const (
- TypeIf = "if"
- )
- type IfBuildParams struct {
- ConditionComponent *SubComponentBuildParams `mapstructure:"condition" structs:"condition"`
- TrueComponent *SubComponentBuildParams `mapstructure:"condition_true" structs:"condition_true"`
- FalseComponent *SubComponentBuildParams `mapstructure:"condition_false" structs:"condition_false"`
- }
- func (params *IfBuildParams) Check() error {
- if params.ConditionComponent == nil {
- return errors.New(TypeIf + "流程的条件参数为空")
- }
- if params.TrueComponent == nil {
- return errors.New(TypeIf + "流程的真组件参数为空")
- }
- if params.FalseComponent == nil {
- return errors.New(TypeIf + "流程的假组件参数为空")
- }
- err := params.ConditionComponent.Check(TypeIf)
- if err != nil {
- return err
- }
- err = params.TrueComponent.Check(TypeIf)
- if err != nil {
- return err
- }
- if params.ConditionComponent.Name == params.TrueComponent.Name ||
- params.TrueComponent.Name == params.FalseComponent.Name ||
- params.ConditionComponent.Name == params.FalseComponent.Name {
- return fmt.Errorf("流程中存在同名的组件: condition: %s true: %s false: %s",
- params.ConditionComponent.Name, params.TrueComponent.Name, params.FalseComponent.Name)
- }
- return nil
- }
- type If struct {
- component.BaseComponent
- ConditionComponent component.Component
- TrueComponent component.Component
- FalseComponent component.Component
- }
- func (f *If) Run(globalRunParams *component.GlobalRunParams, dynamicParams map[string]any) (any, error) {
- return f.OnRun(globalRunParams, dynamicParams,
- func(globalRunParams *component.GlobalRunParams, dynamicParams map[string]any) (any, error) {
- lastResult := globalRunParams.GetLastResult()
- var conditionDynamicParamsMap map[string]any
- conditionDynamicParams, ok := dynamicParams[f.ConditionComponent.GetName()]
- if ok {
- innerConditionDynamicParamsMap, ok := conditionDynamicParams.(map[string]any)
- if !ok {
- return nil, errors.New(TypeIf + "流程" + f.GetName() + "的条件组件" + f.ConditionComponent.GetName() + "动态参数类型错误")
- }
- conditionDynamicParamsMap = innerConditionDynamicParamsMap
- }
- conditionResult, err := f.ConditionComponent.Run(globalRunParams, conditionDynamicParamsMap)
- if err != nil {
- return nil, err
- }
- condition, ok := conditionResult.(bool)
- if !ok {
- return nil, errors.New(TypeIf + "流程的条件子组件返回结果应当为bool")
- }
- if condition {
- var trueDynamicParamsMap map[string]any
- trueDynamicParams, ok := dynamicParams[f.TrueComponent.GetName()]
- if ok {
- innerTrueDynamicParamsMap, ok := trueDynamicParams.(map[string]any)
- if !ok {
- return nil, errors.New(TypeIf + "流程" + f.GetName() + "的真组件" + f.TrueComponent.GetName() + "动态参数类型错误")
- }
- trueDynamicParamsMap = innerTrueDynamicParamsMap
- }
- result, err := f.TrueComponent.Run(globalRunParams, trueDynamicParamsMap)
- if err != nil {
- return nil, err
- }
- return result, nil
- } else {
- if f.FalseComponent != nil {
- var falseDynamicParamsMap map[string]any
- falseDynamicParams, ok := dynamicParams[f.FalseComponent.GetName()]
- if ok {
- innerFalseDynamicParamsMap, ok := falseDynamicParams.(map[string]any)
- if !ok {
- return nil, errors.New(TypeIf + "流程" + f.GetName() + "的假组件" + f.FalseComponent.GetName() + "动态参数类型错误")
- }
- falseDynamicParamsMap = innerFalseDynamicParamsMap
- }
- result, err := f.FalseComponent.Run(globalRunParams, falseDynamicParamsMap)
- if err != nil {
- return nil, err
- }
- return result, nil
- }
- }
- return lastResult, nil
- })
- }
- type IfBuilder struct{}
- func (builder *IfBuilder) ProductType() string {
- return TypeIf
- }
- func (builder *IfBuilder) Build(name string, buildParams map[string]any, runParams map[string]any) (component.Component, error) {
- if buildParams == nil || len(buildParams) == 0 {
- return nil, errors.New(TypeIf + "流没有传递构建参数")
- }
- flowBuildParams := new(IfBuildParams)
- err := mapstructure.Decode(buildParams, flowBuildParams)
- if err != nil {
- return nil, errors.New(TypeIf + "流程构建参数Decode失败: " + err.Error())
- }
- err = flowBuildParams.Check()
- if err != nil {
- return nil, err
- }
- conditionFlow, err := flowBuildParams.ConditionComponent.BuildComponent()
- if err != nil {
- return nil, err
- }
- trueFlow, err := flowBuildParams.TrueComponent.BuildComponent()
- if err != nil {
- return nil, err
- }
- var falseFlow component.Component
- if flowBuildParams.FalseComponent != nil {
- innerFalseFlow, err := flowBuildParams.FalseComponent.BuildComponent()
- if err != nil {
- return nil, err
- }
- falseFlow = innerFalseFlow
- }
- return &If{
- BaseComponent: *component.NewBaseComponent(TypeIf, name, runParams),
- ConditionComponent: conditionFlow,
- TrueComponent: trueFlow,
- FalseComponent: falseFlow,
- }, nil
- }
|