-
Notifications
You must be signed in to change notification settings - Fork 11
/
workflow.go
256 lines (221 loc) 路 4.8 KB
/
workflow.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package workflow
import (
"encoding/json"
"reflect"
py "github.com/termkit/gama/pkg/yaml"
)
type Workflow struct {
// Content is a map of key and value designed for workflow_dispatch.inputs
Content map[string]Content
}
type Content struct {
Description string
Type string
Required bool
// KeyValue is a map of key and value designed for JSONContent
KeyValue *[]KeyValue
// Choice is a map of key and value designed for Options
Choice *Choice
// Value is a map of string and value designed for string
Value *Value
}
type KeyValue struct {
Default string
Key string
Value string
}
type Value struct {
Default any
Value any
}
type Choice struct {
Default string
Options []string
Value string
}
// TODO: Add support for boolean
func ParseWorkflow(content py.WorkflowContent) (*Workflow, error) {
w := &Workflow{
Content: make(map[string]Content),
}
for key, value := range content.On.WorkflowDispatch.Inputs {
if value.JSONContent != nil && len(value.JSONContent) > 0 {
var keyValue []KeyValue
for k, v := range value.JSONContent {
keyValue = append(keyValue, KeyValue{
Key: k,
Value: "",
Default: v,
})
}
w.Content[key] = Content{
Description: value.Description,
Type: "json",
Required: value.Required,
KeyValue: &keyValue,
}
continue // Skip the rest of the loop
}
if value.Type == "choice" {
if value.Default == nil {
value.Default = value.Options[0]
}
w.Content[key] = Content{
Description: value.Description,
Type: "choice",
Required: value.Required,
Choice: &Choice{
Default: value.Default.(string),
Options: value.Options,
Value: "",
},
}
}
if value.Type == "string" || value.Type == "boolean" || value.Type == "number" || value.Type == "" {
defaultValue := ""
if value.Default != nil {
_, ok := value.Default.(string)
if ok {
defaultValue = value.Default.(string)
}
}
w.Content[key] = Content{
Description: value.Description,
Type: "input",
Required: value.Required,
Value: &Value{
Default: defaultValue,
Value: "",
},
}
}
}
return w, nil
}
func (w *Workflow) ToPretty() *Pretty {
var pretty Pretty
var id int
for parent, data := range w.Content {
if data.KeyValue != nil {
for _, v := range *data.KeyValue {
pretty.KeyVals = append(pretty.KeyVals, PrettyKeyValue{
ID: id,
Parent: stringPtr(parent),
Key: v.Key,
Value: "",
Default: v.Default,
})
id++
}
}
if data.Choice != nil {
pretty.Choices = append(pretty.Choices, PrettyChoice{
ID: id,
Key: parent,
Value: "",
Values: data.Choice.Options,
Default: data.Choice.Default,
})
id++
}
if data.Value != nil {
var defaultValue string
if data.Value.Default != nil {
_, ok := data.Value.Default.(string)
if ok {
defaultValue = data.Value.Default.(string)
}
}
pretty.Inputs = append(pretty.Inputs, PrettyInput{
ID: id,
Key: parent,
Value: "",
Default: defaultValue,
})
id++
}
}
return &pretty
}
func (p *Pretty) ToJson() (string, error) {
// Create a map to hold the aggregated data
result := make(map[string]interface{})
// Process KeyVals
for _, kv := range p.KeyVals {
if kv.Parent != nil {
parent := *kv.Parent
if _, ok := result[parent]; !ok {
result[parent] = make(map[string]any)
}
result[parent].(map[string]any)[kv.Key] = kv.Value
} else {
result[kv.Key] = kv.Value
}
}
// Process Choices
for _, c := range p.Choices {
result[c.Key] = c.Value
}
// Process Inputs
for _, i := range p.Inputs {
result[i.Key] = i.Value
}
if err := convertJsonToString(result); err != nil {
return "", err
}
modifiedJSON, err := json.Marshal(result)
if err != nil {
return "", err
}
return string(modifiedJSON), nil
}
func convertJsonToString(m map[string]interface{}) error {
for k, v := range m {
if reflect.TypeOf(v).Kind() == reflect.Map {
// Convert map to a JSON string
str, err := json.Marshal(v)
if err != nil {
return err
}
m[k] = string(str)
}
}
return nil
}
type Pretty struct {
Choices []PrettyChoice
Inputs []PrettyInput
KeyVals []PrettyKeyValue
}
type PrettyChoice struct {
ID int
Key string
Value string
Values []string
Default string
}
func (c *PrettyChoice) SetValue(value string) {
c.Value = value
}
type PrettyInput struct {
ID int
Key string
Value string
Default string
}
func (i *PrettyInput) SetValue(value string) {
i.Value = value
}
type PrettyKeyValue struct {
ID int
Parent *string
Key string
Value string
Default string
}
func (kv *PrettyKeyValue) SetValue(value string) {
kv.Value = value
}
func stringPtr(s string) *string {
return &s
}