-
Notifications
You must be signed in to change notification settings - Fork 11
/
tablib_yaml.go
69 lines (59 loc) · 1.59 KB
/
tablib_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
59
60
61
62
63
64
65
66
67
68
69
package tablib
import "gopkg.in/yaml.v2"
// LoadYAML loads a dataset from a YAML source.
func LoadYAML(yamlContent []byte) (*Dataset, error) {
var input []map[string]interface{}
if err := yaml.Unmarshal(yamlContent, &input); err != nil {
return nil, err
}
return internalLoadFromDict(input)
}
// LoadDatabookYAML loads a Databook from a YAML source.
func LoadDatabookYAML(yamlContent []byte) (*Databook, error) {
var input []map[string]interface{}
var internalInput []map[string]interface{}
if err := yaml.Unmarshal(yamlContent, &input); err != nil {
return nil, err
}
db := NewDatabook()
for _, d := range input {
b, err := yaml.Marshal(d["data"])
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(b, &internalInput); err != nil {
return nil, err
}
if ds, err := internalLoadFromDict(internalInput); err == nil {
db.AddSheet(d["title"].(string), ds)
} else {
return nil, err
}
}
return db, nil
}
// YAML returns a YAML representation of the Dataset as an Exportable.
func (d *Dataset) YAML() (*Exportable, error) {
back := d.Dict()
b, err := yaml.Marshal(back)
if err != nil {
return nil, err
}
return newExportableFromBytes(b), nil
}
// YAML returns a YAML representation of the Databook as an Exportable.
func (d *Databook) YAML() (*Exportable, error) {
y := make([]map[string]interface{}, len(d.sheets))
i := 0
for _, s := range d.sheets {
y[i] = make(map[string]interface{})
y[i]["title"] = s.title
y[i]["data"] = s.dataset.Dict()
i++
}
b, err := yaml.Marshal(y)
if err != nil {
return nil, err
}
return newExportableFromBytes(b), nil
}