/
graph.go
193 lines (177 loc) · 4.43 KB
/
graph.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
package gripql
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"google.golang.org/protobuf/encoding/protojson"
"sigs.k8s.io/yaml"
)
// ParseYAMLGraph parses a YAML doc into the given Graph instance.
func ParseYAMLGraph(raw []byte) (*Graph, error) {
tmp := map[string]interface{}{}
err := yaml.Unmarshal(raw, &tmp)
if err != nil {
return nil, err
}
part, err := json.Marshal(tmp)
if err != nil {
return nil, err
}
g := &Graph{}
err = protojson.Unmarshal(part, g)
if err != nil {
return nil, err
}
return g, nil
}
func ParseYAMLGraphPath(relpath string) (*Graph, error) {
// Try to get absolute path. If it fails, fall back to relative path.
path, err := filepath.Abs(relpath)
if err != nil {
path = relpath
}
// Read file
source, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read graph at path %s: \n%v", path, err)
}
return ParseYAMLGraph(source)
}
// ParseYAMLGraph parses a YAML doc into the given Graph instance.
func ParseYAMLGraphs(raw []byte) ([]*Graph, error) {
graphs := []*Graph{}
tmp := []interface{}{}
err := yaml.Unmarshal(raw, &tmp)
if err != nil {
var tmp2 interface{}
err := yaml.Unmarshal(raw, &tmp2)
if err != nil {
return nil, err
}
tmp = append(tmp, tmp2)
}
for _, s := range tmp {
part, err := json.Marshal(s)
if err != nil {
return nil, err
}
g := &Graph{}
err = protojson.Unmarshal(part, g)
if err != nil {
return nil, err
}
if g.Graph == "" {
return nil, fmt.Errorf("missing graph name")
}
graphs = append(graphs, g)
}
return graphs, nil
}
// ParseJSONGraph parses a JSON doc into the given Graph instance.
func ParseJSONGraphs(raw []byte) ([]*Graph, error) {
graphs := []*Graph{}
tmp := []interface{}{}
err := json.Unmarshal(raw, &tmp)
if err != nil {
var tmp2 interface{}
err := json.Unmarshal(raw, &tmp2)
if err != nil {
return nil, err
}
tmp = append(tmp, tmp2)
}
for _, s := range tmp {
part, err := json.Marshal(s)
if err != nil {
return nil, err
}
g := &Graph{}
err = protojson.Unmarshal(part, g)
if err != nil {
return nil, err
}
if g.Graph == "" {
return nil, fmt.Errorf("missing graph name")
}
graphs = append(graphs, g)
}
return graphs, nil
}
// ParseGraphYAMLFile parses a graph file, which is formatted in YAML,
// and returns a slice of graph objects.
func parseGraphFile(relpath string, format string) ([]*Graph, error) {
var graphs []*Graph
var err error
if relpath == "" {
return nil, fmt.Errorf("path is empty")
}
// Try to get absolute path. If it fails, fall back to relative path.
path, err := filepath.Abs(relpath)
if err != nil {
path = relpath
}
// Read file
source, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read graph at path %s: \n%v", path, err)
}
// Parse file contents
switch format {
case "yaml":
graphs, err = ParseYAMLGraphs(source)
case "json":
graphs, err = ParseJSONGraphs(source)
default:
err = fmt.Errorf("unknown file format: %s", format)
}
if err != nil {
return nil, fmt.Errorf("failed to parse graph at path %s: \n%v", path, err)
}
return graphs, nil
}
// ParseYAMLGraphFile parses a graph file, which is formatted in YAML,
// and returns a slice of graph objects.
func ParseYAMLGraphsFile(relpath string) ([]*Graph, error) {
return parseGraphFile(relpath, "yaml")
}
// ParseJSONGraphFile parses a graph file, which is formatted in JSON,
// and returns a slice of graph objects.
func ParseJSONGraphsFile(relpath string) ([]*Graph, error) {
return parseGraphFile(relpath, "json")
}
// GraphToYAMLString returns a graph formatted as a YAML string
func GraphToYAMLString(graph *Graph) (string, error) {
out, err := protojson.Marshal(graph)
if err != nil {
return "", fmt.Errorf("failed to marshal graph: %v", err)
}
sb, err := yaml.JSONToYAML(out)
if err != nil {
return "", fmt.Errorf("failed to marshal graph: %v", err)
}
return string(sb), nil
}
// GraphToJSONString returns a graph formatted as a JSON string
func GraphToJSONString(graph *Graph) (string, error) {
m := protojson.MarshalOptions{
UseEnumNumbers: false,
EmitUnpopulated: false,
Indent: " ",
UseProtoNames: false,
}
txt := m.Format(graph)
return txt, nil
}
func GraphMapToProto(data map[string]interface{}) (*Graph, error) {
part, err := json.Marshal(data)
if err != nil {
return nil, err
}
g := &Graph{}
err = protojson.Unmarshal(part, g)
if err != nil {
return nil, err
}
return g, nil
}