-
Notifications
You must be signed in to change notification settings - Fork 5
/
filedata.go
160 lines (140 loc) · 3.42 KB
/
filedata.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package conflate
import (
pkgurl "net/url"
"os"
"path/filepath"
"strings"
)
type filedata struct {
url pkgurl.URL
data []byte
obj map[string]interface{}
includes []string
}
var emptyFiledata = filedata{}
type filedatas []filedata
// UnmarshallerFunc defines the type of function used for unmarshalling data
type UnmarshallerFunc func([]byte, interface{}) error
// UnmarshallerFuncs defines the type for a slice of UnmarshallerFunc
type UnmarshallerFuncs []UnmarshallerFunc
// UnmarshallerMap defines the type of a map of string to UnmarshallerFuncs
type UnmarshallerMap map[string]UnmarshallerFuncs
// Unmarshallers is a list of unmarshalling functions to be used for given file extensions. The unmarshaller slice for the blank file extension is used when no match is found.
var Unmarshallers = UnmarshallerMap{
".json": {JSONUnmarshal},
".jsn": {JSONUnmarshal},
".yaml": {YAMLUnmarshal},
".yml": {YAMLUnmarshal},
".toml": {TOMLUnmarshal},
".tml": {TOMLUnmarshal},
"": {JSONUnmarshal, YAMLUnmarshal, TOMLUnmarshal},
}
func newFiledata(data []byte, url pkgurl.URL) (filedata, error) {
fd := filedata{data: data, url: url}
err := fd.unmarshal()
if err != nil {
return emptyFiledata, err
}
err = fd.validate()
if err != nil {
return emptyFiledata, err
}
err = fd.extractIncludes()
if err != nil {
return emptyFiledata, err
}
return fd, nil
}
func newExpandedFiledata(data []byte, url pkgurl.URL) (filedata, error) {
return newFiledata(recursiveExpand(data), url)
}
func (fd *filedata) wrapError(err error) error {
if fd.url == emptyURL {
return err
}
return wrapError(err, "Error processing %v", fd.url.String())
}
func (fd *filedata) validate() error {
return fd.wrapError(validate(fd.obj, getSchema()))
}
func (fd *filedata) unmarshal() error {
ext := strings.ToLower(filepath.Ext(fd.url.Path))
unmarshallers, ok := Unmarshallers[ext]
if !ok {
unmarshallers = Unmarshallers[""]
}
err := makeError("Could not unmarshal data")
for _, unmarshal := range unmarshallers {
uerr := unmarshal(fd.data, &fd.obj)
if uerr == nil {
return nil
}
err = wrapError(uerr, err.Error())
}
return err
}
func (fd *filedata) extractIncludes() error {
if Includes == "" {
return nil
}
err := jsonMarshalUnmarshal(fd.obj[Includes], &fd.includes)
if err != nil {
return wrapError(err, "Could not extract includes")
}
delete(fd.obj, Includes)
return nil
}
func (fds filedatas) objs() []interface{} {
var objs []interface{}
for _, fd := range fds {
objs = append(objs, fd.obj)
}
return objs
}
func (fd *filedata) isEmpty() bool {
return fd == nil || fd.obj == nil
}
func recursiveExpand(b []byte) []byte {
const maxExpansions = 10
var c int
for i := 0; i < maxExpansions; i++ {
b, c = expand(b)
if c == 0 {
return b
}
}
return b
}
func expand(b []byte) ([]byte, int) {
var c int
return []byte(os.Expand(string(b),
func(name string) string {
val, ok := os.LookupEnv(name)
if ok {
c++
return val
}
return "$" + name
})), c
}
var getSchema = getDefaultSchema
func getDefaultSchema() map[string]interface{} {
return map[string]interface{}{
"anyOf": []interface{}{
map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
Includes: map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "string",
},
},
},
},
map[string]interface{}{
"type": "null",
},
},
}
}