-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcodec.go
More file actions
35 lines (29 loc) · 773 Bytes
/
codec.go
File metadata and controls
35 lines (29 loc) · 773 Bytes
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
package goconf
import (
"bytes"
"encoding/json"
"github.com/BurntSushi/toml"
)
type decodeFunc func([]byte, interface{}) error
var decodeFuncMap = map[string]decodeFunc{
".toml": func(bytes []byte, data interface{}) error {
_, err := toml.Decode(string(bytes), data)
return err
},
".json": func(bytes []byte, data interface{}) error {
return json.Unmarshal(bytes, data)
},
}
type encodeFunc func(opts interface{}) (string, error)
var encodeFuncMap = map[string]encodeFunc{
".toml": func(opts interface{}) (string, error) {
var by bytes.Buffer
encoder := toml.NewEncoder(&by)
err := encoder.Encode(opts)
return by.String(), err
},
".json": func(opts interface{}) (string, error) {
by, err := json.Marshal(opts)
return string(by), err
},
}