-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
71 lines (57 loc) · 1.62 KB
/
types.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024-Present Harry Randazzo
package vai
import (
"cmp"
"slices"
"github.com/invopop/jsonschema"
)
// DefaultTaskName is the default task name
const DefaultTaskName = "default"
// DefaultFileName is the default file name
const DefaultFileName = "vai.yaml"
// Task is a list of steps
type Task []Step
// Workflow is a map of tasks, where the key is the task name
//
// This is the main structure that represents `vai.yaml` and other vai workflow files
type Workflow map[string]Task
// Find returns a task by name
func (wf Workflow) Find(call string) (Task, bool) {
task, ok := wf[call]
return task, ok
}
// OrderedTaskNames returns a list of task names in alphabetical order
//
// The default task is always first
func (wf Workflow) OrderedTaskNames() []string {
names := make([]string, 0, len(wf))
for k := range wf {
names = append(names, k)
}
slices.SortStableFunc(names, func(a, b string) int {
if a == DefaultTaskName {
return -1
}
if b == DefaultTaskName {
return 1
}
return cmp.Compare(a, b)
})
return names
}
// WorkFlowSchema returns a JSON schema for a vai workflow
func WorkFlowSchema() *jsonschema.Schema {
reflector := jsonschema.Reflector{}
reflector.ExpandedStruct = true
schema := reflector.Reflect(&Workflow{})
schema.PatternProperties = map[string]*jsonschema.Schema{
TaskNamePattern.String(): {
Ref: "#/$defs/Task",
Description: "Name of the task",
},
}
schema.ID = "https://raw.githubusercontent.com/Noxsios/vai/main/vai.schema.json"
schema.AdditionalProperties = jsonschema.FalseSchema
return schema
}