-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
58 lines (45 loc) · 1023 Bytes
/
yaml.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
package main
import (
"fmt"
"os"
"gopkg.in/yaml.v2"
)
func parseYAML(path string) (*DataSet, error) {
schema := make(map[interface{}]interface{})
yml, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if err = yaml.UnmarshalStrict(yml, &schema); err != nil {
return nil, err
}
tables, _ := schema["tables"].([]interface{})
dataset := DataSet{
Tables: make([]Table, 0, len(tables)),
}
for _, table := range tables {
t, _ := table.(map[interface{}]interface{})
name := t["name"]
records, _ := t["records"].([]interface{})
for _, record := range records {
r, _ := record.(map[interface{}]interface{})
fields := make([]Field, len(r))
var fname, fvalue string
l := 0
for k, v := range r {
fname, _ = k.(string)
fvalue = fmt.Sprintf("%v", v)
fields[l] = Field{
Name: fname,
Value: fvalue,
}
l++
}
dataset.Tables = append(dataset.Tables, Table{
Name: name.(string),
Fields: fields,
})
}
}
return &dataset, nil
}