-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_loader.go
133 lines (117 loc) · 2.7 KB
/
file_loader.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
package main
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"gopkg.in/yaml.v2"
)
const (
gzipExt = ".gz"
)
var availableExts = [...]string{".json", ".yml", ".yaml"}
var mappings = map[string]string{
".yaml": ".yml",
}
type FileLoader func(string) (map[string]interface{}, error)
func isAvailableExt(ext string) bool {
for _, e := range availableExts {
if e == ext {
return true
}
}
return false
}
func uncompressData(data []byte) ([]byte, error) {
reader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
return ioutil.ReadAll(reader)
}
func GetNormalizedExtension(path string) string {
ext := filepath.Ext(path)
if ext == gzipExt {
ext = filepath.Ext(strings.TrimSuffix(path, ext))
}
if mapping, ok := mappings[ext]; ok {
ext = mapping
}
return ext
}
func MakeLoader(loader func([]byte, interface{}) error) FileLoader {
return func(path string) (output map[string]interface{}, _ error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return output, err
}
if filepath.Ext(path) == gzipExt {
if data, err = uncompressData(data); err != nil {
return output, err
}
}
err = loader(data, &output)
return output, err
}
}
func MakeLoaderFor(path string) (FileLoader, error) {
ext := GetNormalizedExtension(path)
switch ext {
case ".json":
return MakeLoader(json.Unmarshal), nil
case ".yml":
return MakeLoader(yaml.Unmarshal), nil
default:
return nil, fmt.Errorf("no loader available for file with extension %s", ext)
}
}
func LoadFile(path string) ([]Fixture, error) {
load, err := MakeLoaderFor(path)
if err != nil {
return nil, err
}
content, err := load(path)
if err != nil {
return nil, err
}
return MakeFixtures(content)
}
func getFileNames(files []os.FileInfo) (names []string) {
for _, file := range files {
names = append(names, file.Name())
}
sort.Strings(names)
return names
}
func LoadDirectory(directory string) (fixtures []Fixture, err error) {
files, err := ioutil.ReadDir(directory)
if err != nil {
return nil, fmt.Errorf("could not read directory %s", directory)
}
fileNames := getFileNames(files)
for _, fileName := range fileNames {
if isAvailableExt(GetNormalizedExtension(fileName)) {
content, err := LoadFile(filepath.Join(directory, fileName))
if err != nil {
return nil, err
}
fixtures = append(fixtures, content...)
}
}
return fixtures, nil
}
func LoadDirectories(directories []string) (fixtures []Fixture, err error) {
for _, directory := range directories {
results, err := LoadDirectory(directory)
if err != nil {
return nil, err
}
fixtures = append(fixtures, results...)
}
return fixtures, nil
}