-
Notifications
You must be signed in to change notification settings - Fork 2
/
yaml.go
50 lines (39 loc) · 1.01 KB
/
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
package loader
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
func (fx FixtureLoader) getDataFromYAML(file string) (Data, error) {
f, err := ioutil.ReadFile(file)
if err != nil {
return Data{}, err
}
var yamlData interface{}
yaml.Unmarshal(f, &yamlData)
rows := make([]map[string]string, 0)
for _, d := range yamlData.([]interface{}) {
row := interfaceInterfaceToMapString(d.(map[interface{}]interface{}))
rows = append(rows, row)
}
if len(rows) < 1 {
return Data{}, fmt.Errorf("[error] %s is data empty", file)
}
columns := make([]string, 0)
for key := range rows[0] {
columns = append(columns, key)
}
data := Data{
columns: columns,
rows: rows,
}
return data, nil
}
// onply support testfixtures yaml type refs: https://github.com/go-testfixtures/testfixtures#usage
func interfaceInterfaceToMapString(d map[interface{}]interface{}) map[string]string {
row := make(map[string]string, len(d))
for key, value := range d {
row[fmt.Sprint(key)] = fmt.Sprint(value)
}
return row
}