-
Notifications
You must be signed in to change notification settings - Fork 21
/
jobinput.go
279 lines (211 loc) · 6.39 KB
/
jobinput.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
package cwl
import (
//"errors"
"fmt"
"github.com/davecgh/go-spew/spew"
//"github.com/mitchellh/mapstructure"
"io/ioutil"
//"os"
//"strings"
"reflect"
"github.com/MG-RAST/AWE/lib/logger"
"gopkg.in/yaml.v2"
//"github.com/MG-RAST/AWE/lib/logger/event"
)
//type Job_document map[string]interface{}
type Job_document []NamedCWLType // JobDocArray
type JobDocMap map[string]CWLType
type NamedCWLType struct {
CWL_id_Impl `yaml:",inline" bson:",inline" json:",inline" mapstructure:",squash"` // provides id
Value CWLType `yaml:"value,omitempty" bson:"value,omitempty" json:"value,omitempty" mapstructure:"value,omitempty"`
}
func NewNamedCWLType(id string, value CWLType) NamedCWLType {
x := NamedCWLType{Value: value}
x.Id = id
return x
}
func (jd_map JobDocMap) GetArray() (result Job_document, err error) {
result = Job_document{}
for key, value := range jd_map {
if value == nil {
err = fmt.Errorf("(GetArray) value of key %s is nil", key)
return
}
named := NewNamedCWLType(key, value)
result = append(result, named)
}
return
}
func NewNamedCWLTypeFromInterface(native interface{}) (cwl_obj_named NamedCWLType, err error) {
native, err = MakeStringMap(native)
if err != nil {
return
}
switch native.(type) {
case map[string]interface{}:
named_thing_map := native.(map[string]interface{})
named_thing_value, ok := named_thing_map["value"]
if !ok {
fmt.Println("Expected NamedCWLType field \"value\" , but not found:")
spew.Dump(native)
err = fmt.Errorf("(NewNamedCWLTypeFromInterface) Expected NamedCWLType field \"value\" , but not found: %s", spew.Sdump(native))
return
}
var id string
var id_interface interface{}
id_interface, ok = named_thing_map["id"]
if ok {
id, ok = id_interface.(string)
if !ok {
err = fmt.Errorf("(NewNamedCWLTypeFromInterface) id has wrong type")
return
}
} else {
id = ""
}
var cwl_obj CWLType
cwl_obj, err = NewCWLType(id, named_thing_value)
if err != nil {
err = fmt.Errorf("(NewNamedCWLTypeFromInterface) B NewCWLType returns: %s", err.Error())
return
}
//var cwl_obj_named NamedCWLType
cwl_obj_named = NewNamedCWLType(id, cwl_obj)
default:
fmt.Println("Expected a NamedCWLType, but did not find a map:")
spew.Dump(native)
err = fmt.Errorf("(NewNamedCWLTypeFromInterface) Expected a NamedCWLType, but did not find a map")
return
}
return
}
func (job_input *Job_document) GetMap() (job_input_map JobDocMap) {
job_input_map = make(JobDocMap)
for _, value := range *job_input {
//id := value.GetId()
job_input_map[value.Id] = value.Value
}
return
}
func NewJob_document(original interface{}) (job *Job_document, err error) {
logger.Debug(3, "(NewJob_document) starting")
original, err = MakeStringMap(original)
if err != nil {
return
}
job_nptr := Job_document{}
job = &job_nptr
switch original.(type) {
case map[string]interface{}:
original_map := original.(map[string]interface{})
for key_str, value := range original_map {
cwl_obj, xerr := NewCWLType(key_str, value)
if xerr != nil {
err = fmt.Errorf("(NewJob_document) NewCWLType returned: %s", xerr.Error())
return
}
job_nptr = append(job_nptr, NewNamedCWLType(key_str, cwl_obj))
}
return
case []interface{}:
original_array := original.([]interface{})
for _, value := range original_array {
cwl_obj, xerr := NewCWLType("", value)
if xerr != nil {
err = fmt.Errorf("(NewJob_document) NewCWLType returned: %s", xerr.Error())
return
}
job_nptr = append(job_nptr, NewNamedCWLType("not supported", cwl_obj))
}
return
default:
spew.Dump(original)
err = fmt.Errorf("(NewJob_document) (A) type %s unknown", reflect.TypeOf(original))
}
return
}
func NewJob_documentFromNamedTypes(original interface{}) (job *Job_document, err error) {
logger.Debug(3, "(NewJob_documentFromNamedTypes) starting")
if original == nil {
err = fmt.Errorf("(NewJob_documentFromNamedTypes) original == nil")
return
}
original, err = MakeStringMap(original)
if err != nil {
err = fmt.Errorf("(NewJob_documentFromNamedTypes) MakeStringMap returned: %s", err.Error())
return
}
job_nptr := Job_document{}
job = &job_nptr
switch original.(type) {
case []interface{}:
original_array := original.([]interface{})
for _, named_thing := range original_array {
var cwl_obj_named NamedCWLType
cwl_obj_named, err = NewNamedCWLTypeFromInterface(named_thing)
if err != nil {
err = fmt.Errorf("(NewJob_documentFromNamedTypes) A) NewNamedCWLTypeFromInterface returns: %s", err.Error())
return
}
job_nptr = append(job_nptr, cwl_obj_named)
}
return
default:
err = fmt.Errorf("(NewJob_document) (B) type %s unknown: %s", reflect.TypeOf(original), spew.Sdump(original))
}
return
}
func ParseJobFile(job_file string) (job_input *Job_document, err error) {
job_stream, err := ioutil.ReadFile(job_file)
if err != nil {
return
}
job_gen := map[string]interface{}{}
err = Unmarshal(&job_stream, &job_gen)
if err != nil {
err = fmt.Errorf("(ParseJobFile) Unmarshal returned: %s (json: %s)", err.Error(), string(job_stream[:]))
return
}
job_input, err = NewJob_document(job_gen)
if err != nil {
err = fmt.Errorf("(ParseJobFile) NewJob_document returned: %s", err.Error())
return
}
return
}
func ParseJob(job_byte_ptr *[]byte) (job_input *Job_document, err error) {
// since Unmarshal (json and yaml) cannot unmarshal into interface{}, we try array and map
job_byte := *job_byte_ptr
if job_byte[0] == '-' {
//fmt.Println("yaml list")
// I guess this is a yaml list
//job_array := []CWLType{}
//job_array := []map[string]interface{}{}
job_array := []interface{}{}
err = yaml.Unmarshal(job_byte, &job_array)
if err != nil {
err = fmt.Errorf("(ParseJob) Could, not parse job input as yaml list: %s", err.Error())
return
}
job_input, err = NewJob_document(job_array)
if err != nil {
err = fmt.Errorf("(ParseJob) A NewJob_document returns: %s", err.Error())
}
} else {
//fmt.Println("yaml map")
job_map := make(map[string]interface{})
err = yaml.Unmarshal(job_byte, &job_map)
if err != nil {
err = fmt.Errorf("Could, not parse job input as yaml map: %s", err.Error())
return
}
job_input, err = NewJob_document(job_map)
if err != nil {
err = fmt.Errorf("(ParseJob) B NewJob_document returns: %s", err.Error())
}
}
if err != nil {
return
}
return
}