flow.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package approve_former
  2. import (
  3. "strings"
  4. )
  5. const flowTemplate = `apiVersion: argoproj.io/v1alpha1
  6. kind: Workflow
  7. metadata:
  8. generateName: [[ .name ]]-
  9. spec:
  10. entrypoint: front-json-flow
  11. arguments:
  12. parameters:
  13. - name: matter
  14. value: [[ .matter ]]
  15. - name: create-user-id
  16. value: [[ .create_user_id ]]
  17. - name: business-object
  18. value: |
  19. [[ .business_object ]]
  20. templates:
  21. - name: front-json-flow
  22. steps:
  23. [[- if .childNodesContentLines ]]
  24. [[- range .childNodesContentLines ]]
  25. [[ . ]]
  26. [[- end ]]
  27. [[- else ]]
  28. - name: parse-flow
  29. template: approval
  30. [[- end ]]
  31. - name: approval
  32. suspend: {}
  33. inputs:
  34. parameters:
  35. - name: front-json
  36. default: '通过'
  37. enum:
  38. - '通过'
  39. - '拒绝'
  40. - '忽略'
  41. outputs:
  42. parameters:
  43. - name: front-json
  44. valueFrom:
  45. supplied: {}`
  46. type FlowBaseInfo struct {
  47. Identifier string
  48. Matter string
  49. CreateUserID string
  50. BusinessObject string
  51. }
  52. type Flow struct {
  53. BaseInfo *FlowBaseInfo
  54. childNodes []Noder
  55. }
  56. func NewFlow(baseInfo *FlowBaseInfo, childNodes ...Noder) *Flow {
  57. return &Flow{
  58. BaseInfo: baseInfo,
  59. childNodes: childNodes,
  60. }
  61. }
  62. func (flow *Flow) GetDepends() string {
  63. return ""
  64. }
  65. func (flow *Flow) GetWhen() string {
  66. return ""
  67. }
  68. func (flow *Flow) AddNodes(childNodes ...Noder) *Flow {
  69. if childNodes == nil || len(childNodes) == 0 {
  70. return flow
  71. }
  72. flow.childNodes = append(flow.childNodes, childNodes...)
  73. return flow
  74. }
  75. func (flow *Flow) Render(_ Noder) (string, error) {
  76. childNodesContents := make([]string, 0)
  77. for i, childNode := range flow.childNodes {
  78. var childNodeContent string
  79. if i == 0 {
  80. content, err := childNode.Render(nil)
  81. if err != nil {
  82. return "", err
  83. }
  84. childNodeContent = content
  85. } else {
  86. content, err := childNode.Render(flow.childNodes[i-1])
  87. if err != nil {
  88. return "", err
  89. }
  90. childNodeContent = content
  91. }
  92. childNodesContents = append(childNodesContents, strings.Split(childNodeContent, "\n")...)
  93. }
  94. return render("flow", flowTemplate, map[string]any{
  95. "name": "flow-" + flow.BaseInfo.Identifier,
  96. "matter": flow.BaseInfo.Matter,
  97. "create_user_id": flow.BaseInfo.CreateUserID,
  98. "business_object": flow.BaseInfo.BusinessObject,
  99. "childNodesContentLines": childNodesContents,
  100. })
  101. }