-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
101 lines (91 loc) · 2.25 KB
/
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
package jsons
import (
"fmt"
"io"
"os"
)
// LoadFunc load the input bytes to map[string]interface{}
type LoadFunc func([]byte) (map[string]interface{}, error)
// loader is a configurable loader for specific format files.
type loader struct {
Name Format
Extensions []string
LoadFunc LoadFunc
}
// makeLoader makes a merger who merge the format by converting it to JSON
func newLoader(name Format, extensions []string, fn LoadFunc) *loader {
return &loader{
Name: name,
Extensions: extensions,
LoadFunc: fn,
}
}
// makeLoadFunc makes a merge func who merge the input to
func (l *loader) Load(input interface{}) ([]map[string]interface{}, error) {
if input == nil {
return nil, nil
}
switch v := input.(type) {
case string:
return l.loadFiles([]string{v})
case []string:
return l.loadFiles(v)
case []byte:
return l.loadSlices([][]byte{v})
case [][]byte:
return l.loadSlices(v)
case io.Reader:
return l.loadReaders([]io.Reader{v})
case []io.Reader:
return l.loadReaders(v)
default:
return nil, fmt.Errorf("unsupported input type: %T", input)
}
}
func (l *loader) loadFiles(files []string) ([]map[string]interface{}, error) {
maps := make([]map[string]interface{}, 0, len(files))
for _, file := range files {
m, err := l.loadFile(file)
if err != nil {
return nil, err
}
maps = append(maps, m)
}
return maps, nil
}
func (l *loader) loadReaders(readers []io.Reader) ([]map[string]interface{}, error) {
maps := make([]map[string]interface{}, 0, len(readers))
for _, r := range readers {
m, err := l.loadReader(r)
if err != nil {
return nil, err
}
maps = append(maps, m)
}
return maps, nil
}
func (l *loader) loadSlices(slices [][]byte) ([]map[string]interface{}, error) {
maps := make([]map[string]interface{}, 0, len(slices))
for _, slice := range slices {
m, err := l.LoadFunc(slice)
if err != nil {
return nil, err
}
maps = append(maps, m)
}
return maps, nil
}
func (l *loader) loadFile(file string) (map[string]interface{}, error) {
bs, err := os.ReadFile(file)
if err != nil {
return nil, err
}
return l.LoadFunc(bs)
}
func (l *loader) loadReader(reader io.Reader) (map[string]interface{}, error) {
bs, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
return l.LoadFunc(bs)
}