Ver código fonte

添加add nodes函数

yjp 3 meses atrás
pai
commit
a10210ff40

+ 8 - 0
approve_former/and.go

@@ -38,6 +38,14 @@ func (and *And) GetWhen() string {
 	return when
 }
 
+func (and *And) AddNodes(childNodes ...Node) *And {
+	if childNodes == nil || len(childNodes) == 0 {
+		and.childNodes = append(and.childNodes, childNodes...)
+	}
+
+	return and
+}
+
 func (and *And) Render(upperNode Node) (string, error) {
 	var content string
 

+ 15 - 7
approve_former/flow.go

@@ -46,14 +46,14 @@ spec:
               supplied: {}`
 
 type Flow struct {
-	Name       string
-	ChildNodes []Node
+	name       string
+	childNodes []Node
 }
 
 func NewFlow(name string, childNodes ...Node) *Flow {
 	return &Flow{
-		Name:       name,
-		ChildNodes: childNodes,
+		name:       name,
+		childNodes: childNodes,
 	}
 }
 
@@ -65,9 +65,17 @@ func (flow *Flow) GetWhen() string {
 	return ""
 }
 
+func (flow *Flow) AddNodes(childNodes ...Node) *Flow {
+	if childNodes == nil || len(childNodes) == 0 {
+		flow.childNodes = append(flow.childNodes, childNodes...)
+	}
+
+	return flow
+}
+
 func (flow *Flow) Render(_ Node) (string, error) {
 	childNodesContents := make([]string, 0)
-	for i, childNode := range flow.ChildNodes {
+	for i, childNode := range flow.childNodes {
 		var childNodeContent string
 
 		if i == 0 {
@@ -78,7 +86,7 @@ func (flow *Flow) Render(_ Node) (string, error) {
 
 			childNodeContent = content
 		} else {
-			content, err := childNode.Render(flow.ChildNodes[i-1])
+			content, err := childNode.Render(flow.childNodes[i-1])
 			if err != nil {
 				return "", err
 			}
@@ -90,7 +98,7 @@ func (flow *Flow) Render(_ Node) (string, error) {
 	}
 
 	return render("flow", flowTemplate, map[string]any{
-		"name":                   flow.Name,
+		"name":                   flow.name,
 		"childNodesContentLines": childNodesContents,
 	})
 }

+ 0 - 72
approve_former/flow_template.go

@@ -1,72 +0,0 @@
-package approve_former
-
-import (
-	"bytes"
-	"github.com/pkg/errors"
-	"text/template"
-)
-
-const workflowTemplateTpl = `
-[[- $workflowTemplateName := .real_workflow_name -]]
-[[- $rootChildren := .children -]]
-
-apiVersion: argoproj.io/v1alpha1
-kind: WorkflowTemplate
-metadata:
-  name: [[ $workflowTemplateName ]]
-spec:
-  entrypoint: approve-flow
-  arguments:
-    parameters:
-      - name: object
-        value: {}
-
-  templates:
-    - name: approve-flow
-      inputs:
-        parameters:
-          - name: object
-      dag:
-        tasks:
-          [[ template "template" $rootChildren ]]
-    - name: approval
-      suspend: {}
-      inputs:
-        parameters:
-          - name: approve
-            default: '通过'
-            enum:
-              - '通过'
-              - '拒绝'
-              - '忽略'
-      outputs:
-        parameters:
-          - name: approve
-            valueFrom:
-              supplied: {}
-
-[[- define "template" -]]
-- name: node
-  template: approval
-[[- end -]]
-`
-
-func ParseWorkflowTemplate(name string, templateParams map[string]any) (string, error) {
-	tpl := template.New(name)
-	tpl.Delims("[[", "]]")
-
-	tpl, err := tpl.Parse(workflowTemplateTpl)
-	if err != nil {
-		return "", errors.New(err.Error())
-	}
-
-	templateParams["real_workflow_name"] = name
-
-	buffer := &bytes.Buffer{}
-	err = tpl.Execute(buffer, templateParams)
-	if err != nil {
-		return "", errors.New(err.Error())
-	}
-
-	return buffer.String(), nil
-}

+ 8 - 0
approve_former/or.go

@@ -38,6 +38,14 @@ func (or *Or) GetWhen() string {
 	return when
 }
 
+func (or *Or) AddNodes(childNodes ...Node) *Or {
+	if childNodes == nil || len(childNodes) == 0 {
+		or.childNodes = append(or.childNodes, childNodes...)
+	}
+
+	return or
+}
+
 func (or *Or) Render(upperNode Node) (string, error) {
 	var content string