forked from lijiang2014/cwl.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
126 lines (114 loc) · 2.33 KB
/
root.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
package cwl
import (
"encoding/json"
"fmt"
//"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
"github.com/robertkrimen/otto"
)
// NewCWL ...
func NewCWL() *Root {
root := new(Root)
return root
}
type SaladRootDoc struct {
Graph Graphs `json:"$graph"`
}
// Root ...
type Root struct {
SaladRootDoc `json:",inline"`
Process Process
// Use $graphs 组合
Class string `json:"class"`
// Path
Path string `json:"-"`
// InputsVM
InputsVM *otto.Otto
}
// UnmarshalJSON ...
func (root *Root) UnmarshalJSON(b []byte) error {
p, err := ParseCWLProcess(b)
if err != nil {
return err
}
root.Process = p
if c, ok := p.(Classable); ok {
root.Class = c.ClassName()
}
return nil
}
// Decode decodes specified file to this root
func (root *Root) Decode(r io.Reader) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("Parse error: %v", e)
}
}()
buf, err := ioutil.ReadAll(r)
if err != nil {
return err
}
buf2, err := Y2J(buf)
if err != nil {
return err
}
if err = json.Unmarshal(buf2, root); err != nil {
return err
}
return nil
}
//// AsStep constructs Root as a step of "steps" from interface.
//func (root *Root) AsStep(i interface{}) *Root {
// dest := new(Root)
// switch x := i.(type) {
// case string:
// dest.ID = x
// case map[string]interface{}:
// err := dest.UnmarshalMap(x)
// if err != nil {
// panic(fmt.Sprintf("Failed to parse step as CWL.Root: %v", err))
// }
// }
// return dest
//}
// Y2J converts yaml to json.
func Y2J(in []byte) ([]byte, error) {
result := []byte{}
var root interface{}
if err := yaml.Unmarshal(in, &root); err != nil {
return result, err
}
return json.Marshal(convert(root))
}
// J2Y converts json to yaml.
func J2Y(r io.Reader) ([]byte, error) {
result := []byte{}
b, err := ioutil.ReadAll(r)
if err != nil {
return result, err
}
var root interface{}
if err := json.Unmarshal(b, &root); err != nil {
return result, err
}
return yaml.Marshal(convert(root))
}
// convert ...
func convert(parent interface{}) interface{} {
switch entity := parent.(type) {
case map[interface{}]interface{}:
node := map[string]interface{}{}
for key, val := range entity {
node[key.(string)] = convert(val)
}
return node
case []interface{}:
for idx, val := range entity {
entity[idx] = convert(val)
}
return entity
}
return parent
}