-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonschema.go
120 lines (104 loc) · 2.87 KB
/
jsonschema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"github.com/bcc-code/bcc-media-flows/workflows"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/google/uuid"
"github.com/invopop/jsonschema"
"github.com/samber/lo"
"go.temporal.io/sdk/client"
)
type WorkflowSchema struct {
Name string `json:"name"`
Schema *jsonschema.Schema `json:"schema"`
}
func getWorkflowSchemas(ctx *gin.Context) {
var schemas []WorkflowSchema
for _, wf := range workflows.TriggerableWorkflows {
typ := reflect.TypeOf(wf)
if typ.NumIn() > 1 {
name, _ := getFunctionName(wf)
arg2Type := typ.In(1)
// log the name of the type
fmt.Printf("Type: %s\n", arg2Type.Name())
// log the fields of the type
for i := 0; i < arg2Type.NumField(); i++ {
field := arg2Type.Field(i)
fmt.Printf("Type: %s, Field: %s\n", arg2Type.Name(), field.Name)
}
schema := jsonschema.ReflectFromType(arg2Type)
fmt.Printf("Type: %s, Schema: %v\n", arg2Type.Name(), schema)
fmt.Println()
schemas = append(schemas, WorkflowSchema{
Name: name,
Schema: schema,
})
}
}
ctx.JSON(http.StatusOK, schemas)
}
func triggerDynamicHandler(ctx *gin.Context) {
wfClient, err := getClient()
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
defer wfClient.Close()
queue := getQueue()
workflowOptions := client.StartWorkflowOptions{
ID: uuid.NewString(),
TaskQueue: queue,
}
// use reflection to trigger an aribtrary workflow
workflowName := getParamFromCtx(ctx, "workflow")
if workflowName == "" {
ctx.Status(http.StatusBadRequest)
return
}
var rawMessage json.RawMessage
if err := ctx.ShouldBindBodyWith(&rawMessage, binding.JSON); err != nil {
ctx.Status(http.StatusBadRequest)
return
}
workflows := workflows.TriggerableWorkflows
wf, found := lo.Find(workflows, func(wf any) bool {
name, _ := getFunctionName(wf)
return name == workflowName
})
if !found {
ctx.Status(http.StatusNotFound)
return
}
typ := reflect.TypeOf(wf)
var arg2 interface{}
if typ.In(1).Kind() == reflect.Ptr {
// If the second argument is a pointer, we need to create a new instance of the underlying type
arg2 = reflect.New(typ.In(1).Elem()).Interface()
} else {
// Otherwise, we can just create a new instance of the type itself
arg2 = reflect.New(typ.In(1)).Interface()
}
err = json.Unmarshal(rawMessage, arg2)
if err != nil {
ctx.Status(http.StatusBadRequest)
return
}
// If arg2 was not a pointer, we need to dereference it before passing to ExecuteWorkflow
if typ.In(1).Kind() != reflect.Ptr {
arg2 = reflect.ValueOf(arg2).Elem().Interface()
}
res, err := wfClient.ExecuteWorkflow(ctx, workflowOptions, wf, arg2)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
ctx.JSON(http.StatusOK, res)
}