-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefault_codec.go
More file actions
37 lines (32 loc) · 771 Bytes
/
default_codec.go
File metadata and controls
37 lines (32 loc) · 771 Bytes
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
package mgcache
import (
"github.com/vmihailenco/msgpack/v5"
)
type (
defaultCodec struct {
}
)
// NewDefaultCodec initializes the default codec
func NewDefaultCodec() ICodec {
return &defaultCodec{}
}
// Encode encodes value pointed to by valuePtr
// into bytes []byte, which can be stored in cache
func (d defaultCodec) Encode(value interface{}) (bytes []byte, err error) {
switch value.(type) {
case []byte:
bytes = value.([]byte)
return
}
return msgpack.Marshal(value)
}
// Decode decodes bytes []byte from cache
// into value pointed to by valuePtr.
func (d defaultCodec) Decode(bytes []byte, valuePtr interface{}) (err error) {
switch typ := valuePtr.(type) {
case *[]byte:
*typ = bytes
return
}
return msgpack.Unmarshal(bytes, valuePtr)
}