-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshared.go
45 lines (40 loc) · 1.11 KB
/
shared.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
package envelope
import (
"encoding/json"
"fmt"
"github.com/naoina/toml"
"gopkg.in/yaml.v2"
)
type structuredCodec struct {
Marshal func(interface{}) ([]byte, error)
Unmarshal func([]byte, interface{}) error
}
func codecForFormat(format string) (structuredCodec, error) {
switch format {
case "yaml":
return structuredCodec{
Marshal: yaml.Marshal,
Unmarshal: yaml.Unmarshal,
}, nil
case "json":
return structuredCodec{
Marshal: func(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
},
Unmarshal: json.Unmarshal,
}, nil
case "toml":
return structuredCodec{
Marshal: func(v interface{}) ([]byte, error) {
// Internally we use with map[interface{}]interface{}
// YAML uses that natively and JSON figures it its actually a map[string]interface{} (or the implementation doesn't care)
// TOML *does* care so we have to do some type juggling
ptr := v.(*interface{})
val := (*ptr).(map[string]interface{})
return toml.Marshal(val)
},
Unmarshal: toml.Unmarshal,
}, nil
}
return structuredCodec{}, fmt.Errorf("Unknown format '%s'", format)
}