-
Notifications
You must be signed in to change notification settings - Fork 202
/
marshalizerMock.go
53 lines (41 loc) · 1.11 KB
/
marshalizerMock.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
package testscommon
import (
"encoding/json"
"errors"
)
// ErrMockMarshalizer -
var ErrMockMarshalizer = errors.New("MarshalizerMock generic error")
// MarshalizerMock that will be used for testing
type MarshalizerMock struct {
Fail bool
}
// Marshal converts the input object in a slice of bytes
func (mm MarshalizerMock) Marshal(obj interface{}) ([]byte, error) {
if mm.Fail {
return nil, ErrMockMarshalizer
}
if obj == nil {
return nil, errors.New("nil object to serilize from")
}
return json.Marshal(obj)
}
// Unmarshal applies the serialized values over an instantiated object
func (mm MarshalizerMock) Unmarshal(obj interface{}, buff []byte) error {
if mm.Fail {
return ErrMockMarshalizer
}
if obj == nil {
return errors.New("nil object to serilize to")
}
if buff == nil {
return errors.New("nil byte buffer to deserialize from")
}
if len(buff) == 0 {
return errors.New("empty byte buffer to deserialize from")
}
return json.Unmarshal(buff, obj)
}
// IsInterfaceNil returns true if there is no value under the interface
func (mm MarshalizerMock) IsInterfaceNil() bool {
return false
}