if.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package flow
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-tools/utils/pipeline/component"
  5. "github.com/mitchellh/mapstructure"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. TypeIf = "if"
  10. )
  11. type IfBuildParams struct {
  12. ConditionComponent *SubComponentBuildParams `mapstructure:"condition" structs:"condition"`
  13. TrueComponent *SubComponentBuildParams `mapstructure:"condition_true" structs:"condition_true"`
  14. FalseComponent *SubComponentBuildParams `mapstructure:"condition_false" structs:"condition_false"`
  15. }
  16. func (params *IfBuildParams) Check() error {
  17. if params.ConditionComponent == nil {
  18. return errors.New(TypeIf + "流程的条件参数为空")
  19. }
  20. if params.TrueComponent == nil {
  21. return errors.New(TypeIf + "流程的真组件参数为空")
  22. }
  23. if params.FalseComponent == nil {
  24. return errors.New(TypeIf + "流程的假组件参数为空")
  25. }
  26. err := params.ConditionComponent.Check(TypeIf)
  27. if err != nil {
  28. return err
  29. }
  30. err = params.TrueComponent.Check(TypeIf)
  31. if err != nil {
  32. return err
  33. }
  34. if params.ConditionComponent.Name == params.TrueComponent.Name ||
  35. params.TrueComponent.Name == params.FalseComponent.Name ||
  36. params.ConditionComponent.Name == params.FalseComponent.Name {
  37. return fmt.Errorf("流程中存在同名的组件: condition: %s true: %s false: %s",
  38. params.ConditionComponent.Name, params.TrueComponent.Name, params.FalseComponent.Name)
  39. }
  40. return nil
  41. }
  42. type If struct {
  43. component.BaseComponent
  44. ConditionComponent component.Component
  45. TrueComponent component.Component
  46. FalseComponent component.Component
  47. }
  48. func (f *If) Run(globalRunParams *component.GlobalRunParams, dynamicParams map[string]any) (any, error) {
  49. return f.OnRun(globalRunParams, dynamicParams,
  50. func(globalRunParams *component.GlobalRunParams, dynamicParams map[string]any) (any, error) {
  51. lastResult := globalRunParams.GetLastResult()
  52. var conditionDynamicParamsMap map[string]any
  53. conditionDynamicParams, ok := dynamicParams[f.ConditionComponent.GetName()]
  54. if ok {
  55. innerConditionDynamicParamsMap, ok := conditionDynamicParams.(map[string]any)
  56. if !ok {
  57. return nil, errors.New(TypeIf + "流程" + f.GetName() + "的条件组件" + f.ConditionComponent.GetName() + "动态参数类型错误")
  58. }
  59. conditionDynamicParamsMap = innerConditionDynamicParamsMap
  60. }
  61. conditionResult, err := f.ConditionComponent.Run(globalRunParams, conditionDynamicParamsMap)
  62. if err != nil {
  63. return nil, err
  64. }
  65. condition, ok := conditionResult.(bool)
  66. if !ok {
  67. return nil, errors.New(TypeIf + "流程的条件子组件返回结果应当为bool")
  68. }
  69. if condition {
  70. var trueDynamicParamsMap map[string]any
  71. trueDynamicParams, ok := dynamicParams[f.TrueComponent.GetName()]
  72. if ok {
  73. innerTrueDynamicParamsMap, ok := trueDynamicParams.(map[string]any)
  74. if !ok {
  75. return nil, errors.New(TypeIf + "流程" + f.GetName() + "的真组件" + f.TrueComponent.GetName() + "动态参数类型错误")
  76. }
  77. trueDynamicParamsMap = innerTrueDynamicParamsMap
  78. }
  79. result, err := f.TrueComponent.Run(globalRunParams, trueDynamicParamsMap)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return result, nil
  84. } else {
  85. if f.FalseComponent != nil {
  86. var falseDynamicParamsMap map[string]any
  87. falseDynamicParams, ok := dynamicParams[f.FalseComponent.GetName()]
  88. if ok {
  89. innerFalseDynamicParamsMap, ok := falseDynamicParams.(map[string]any)
  90. if !ok {
  91. return nil, errors.New(TypeIf + "流程" + f.GetName() + "的假组件" + f.FalseComponent.GetName() + "动态参数类型错误")
  92. }
  93. falseDynamicParamsMap = innerFalseDynamicParamsMap
  94. }
  95. result, err := f.FalseComponent.Run(globalRunParams, falseDynamicParamsMap)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return result, nil
  100. }
  101. }
  102. return lastResult, nil
  103. })
  104. }
  105. type IfBuilder struct{}
  106. func (builder *IfBuilder) ProductType() string {
  107. return TypeIf
  108. }
  109. func (builder *IfBuilder) Build(name string, buildParams map[string]any, runParams map[string]any) (component.Component, error) {
  110. if buildParams == nil || len(buildParams) == 0 {
  111. return nil, errors.New(TypeIf + "流没有传递构建参数")
  112. }
  113. flowBuildParams := new(IfBuildParams)
  114. err := mapstructure.Decode(buildParams, flowBuildParams)
  115. if err != nil {
  116. return nil, errors.New(TypeIf + "流程构建参数Decode失败: " + err.Error())
  117. }
  118. err = flowBuildParams.Check()
  119. if err != nil {
  120. return nil, err
  121. }
  122. conditionFlow, err := flowBuildParams.ConditionComponent.BuildComponent()
  123. if err != nil {
  124. return nil, err
  125. }
  126. trueFlow, err := flowBuildParams.TrueComponent.BuildComponent()
  127. if err != nil {
  128. return nil, err
  129. }
  130. var falseFlow component.Component
  131. if flowBuildParams.FalseComponent != nil {
  132. innerFalseFlow, err := flowBuildParams.FalseComponent.BuildComponent()
  133. if err != nil {
  134. return nil, err
  135. }
  136. falseFlow = innerFalseFlow
  137. }
  138. return &If{
  139. BaseComponent: *component.NewBaseComponent(TypeIf, name, runParams),
  140. ConditionComponent: conditionFlow,
  141. TrueComponent: trueFlow,
  142. FalseComponent: falseFlow,
  143. }, nil
  144. }