-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinput_default.go
90 lines (83 loc) · 1.9 KB
/
input_default.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
package cwl
import (
"fmt"
"io/ioutil"
"reflect"
)
// InputDefault represents "default" field in an element of "inputs".
type InputDefault struct {
ID string
Self interface{}
Kind reflect.Kind
Entry *Entry
Error error
// TODO: Refactor
Int int
}
// New constructs new "InputDefault".
func (d InputDefault) New(i interface{}) *InputDefault {
dest := &InputDefault{Self: i, Kind: reflect.TypeOf(i).Kind()}
switch v := i.(type) {
case nil:
return dest // do nothing
case int:
dest.Int = v
case map[string]interface{}: // It's "File" in most cases
dest.Entry, dest.Error = dest.EntryFromDictionary(v)
}
return dest
}
// Flatten ...
func (d *InputDefault) Flatten(binding *Binding) []string {
flattened := []string{}
if d.Entry != nil {
flattened = append(flattened, d.Entry.Location)
}
if binding != nil && binding.Prefix != "" {
flattened = append([]string{binding.Prefix}, flattened...)
}
return flattened
}
// EntryFromDictionary ...
func (d *InputDefault) EntryFromDictionary(dict map[string]interface{}) (*Entry, error) {
if dict == nil {
return nil, nil
}
if dict["class"] == nil {
return nil, nil
}
class := dict["class"].(string)
location := dict["location"]
contents := dict["contents"]
if class == "" && location == nil && contents == nil {
return nil, nil
}
switch class {
case "File":
// Use location if specified
if location != nil {
return &Entry{
Class: class,
Location: fmt.Sprintf("%v", location),
File: File{},
}, nil
}
// Use contents if specified
if contentsstring, ok := contents.(string); ok {
tmpfile, err := ioutil.TempFile("/tmp", d.ID)
if err != nil {
return nil, err
}
defer tmpfile.Close()
if _, err := tmpfile.WriteString(contentsstring); err != nil {
return nil, err
}
return &Entry{
Class: class,
Location: tmpfile.Name(),
File: File{},
}, nil
}
}
return nil, nil
}