Procházet zdrojové kódy

json生成yaml模板

duyong mac před 3 měsíci
rodič
revize
ed829e40b8

+ 70 - 0
approve_former/flow_template.go

@@ -0,0 +1,70 @@
+package approve_former
+
+import (
+	"bytes"
+	"github.com/pkg/errors"
+	"text/template"
+)
+
+const workflowTemplateTpl = `
+[[- $workflowTemplateName := .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())
+	}
+
+	buffer := &bytes.Buffer{}
+	err = tpl.Execute(buffer, templateParams)
+	if err != nil {
+		return "", errors.New(err.Error())
+	}
+
+	return buffer.String(), nil
+}

+ 535 - 0
test/approve/approve.json

@@ -0,0 +1,535 @@
+{
+  "name": "发起人",
+  "desc": "任何人",
+  "type": "ROOT",
+  "parentId": null,
+  "id": "root",
+  "props": {
+    "assignedUser": [],
+    "formPerms": []
+  },
+  "children": {
+    "id": "9oMJ0ZGUSPsz3fGQ",
+    "name": "或签审批",
+    "type": "APPROVAL",
+    "props": {
+      "assignedType": "ASSIGN_USER",
+      "mode": "OR",
+      "sign": false,
+      "nobody": {
+        "handler": "TO_PASS",
+        "assignedUser": [],
+        "assignedRole": []
+      },
+      "timeLimit": {
+        "timeout": {
+          "unit": "H",
+          "value": 0
+        },
+        "handler": {
+          "type": "REFUSE",
+          "notify": {
+            "once": true,
+            "hour": 1
+          }
+        }
+      },
+      "assignedUser": [
+        {
+          "type": "user",
+          "subjectId": "81d10c9f19c244088c0066a6ba13bab7",
+          "name": "质检人员",
+          "id": "81d10c9f19c244088c0066a6ba13bab7"
+        },
+        {
+          "type": "user",
+          "subjectId": "c09bb598a48147e2a37f43e4130df843",
+          "name": "行政人员",
+          "id": "c09bb598a48147e2a37f43e4130df843"
+        }
+      ],
+      "assignedRole": [],
+      "formPerms": [
+        {
+          "id": "field4368279468887",
+          "title": "名称",
+          "required": true,
+          "perm": "R"
+        }
+      ],
+      "selfSelect": {
+        "multiple": false
+      },
+      "leaderTop": {
+        "endCondition": "TOP",
+        "endLevel": 1
+      },
+      "leader": {
+        "level": 1
+      },
+      "role": [],
+      "refuse": {
+        "type": "TO_END",
+        "target": ""
+      },
+      "formUser": "",
+      "passHttp": {
+        "method": "POST",
+        "url": "http://10.0.0.198:30701/mbwb/api/audit",
+        "headers": [
+          {
+            "name": "",
+            "isField": true,
+            "value": ""
+          }
+        ],
+        "contentType": "JSON",
+        "params": [
+          {
+            "name": "",
+            "isField": true,
+            "value": ""
+          }
+        ],
+        "retry": 1,
+        "handlerByScript": false,
+        "success": "function handlerOk(res) {\n  return true;\n}",
+        "fail": "function handlerFail(res) {\n  return true;\n}"
+      },
+      "rejectHttp": {
+        "method": "POST",
+        "url": "http://10.0.0.198:30701/mbwb/api/refuse",
+        "headers": [
+          {
+            "name": "",
+            "isField": true,
+            "value": ""
+          }
+        ],
+        "contentType": "JSON",
+        "params": [
+          {
+            "name": "",
+            "isField": true,
+            "value": ""
+          }
+        ],
+        "retry": 1,
+        "handlerByScript": false,
+        "success": "function handlerOk(res) {\n  return true;\n}",
+        "fail": "function handlerFail(res) {\n  return true;\n}"
+      }
+    },
+    "error": false,
+    "children": {
+      "id": "UnDmPp4P5JVP5DMT",
+      "name": "会签审批",
+      "type": "APPROVAL",
+      "props": {
+        "assignedType": "ASSIGN_USER",
+        "mode": "NEXT",
+        "sign": false,
+        "nobody": {
+          "handler": "TO_PASS",
+          "assignedUser": [],
+          "assignedRole": []
+        },
+        "timeLimit": {
+          "timeout": {
+            "unit": "H",
+            "value": 0
+          },
+          "handler": {
+            "type": "REFUSE",
+            "notify": {
+              "once": true,
+              "hour": 1
+            }
+          }
+        },
+        "assignedUser": [
+          {
+            "type": "user",
+            "subjectId": "afb08622b0144502b1294aad1021b876",
+            "name": "生产调度",
+            "id": "afb08622b0144502b1294aad1021b876"
+          },
+          {
+            "type": "user",
+            "subjectId": "2013392b19824330881c896a34fa0e77",
+            "name": "仓管人员",
+            "id": "2013392b19824330881c896a34fa0e77"
+          },
+          {
+            "type": "user",
+            "subjectId": "cbe10e4bc82145a8b57769821fbbf761",
+            "name": "销售人员",
+            "id": "cbe10e4bc82145a8b57769821fbbf761"
+          }
+        ],
+        "assignedRole": [],
+        "formPerms": [
+          {
+            "id": "field4368279468887",
+            "title": "名称",
+            "required": true,
+            "perm": "R"
+          }
+        ],
+        "selfSelect": {
+          "multiple": false
+        },
+        "leaderTop": {
+          "endCondition": "TOP",
+          "endLevel": 1
+        },
+        "leader": {
+          "level": 1
+        },
+        "role": [],
+        "refuse": {
+          "type": "TO_END",
+          "target": ""
+        },
+        "formUser": "",
+        "passHttp": {
+          "method": "POST",
+          "url": "http://10.0.0.198:30701/mbwb/api/audit",
+          "headers": [
+            {
+              "name": "",
+              "isField": true,
+              "value": ""
+            }
+          ],
+          "contentType": "JSON",
+          "params": [
+            {
+              "name": "",
+              "isField": true,
+              "value": ""
+            }
+          ],
+          "retry": 1,
+          "handlerByScript": false,
+          "success": "function handlerOk(res) {\n  return true;\n}",
+          "fail": "function handlerFail(res) {\n  return true;\n}"
+        },
+        "rejectHttp": {
+          "method": "POST",
+          "url": "http://10.0.0.198:30701/mbwb/api/refuse",
+          "headers": [
+            {
+              "name": "",
+              "isField": true,
+              "value": ""
+            }
+          ],
+          "contentType": "JSON",
+          "params": [
+            {
+              "name": "",
+              "isField": true,
+              "value": ""
+            }
+          ],
+          "retry": 1,
+          "handlerByScript": false,
+          "success": "function handlerOk(res) {\n  return true;\n}",
+          "fail": "function handlerFail(res) {\n  return true;\n}"
+        }
+      },
+      "error": false,
+      "children": {
+        "id": "UBtBe6PTv0kQDm4B",
+        "name": "并行分支",
+        "type": "CONCURRENTS",
+        "children": {
+          "id": "ntWxexK46WotEF0C",
+          "name": "抄送人",
+          "children": {
+            "ParentID": "ntWxexK46WotEF0C"
+          },
+          "type": "CC",
+          "props": {
+            "shouldAdd": false,
+            "assignedUser": [
+              {
+                "type": "user",
+                "subjectId": "81d10c9f19c244088c0066a6ba13bab7",
+                "name": "质检人员",
+                "id": "81d10c9f19c244088c0066a6ba13bab7"
+              },
+              {
+                "type": "user",
+                "subjectId": "2013392b19824330881c896a34fa0e77",
+                "name": "仓管人员",
+                "id": "2013392b19824330881c896a34fa0e77"
+              },
+              {
+                "type": "user",
+                "subjectId": "ff4466c00b944584b567a454f94fb8ce",
+                "name": "采购人员",
+                "id": "ff4466c00b944584b567a454f94fb8ce"
+              },
+              {
+                "type": "user",
+                "subjectId": "2d79964af26b4534a09a4a57b2f56d44",
+                "name": "系统人员",
+                "id": "2d79964af26b4534a09a4a57b2f56d44"
+              }
+            ],
+            "assignedRole": [],
+            "formPerms": [
+              {
+                "id": "field4368279468887",
+                "title": "名称",
+                "required": true,
+                "perm": "R"
+              }
+            ]
+          },
+          "ccSelfSelectFlag": 0,
+          "error": false,
+          "ParentID": "UBtBe6PTv0kQDm4B"
+        },
+        "branchs": [
+          {
+            "id": "node_371077991169",
+            "name": "分支1",
+            "parentId": "node_371077998976",
+            "type": "CONCURRENT",
+            "props": {},
+            "children": {
+              "id": "oSIsJsle3MWLrt26",
+              "name": "角色审批",
+              "type": "APPROVAL",
+              "props": {
+                "assignedType": "ROLE",
+                "mode": "OR",
+                "sign": false,
+                "nobody": {
+                  "handler": "TO_PASS",
+                  "assignedUser": [],
+                  "assignedRole": []
+                },
+                "timeLimit": {
+                  "timeout": {
+                    "unit": "H",
+                    "value": 0
+                  },
+                  "handler": {
+                    "type": "REFUSE",
+                    "notify": {
+                      "once": true,
+                      "hour": 1
+                    }
+                  }
+                },
+                "assignedUser": [],
+                "assignedRole": [
+                  {
+                    "type": "role",
+                    "roleCode": "00002",
+                    "name": "平台人员",
+                    "id": "6121fa5cd2ff451da98ad7c451790367"
+                  }
+                ],
+                "formPerms": [
+                  {
+                    "id": "field4368279468887",
+                    "title": "名称",
+                    "required": true,
+                    "perm": "R"
+                  }
+                ],
+                "selfSelect": {
+                  "multiple": false
+                },
+                "leaderTop": {
+                  "endCondition": "TOP",
+                  "endLevel": 1
+                },
+                "leader": {
+                  "level": 1
+                },
+                "role": [],
+                "refuse": {
+                  "type": "TO_END",
+                  "target": ""
+                },
+                "formUser": "",
+                "passHttp": {
+                  "method": "POST",
+                  "url": "",
+                  "headers": [
+                    {
+                      "name": "",
+                      "isField": true,
+                      "value": ""
+                    }
+                  ],
+                  "contentType": "JSON",
+                  "params": [
+                    {
+                      "name": "",
+                      "isField": true,
+                      "value": ""
+                    }
+                  ],
+                  "retry": 1,
+                  "handlerByScript": false,
+                  "success": "function handlerOk(res) {\n  return true;\n}",
+                  "fail": "function handlerFail(res) {\n  return true;\n}"
+                },
+                "rejectHttp": {
+                  "method": "POST",
+                  "url": "",
+                  "headers": [
+                    {
+                      "name": "",
+                      "isField": true,
+                      "value": ""
+                    }
+                  ],
+                  "contentType": "JSON",
+                  "params": [
+                    {
+                      "name": "",
+                      "isField": true,
+                      "value": ""
+                    }
+                  ],
+                  "retry": 1,
+                  "handlerByScript": false,
+                  "success": "function handlerOk(res) {\n  return true;\n}",
+                  "fail": "function handlerFail(res) {\n  return true;\n}"
+                }
+              },
+              "subjects": [],
+              "examineMode": 1,
+              "noHanderAction": 2,
+              "error": false
+            },
+            "typeElse": false
+          },
+          {
+            "id": "node_371077991169",
+            "name": "分支2",
+            "parentId": "node_371077998976",
+            "type": "CONCURRENT",
+            "props": {},
+            "children": {
+              "id": "jxHOnRDBoz0vH0vx",
+              "name": "主管审批",
+              "type": "APPROVAL",
+              "props": {
+                "assignedType": "LEADER",
+                "mode": "OR",
+                "sign": false,
+                "nobody": {
+                  "handler": "TO_PASS",
+                  "assignedUser": [],
+                  "assignedRole": []
+                },
+                "timeLimit": {
+                  "timeout": {
+                    "unit": "H",
+                    "value": 0
+                  },
+                  "handler": {
+                    "type": "REFUSE",
+                    "notify": {
+                      "once": true,
+                      "hour": 1
+                    }
+                  }
+                },
+                "assignedUser": [],
+                "assignedRole": [],
+                "formPerms": [
+                  {
+                    "id": "field4368279468887",
+                    "title": "名称",
+                    "required": true,
+                    "perm": "R"
+                  }
+                ],
+                "selfSelect": {
+                  "multiple": false
+                },
+                "leaderTop": {
+                  "endCondition": "TOP",
+                  "endLevel": 1
+                },
+                "leader": {
+                  "level": 1
+                },
+                "role": [],
+                "refuse": {
+                  "type": "TO_END",
+                  "target": ""
+                },
+                "formUser": "",
+                "passHttp": {
+                  "method": "POST",
+                  "url": "",
+                  "headers": [
+                    {
+                      "name": "",
+                      "isField": true,
+                      "value": ""
+                    }
+                  ],
+                  "contentType": "JSON",
+                  "params": [
+                    {
+                      "name": "",
+                      "isField": true,
+                      "value": ""
+                    }
+                  ],
+                  "retry": 1,
+                  "handlerByScript": false,
+                  "success": "function handlerOk(res) {\n  return true;\n}",
+                  "fail": "function handlerFail(res) {\n  return true;\n}"
+                },
+                "rejectHttp": {
+                  "method": "POST",
+                  "url": "",
+                  "headers": [
+                    {
+                      "name": "",
+                      "isField": true,
+                      "value": ""
+                    }
+                  ],
+                  "contentType": "JSON",
+                  "params": [
+                    {
+                      "name": "",
+                      "isField": true,
+                      "value": ""
+                    }
+                  ],
+                  "retry": 1,
+                  "handlerByScript": false,
+                  "success": "function handlerOk(res) {\n  return true;\n}",
+                  "fail": "function handlerFail(res) {\n  return true;\n}"
+                }
+              },
+              "subjects": [],
+              "examineMode": 1,
+              "noHanderAction": 2,
+              "error": false
+            },
+            "typeElse": false
+          }
+        ],
+        "ParentID": "UnDmPp4P5JVP5DMT"
+      },
+      "ParentID": "9oMJ0ZGUSPsz3fGQ"
+    },
+    "ParentID": "root"
+  }
+}

+ 21 - 0
test/approve_former_test.go

@@ -1,16 +1,37 @@
 package test
 
 import (
+	_ "embed"
+	"encoding/json"
 	"fmt"
 	"git.sxidc.com/go-tools/argo-api"
 	"git.sxidc.com/go-tools/argo-api/approve_former"
 	"git.sxidc.com/go-tools/argo-api/client"
 	"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
+	"github.com/pkg/errors"
 	"sync"
 	"testing"
 	"time"
 )
 
+//go:embed approve/approve.json
+var approveJson []byte
+
+func TestParseWorkflowTemplate(t *testing.T) {
+	approveJsonMap := make(map[string]any)
+	err := json.Unmarshal(approveJson, &approveJsonMap)
+	if err != nil {
+		t.Fatalf("%+v\n", errors.New(err.Error()))
+	}
+
+	workflowTemplateDefinition, err := approve_former.ParseWorkflowTemplate("test", approveJsonMap)
+	if err != nil {
+		t.Fatalf("%+v\n", err)
+	}
+
+	fmt.Println(workflowTemplateDefinition)
+}
+
 func TestApproveFormer(t *testing.T) {
 	flow := approve_former.NewFlow("flow",
 		approve_former.NewSign("A"),