-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
81 lines (69 loc) · 1.83 KB
/
encoder.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
package config
import (
"errors"
"sync"
)
var (
// default package level Encoder factory
encoderFactory = NewEncoderFactory()
// ErrUnsupportedEncoder is error
ErrUnsupportedEncoder = errors.New("unsupported encoder")
)
// Encoder handles source encoding formats
//
// Unmarshal []byte to object
// Marshal object to []byte
type Encoder interface {
Decode([]byte, interface{}) error
Encode(interface{}) ([]byte, error)
String() string
}
// EncoderFactory multi encoders
type EncoderFactory struct {
sync.RWMutex
encoders map[string]Encoder
}
// Register register a loader
func (t *EncoderFactory) Register(Encoder Encoder, names ...string) error {
for _, name := range names {
if _, ok := t.encoders[name]; ok {
// TODO: need to return error?
}
t.encoders[name] = Encoder
}
return nil
}
// Decode data to out with special type name
func (t *EncoderFactory) Decode(name string, data []byte, out interface{}) error {
er, ok := t.encoders[name]
if !ok {
return ErrUnsupportedEncoder
}
return er.Decode(data, out)
}
// Encode out to data with special type name
func (t *EncoderFactory) Encode(name string, out interface{}) ([]byte, error) {
er, ok := t.encoders[name]
if !ok {
return nil, ErrUnsupportedEncoder
}
return er.Encode(out)
}
// NewEncoderFactory create a new Encoder factry
func NewEncoderFactory() *EncoderFactory {
return &EncoderFactory{
encoders: make(map[string]Encoder),
}
}
// RegisterEncoder retister a type loader to package level
func RegisterEncoder(Encoder Encoder, names ...string) error {
return encoderFactory.Register(Encoder, names...)
}
func init() {
// register all encoder
RegisterEncoder(NewJSONEncoder(), "json")
RegisterEncoder(NewYAMLEncoder(), "yaml", "yml")
RegisterEncoder(NewTOMLEncoder(), "toml")
RegisterEncoder(NewHCLEncoder(), "hcl")
RegisterEncoder(NewXMLEncoder(), "xml")
}