-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
41 lines (35 loc) · 973 Bytes
/
decoder.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
package conf
import (
"io"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/cockroachdb/errors"
"gopkg.in/yaml.v3"
)
type DecoderFunc func(cfg any, r io.Reader) error
func getDecoder(copts *confOptions, path string) (DecoderFunc, error) {
ext := filepath.Ext(path)
dec, ok := copts.decoders[ext]
if !ok {
return nil, errors.Errorf("no decoder for %s", ext)
}
return dec, nil
}
var DefaultDecoders = map[string]DecoderFunc{
".yaml": YAMLDecoder,
".yml": YAMLDecoder,
".json": JSONDecoder,
".toml": TOMLDecoder,
}
var YAMLDecoder = func(cfg any, r io.Reader) error {
err := yaml.NewDecoder(r).Decode(cfg)
return errors.Wrap(err, "failed to decode yaml")
}
var JSONDecoder = func(cfg any, r io.Reader) error {
err := yaml.NewDecoder(r).Decode(cfg)
return errors.Wrap(err, "failed to decode json")
}
var TOMLDecoder = func(cfg any, r io.Reader) error {
_, err := toml.DecodeReader(r, cfg)
return errors.Wrap(err, "failed to decode toml")
}