range.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package flow
  2. import (
  3. "git.sxidc.com/go-tools/utils/pipeline/component"
  4. "github.com/mitchellh/mapstructure"
  5. "github.com/pkg/errors"
  6. )
  7. const (
  8. TypeRange = "range"
  9. )
  10. const (
  11. RangeIndex = "range_index"
  12. RangeOnceValue = "range_once_value"
  13. )
  14. type RangeBuildParams struct {
  15. Values []any `mapstructure:"values" structs:"values"`
  16. SubComponent *SubComponentBuildParams `mapstructure:"sub" structs:"sub"`
  17. }
  18. func (params *RangeBuildParams) Check() error {
  19. if params.SubComponent == nil {
  20. return errors.New(TypeRange + "流程的子组件参数为空")
  21. }
  22. err := params.SubComponent.Check(TypeRange)
  23. if err != nil {
  24. return err
  25. }
  26. return nil
  27. }
  28. type Range struct {
  29. component.BaseComponent
  30. Values []any
  31. SubComponent component.Component
  32. }
  33. func (f *Range) Run(globalRunParams *component.GlobalRunParams, dynamicParams map[string]any) (any, error) {
  34. return f.OnRun(globalRunParams, dynamicParams,
  35. func(globalRunParams *component.GlobalRunParams, dynamicParams map[string]any) (any, error) {
  36. lastResult := globalRunParams.GetLastResult()
  37. subDynamicParamsMap := make(map[string]any)
  38. subDynamicParams, ok := dynamicParams[f.SubComponent.GetName()]
  39. if ok {
  40. innerSubDynamicParamsMap, ok := subDynamicParams.(map[string]any)
  41. if !ok {
  42. return nil, errors.New(TypeLoop + "流程" + f.GetName() + "的子组件" + f.SubComponent.GetName() + "动态参数类型错误")
  43. }
  44. subDynamicParamsMap = innerSubDynamicParamsMap
  45. }
  46. if f.Values == nil || len(f.Values) == 0 {
  47. return lastResult, nil
  48. }
  49. var result any
  50. for index, value := range f.Values {
  51. subDynamicParamsMap[RangeIndex] = index
  52. subDynamicParamsMap[RangeOnceValue] = value
  53. innerResult, err := f.SubComponent.Run(globalRunParams, subDynamicParamsMap)
  54. if err != nil {
  55. return nil, err
  56. }
  57. result = innerResult
  58. }
  59. return result, nil
  60. })
  61. }
  62. type RangeBuilder struct{}
  63. func (builder *RangeBuilder) ProductType() string {
  64. return TypeRange
  65. }
  66. func (builder *RangeBuilder) Build(name string, buildParams map[string]any, runParams map[string]any) (component.Component, error) {
  67. if buildParams == nil || len(buildParams) == 0 {
  68. return nil, errors.New(TypeRange + "流程没有传递构建参数")
  69. }
  70. flowBuildParams := new(RangeBuildParams)
  71. err := mapstructure.Decode(buildParams, flowBuildParams)
  72. if err != nil {
  73. return nil, errors.New(TypeRange + "流程构建参数Decode失败: " + err.Error())
  74. }
  75. err = flowBuildParams.Check()
  76. if err != nil {
  77. return nil, err
  78. }
  79. subFlow, err := flowBuildParams.SubComponent.BuildComponent()
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &Range{
  84. BaseComponent: *component.NewBaseComponent(TypeLoop, name, runParams),
  85. Values: flowBuildParams.Values,
  86. SubComponent: subFlow,
  87. }, nil
  88. }