-
Notifications
You must be signed in to change notification settings - Fork 0
/
formats.go
89 lines (69 loc) · 2.14 KB
/
formats.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
package pantry
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
// UnsupportedConfigError denotes encountering an unsupported
// configuration filetype.
type UnsupportedConfigError string
// Returns the formatted configuration error.
func (e UnsupportedConfigError) Error() string {
return fmt.Sprintf("Unsupported Config Type %q", string(e))
}
// ConfigParseError denotes failing to parse configuration file.
type ConfigParseError struct {
err error
}
// Returns the formatted configuration error.
func (e ConfigParseError) Error() string {
return fmt.Sprintf("Parsing config failed: %s", e.err.Error())
}
// ConfigEncodeError denotes failing to encode configuration
type ConfigEncodeError struct {
err error
}
// Returns the formatted configuration error.
func (e ConfigEncodeError) Error() string {
return fmt.Sprintf("Encoding config failed: %s", e.err.Error())
}
// MarshalFunc is any marshaler.
type MarshalFunc func(v interface{}) ([]byte, error)
// UnmarshalFunc is any unmarshaler.
type UnmarshalFunc func(data []byte, v interface{}) error
type ConfigFormat struct {
Marshal MarshalFunc
Unmarshal UnmarshalFunc
}
type ConfigFormats map[string]*ConfigFormat
// Register format marshaler and unmarphaler
func (c ConfigFormats) Register(format string, m MarshalFunc, um UnmarshalFunc) {
c[strings.ToLower(format)] = &ConfigFormat{m, um}
}
func (c ConfigFormats) Search(format string) (*ConfigFormat, error) {
format = strings.ToLower(format)
f, ok := c[format]
if !ok {
if f, ok = c[ext(format)]; !ok {
return f, UnsupportedConfigError(format)
}
}
return f, nil
}
// Formats contains marshalers and unmarshalers for different file formats
var Formats = ConfigFormats{}
func init() {
Formats.Register("json", func(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}, json.Unmarshal)
Formats.Register("yaml", yaml.Marshal, yaml.Unmarshal)
Formats.Register("yml", yaml.Marshal, yaml.Unmarshal)
Formats.Register("toml", func(v interface{}) ([]byte, error) {
b := bytes.Buffer{}
err := toml.NewEncoder(&b).Encode(v)
return b.Bytes(), err
}, toml.Unmarshal)
}