forked from otiai10/cwl.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_input.go
97 lines (89 loc) · 2.09 KB
/
step_input.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
package cwl
// StepInput represents WorkflowStepInput.
// @see http://www.commonwl.org/v1.0/Workflow.html#WorkflowStepInput
type StepInput struct {
ID string
Source []string
LinkMerge string
Default *InputDefault
ValueFrom string
}
// New constructs a StepInput struct from any interface.
func (_ StepInput) New(i interface{}) StepInput {
dest := StepInput{}
switch x := i.(type) {
case map[string]interface{}:
for key, v := range x {
if dest.ID == "" {
dest.ID = key
}
if key == "id" {
dest.ID = v.(string)
} else {
switch e := v.(type) {
case string:
dest.Source = []string{e}
case []interface{}:
for _, s := range e {
dest.Source = append(dest.Source, s.(string))
}
case map[string]interface{}:
for key, v := range e {
switch key {
case "id":
dest.ID = v.(string)
case "source":
if list, ok := v.([]interface{}); ok {
for _, s := range list {
dest.Source = append(dest.Source, s.(string))
}
} else {
dest.Source = append(dest.Source, v.(string))
}
case "linkMerge":
dest.LinkMerge = v.(string)
case "default":
dest.Default = InputDefault{}.New(v)
case "valueFrom":
dest.ValueFrom = v.(string)
}
}
}
}
}
}
return dest
}
// StepInputs represents []StepInput
type StepInputs []StepInput
// NewList constructs a list of StepInput from interface.
func (_ StepInput) NewList(i interface{}) StepInputs {
dest := StepInputs{}
switch x := i.(type) {
case []interface{}:
for _, v := range x {
dest = append(dest, StepInput{}.New(v))
}
case map[string]interface{}:
for key, v := range x {
item := make(map[string]interface{})
item[key] = v
dest = append(dest, StepInput{}.New(item))
}
default:
dest = append(dest, StepInput{}.New(x))
}
return dest
}
// Len for sorting
func (s StepInputs) Len() int {
return len(s)
}
// Less for sorting
func (s StepInputs) Less(i, j int) bool {
return s[i].ID < s[j].ID
}
// Swap for sorting
func (s StepInputs) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}