-
Notifications
You must be signed in to change notification settings - Fork 21
/
collection.go
141 lines (117 loc) · 3.22 KB
/
collection.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
package cwl
import (
"bytes"
"fmt"
"github.com/MG-RAST/AWE/lib/logger"
"regexp"
"strings"
)
type CWL_collection struct {
Workflows map[string]*Workflow
WorkflowStepInputs map[string]*WorkflowStepInput
CommandLineTools map[string]*CommandLineTool
Files map[string]*File
Strings map[string]*String
Ints map[string]*Int
All map[string]*CWL_object
}
func (c CWL_collection) Evaluate(raw string) (parsed string) {
reg := regexp.MustCompile(`\$\([\w.]+\)`) // https://github.com/google/re2/wiki/Syntax
parsed = raw
for {
matches := reg.FindAll([]byte(parsed), -1)
fmt.Printf("Matches: %d\n", len(matches))
if len(matches) == 0 {
return parsed
}
for _, match := range matches {
key := bytes.TrimPrefix(match, []byte("$("))
key = bytes.TrimSuffix(key, []byte(")"))
// trimming of inputs. is only a work-around
key = bytes.TrimPrefix(key, []byte("inputs."))
value_str := ""
value, err := c.GetString(string(key))
if err != nil {
value_str = "<ERROR_NOT_FOUND:" + string(key) + ">"
} else {
value_str = value.String()
}
logger.Debug(1, "evaluate %s -> %s\n", key, value_str)
parsed = strings.Replace(parsed, string(match), value_str, 1)
}
}
}
func (c CWL_collection) Add(obj CWL_object) (err error) {
id := obj.GetId()
if id == "" {
err = fmt.Errorf("key is empty")
return
}
switch obj.GetClass() {
case "Workflow":
c.Workflows[id] = obj.(*Workflow)
case "WorkflowStepInput":
c.WorkflowStepInputs[id] = obj.(*WorkflowStepInput)
case "CommandLineTool":
c.CommandLineTools[id] = obj.(*CommandLineTool)
case "File":
c.Files[id] = obj.(*File)
case "String":
c.Strings[id] = obj.(*String)
case "Int":
obj_int, ok := obj.(*Int)
if !ok {
err = fmt.Errorf("could not make Int type assertion")
return
}
c.Ints[id] = obj_int
}
c.All[id] = &obj
return
}
func (c CWL_collection) Get(id string) (obj *CWL_object, err error) {
obj, ok := c.All[id]
if !ok {
err = fmt.Errorf("item %s not found in collection", id)
}
return
}
func (c CWL_collection) GetFile(id string) (obj *File, err error) {
obj, ok := c.Files[id]
if !ok {
err = fmt.Errorf("item %s not found in collection", id)
}
return
}
func (c CWL_collection) GetString(id string) (obj *String, err error) {
obj, ok := c.Strings[id]
if !ok {
err = fmt.Errorf("item %s not found in collection", id)
}
return
}
func (c CWL_collection) GetInt(id string) (obj *Int, err error) {
obj, ok := c.Ints[id]
if !ok {
err = fmt.Errorf("item %s not found in collection", id)
}
return
}
func (c CWL_collection) GetWorkflowStepInput(id string) (obj *WorkflowStepInput, err error) {
obj, ok := c.WorkflowStepInputs[id]
if !ok {
err = fmt.Errorf("item %s not found in collection", id)
}
return
}
func NewCWL_collection() (collection CWL_collection) {
collection = CWL_collection{}
collection.Workflows = make(map[string]*Workflow)
collection.WorkflowStepInputs = make(map[string]*WorkflowStepInput)
collection.CommandLineTools = make(map[string]*CommandLineTool)
collection.Files = make(map[string]*File)
collection.Strings = make(map[string]*String)
collection.Ints = make(map[string]*Int)
collection.All = make(map[string]*CWL_object)
return
}