-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
43 lines (40 loc) · 807 Bytes
/
parse.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
package configer
import (
"errors"
"path/filepath"
"strings"
)
var (
parseMap = map[string]func(data []byte, v interface{}) error{
"json": jsonUnmarshal,
"xml": xmlUnmarshal,
"yaml": yamlUnmarshal,
"yml": yamlUnmarshal,
"toml": tomlUnmarshal,
"ini": iniUnmarshal,
"hcl": hclUnmarshal,
}
parseList = []func(data []byte, v interface{}) error{
jsonUnmarshal,
hclUnmarshal,
xmlUnmarshal,
tomlUnmarshal,
yamlUnmarshal,
iniUnmarshal,
}
)
func Parse(data []byte, cfgpath string, val interface{}) (err error) {
ext := strings.TrimPrefix(filepath.Ext(cfgpath), ".")
pf := parseMap[ext]
if pf != nil {
return pf(data, val)
}
for _, v := range parseList {
err = v(data, val)
if err != nil {
continue
}
return
}
return errors.New("Failed to decode config")
}