base.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package component
  2. import (
  3. "fmt"
  4. )
  5. type BaseComponent struct {
  6. typeName string
  7. name string
  8. runParams map[string]any
  9. }
  10. func NewBaseComponent(typeName string, name string, runParams map[string]any) *BaseComponent {
  11. return &BaseComponent{
  12. typeName: typeName,
  13. name: name,
  14. runParams: runParams,
  15. }
  16. }
  17. func (c *BaseComponent) GetType() string {
  18. return c.typeName
  19. }
  20. func (c *BaseComponent) GetName() string {
  21. return c.name
  22. }
  23. type RunFunc func(globalRunParams *GlobalRunParams, dynamicParams map[string]any) (any, error)
  24. func (c *BaseComponent) OnRun(globalRunParams *GlobalRunParams, dynamicParams map[string]any, runFunc RunFunc) (any, error) {
  25. if globalRunParams == nil {
  26. globalRunParams = new(GlobalRunParams)
  27. }
  28. fmt.Println("----------" + c.typeName + "::" + c.name + "开始运行----------")
  29. if dynamicParams == nil {
  30. dynamicParams = make(map[string]any)
  31. }
  32. for key, params := range c.runParams {
  33. _, ok := dynamicParams[key]
  34. if !ok {
  35. dynamicParams[key] = params
  36. }
  37. }
  38. result, err := runFunc(globalRunParams, dynamicParams)
  39. if err != nil {
  40. return nil, err
  41. }
  42. if result != nil {
  43. globalRunParams.SetLastResult(result)
  44. }
  45. fmt.Println("----------" + c.typeName + "类型的" + c.name + "结束运行----------")
  46. return result, nil
  47. }