-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaults.go
62 lines (49 loc) · 1.25 KB
/
defaults.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
package encode
import (
"bytes"
"encoding/gob"
"encoding/json"
"github.com/pkg/errors"
)
const TEXT = "plain_text"
type textEncoding struct{}
func (e *textEncoding) String() string { return TEXT }
func (e *textEncoding) Extension() string { return "txt" }
func (e *textEncoding) Marshal(v interface{}) ([]byte, error) {
var out []byte
switch t := v.(type) {
case []byte:
out = t
case string:
out = []byte(t)
case *string:
out = []byte(*t)
default:
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(v); err != nil {
return nil, errors.WithStack(err)
}
out = buf.Bytes()
}
return out, nil
}
// TODO: should this even be implemented for plain text?
func (e *textEncoding) Unmarshal(data []byte, v interface{}) error {
switch s := v.(type) {
case *string:
*s = string(data)
default:
return errors.Errorf("cannot unmarshal plain text to type '%T'", s)
}
return nil
}
const JSON = "json"
type jsonEncoding struct{}
func (e *jsonEncoding) String() string { return JSON }
func (e *jsonEncoding) Extension() string { return JSON }
func (e *jsonEncoding) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (e *jsonEncoding) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}