-
Notifications
You must be signed in to change notification settings - Fork 3
/
common_handlers.go
485 lines (425 loc) · 9.2 KB
/
common_handlers.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package cwl
import (
"fmt"
"github.com/commondream/yamlast"
"strings"
)
func (l *loader) MappingToDocument(n node) (Document, error) {
graphNodes, ok := findValue(n, "$graph")
if ok {
if graphNodes.Kind != yamlast.SequenceNode {
return nil, errf("$graph must be a list of objects")
}
graph := Graph{}
err := l.load(n, &graph)
if err != nil {
return nil, err
}
return graph, nil
}
class := findKey(n, "class")
switch strings.ToLower(class) {
case "commandlinetool":
t := &Tool{}
if err := l.load(n, t); err != nil {
return nil, err
}
return t, nil
case "workflow":
wf := &Workflow{}
if err := l.load(n, wf); err != nil {
return nil, err
}
return wf, nil
case "expressiontool":
t := &ExpressionTool{}
if err := l.load(n, t); err != nil {
return nil, err
}
return t, nil
default:
return nil, fmt.Errorf("unknown document class: '%s'", class)
}
}
func (l *loader) ScalarToDocument(n node) (Document, error) {
if _, ok := l.resolver.(noResolver); ok {
return DocumentRef{Location: n.Value}, nil
}
b, base, err := l.resolver.Resolve(l.base, n.Value)
if err != nil {
return nil, fmt.Errorf("failed to resolve document: %s", err)
}
return LoadDocumentBytes(b, base, l.resolver)
}
func (l *loader) ScalarToExpressionSlice(n node) ([]Expression, error) {
return []Expression{Expression(n.Value)}, nil
}
func (l *loader) MappingToExpressionMap(n node) (map[string]Expression, error) {
out := map[string]Expression{}
for _, kv := range itermap(n) {
k := kv.k
v := kv.v
expr := Expression("")
err := l.load(v, &expr)
if err != nil {
return nil, errf("loading expression map: %s", err)
}
out[k] = expr
}
return out, nil
}
func (l *loader) SeqToExpressionMap(n node) (map[string]Expression, error) {
out := map[string]Expression{}
for _, c := range n.Children {
type envdef struct {
Name string `json:"envName"`
Value Expression `json:"envValue"`
}
item := envdef{}
err := l.load(c, &item)
if err != nil {
return nil, errf("loading expression map: %s", err)
}
out[item.Name] = item.Value
}
return out, nil
}
func (l *loader) SeqToStringSlice(n node) ([]string, error) {
strs := []string{}
for _, c := range n.Children {
strs = append(strs, c.Value)
}
return strs, nil
}
func (l *loader) SeqToCommandLineBindingSlice(n node) ([]CommandLineBinding, error) {
var b []CommandLineBinding
for _, c := range n.Children {
if c.Kind != yamlast.MappingNode {
return nil, fmt.Errorf("unhandled command line binding type")
}
clb := CommandLineBinding{}
err := l.load(c, &clb)
if err != nil {
return nil, err
}
b = append(b, clb)
}
return b, nil
}
func (l *loader) SeqToString(n node) (string, error) {
s := ""
for _, c := range n.Children {
if c.Kind != yamlast.ScalarNode {
return "", fmt.Errorf("unhandled string concat type")
}
if s != "" {
s += "\n" + c.Value
} else {
s = c.Value
}
}
return s, nil
}
func (l *loader) MappingToInputFieldSlice(n node) ([]InputField, error) {
var fields []InputField
for _, kv := range itermap(n) {
k := kv.k
v := kv.v
i := InputField{Name: k}
err := l.load(v, &i)
if err != nil {
return nil, err
}
fields = append(fields, i)
}
return fields, nil
}
/* Type loading is pretty complex.... */
func (l *loader) MappingToInputTypeSlice(n node) ([]InputType, error) {
typeVal, ok := findValue(n, "type")
if !ok {
return nil, fmt.Errorf("missing input type")
}
n = transformTypeNode(n)
var t []InputType
err := l.load(typeVal, &t)
if err != nil {
return nil, err
}
if len(t) == 1 {
switch z := t[0].(type) {
case InputArray:
err := l.load(n, &z)
if err != nil {
return nil, err
}
t[0] = z
case InputEnum:
err := l.load(n, &z)
if err != nil {
return nil, err
}
t[0] = z
case InputRecord:
err := l.load(n, &z)
if err != nil {
return nil, err
}
t[0] = z
}
}
return t, err
}
func (l *loader) MappingToOutputTypeSlice(n node) ([]OutputType, error) {
typeVal, ok := findValue(n, "type")
if !ok {
return nil, fmt.Errorf("missing output type")
}
n = transformTypeNode(n)
var t []OutputType
err := l.load(typeVal, &t)
if err != nil {
return nil, err
}
if len(t) == 1 {
switch z := t[0].(type) {
case OutputArray:
err := l.load(n, &z)
if err != nil {
return nil, err
}
t[0] = z
case OutputEnum:
err := l.load(n, &z)
if err != nil {
return nil, err
}
t[0] = z
case OutputRecord:
err := l.load(n, &z)
if err != nil {
return nil, err
}
t[0] = z
}
}
return t, err
}
func (l *loader) ScalarToInputTypeSlice(n node) ([]InputType, error) {
n = transformTypeNode(n)
if n.Kind != yamlast.ScalarNode {
var out []InputType
err := l.load(n, &out)
if err != nil {
return nil, err
}
return out, nil
}
t := l.scalarToType(n.Value, true)
if t == nil {
return nil, fmt.Errorf("unknown input type: %s", n.Value)
}
ot, ok := t.(InputType)
if !ok {
return nil, fmt.Errorf("invalid input type: %s", n.Value)
}
return []InputType{ot}, nil
}
func (l *loader) ScalarToOutputTypeSlice(n node) ([]OutputType, error) {
n = transformTypeNode(n)
if n.Kind != yamlast.ScalarNode {
var out []OutputType
err := l.load(n, &out)
if err != nil {
return nil, err
}
return out, nil
}
t := l.scalarToType(n.Value, false)
if t == nil {
return nil, fmt.Errorf("unknown output type: %s", n.Value)
}
ot, ok := t.(OutputType)
if !ok {
return nil, fmt.Errorf("invalid output type: %s", n.Value)
}
return []OutputType{ot}, nil
}
func (l *loader) scalarToType(name string, isInput bool) cwltype {
var t cwltype
switch strings.ToLower(name) {
case "":
return nil
case "any":
t = Any{}
case "null":
t = Null{}
case "boolean":
t = Boolean{}
case "int":
t = Int{}
case "float":
t = Float{}
case "long":
t = Long{}
case "double":
t = Double{}
case "string":
t = String{}
case "file":
t = FileType{}
case "directory":
t = DirectoryType{}
case "stdout":
t = Stdout{}
case "stderr":
t = Stderr{}
case "record":
if isInput {
t = InputRecord{}
} else {
t = OutputRecord{}
}
case "enum":
if isInput {
t = InputEnum{}
} else {
t = OutputEnum{}
}
case "array":
if isInput {
t = InputArray{}
} else {
t = OutputArray{}
}
default:
// TODO possibly only create TypeRef for types staring with "#" or otherwise
// looking like IRI/URI format?
return TypeRef{name}
}
return t
}
func (l *loader) MappingToSchemaDef(n node) (SchemaDef, error) {
typeVal, ok := findValue(n, "type")
if !ok {
return SchemaDef{}, fmt.Errorf("missing type for schema def")
}
name := findKey(n, "name")
if name == "" {
return SchemaDef{}, fmt.Errorf("missing name for schema def")
}
// TODO support output types?
switch typeVal.Value {
case "record":
t := InputRecord{}
err := l.load(n, &t)
return SchemaDef{Name: name, Type: t}, err
case "array":
t := InputArray{}
err := l.load(n, &t)
return SchemaDef{Name: name, Type: t}, err
case "enum":
t := InputEnum{}
err := l.load(n, &t)
return SchemaDef{Name: name, Type: t}, err
default:
return SchemaDef{}, fmt.Errorf("unknown schema type: %s", typeVal.Value)
}
}
/* These are here to avoid the automatic loading of slice types in the loader */
func (l *loader) SeqToInputTypeSlice(n node) ([]InputType, error) {
var out []InputType
for _, c := range n.Children {
var t []InputType
err := l.load(c, &t)
if err != nil {
return nil, err
}
out = append(out, t...)
}
return out, nil
}
func (l *loader) SeqToOutputTypeSlice(n node) ([]OutputType, error) {
var out []OutputType
for _, c := range n.Children {
var t []OutputType
err := l.load(c, &t)
if err != nil {
return nil, err
}
out = append(out, t...)
}
return out, nil
}
// transformTypeNode handles type name transformations such as "string[]", "string?", etc.
// http://www.commonwl.org/v1.0/Workflow.html#Document_preprocessing
func transformTypeNode(n node) node {
if n.Kind != yamlast.ScalarNode {
return n
}
//name := strings.ToLower(n.Value)
name := strings.TrimSpace(n.Value)
isNullable := false
isArray := false
if strings.HasSuffix(name, "?") {
name = strings.TrimSuffix(name, "?")
isNullable = true
}
if strings.HasSuffix(name, "[]") {
name = strings.TrimSuffix(name, "[]")
isArray = true
}
n.Value = strings.TrimSpace(name)
// Copy input node
out := &yamlast.Node{
Kind: n.Kind,
Line: n.Line,
Column: n.Column,
Value: n.Value,
}
if isArray {
out = &yamlast.Node{
Kind: yamlast.MappingNode,
Line: n.Line,
Column: n.Column,
Children: []*yamlast.Node{
{
Kind: yamlast.ScalarNode,
Line: n.Line,
Column: n.Column,
Value: "type",
},
{
Kind: yamlast.ScalarNode,
Line: n.Line,
Column: n.Column,
Value: "array",
},
{
Kind: yamlast.ScalarNode,
Line: n.Line,
Column: n.Column,
Value: "items",
},
out,
},
}
}
if isNullable {
out = &yamlast.Node{
Kind: yamlast.SequenceNode,
Line: n.Line,
Column: n.Column,
Children: []*yamlast.Node{
out,
{
Kind: yamlast.ScalarNode,
Line: n.Line,
Column: n.Column,
Value: "null",
},
},
}
}
return node(out)
}