-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshaller.go
47 lines (38 loc) · 1.39 KB
/
marshaller.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
package jsmu
import "encoding/json"
var (
// DefaultMarshaller is the Marshaller used when no Marshaller is provided to MU.
DefaultMarshaller = &JSONMarshaller{}
)
// Marshaller is the interface for marshalling data.
type Marshaller interface {
// Marshal marshals v into the expected encoding.
Marshal(v interface{}) ([]byte, error)
// Unmarshal unmarshals data into v.
Unmarshal(data []byte, v interface{}) error
}
// JSONMarshaller implements Marshaller with JSON encoding.
type JSONMarshaller struct{}
// Marshal marshals v into the expected encoding.
func (me *JSONMarshaller) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// Unmarshal unmarshals data into v.
func (me *JSONMarshaller) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
// MockMarshaller implements Marshaller but allows you to swap out the implementation with functions.
//
// Consider using this during unit testing.
type MockMarshaller struct {
MarshalImplementation func(v interface{}) ([]byte, error)
UnmarshalImplementation func(data []byte, v interface{}) error
}
// Marshal marshals v into the expected encoding.
func (me *MockMarshaller) Marshal(v interface{}) ([]byte, error) {
return me.MarshalImplementation(v)
}
// Unmarshal unmarshals data into v.
func (me *MockMarshaller) Unmarshal(data []byte, v interface{}) error {
return me.UnmarshalImplementation(data, v)
}