-
Notifications
You must be signed in to change notification settings - Fork 0
/
gob.go
46 lines (35 loc) · 811 Bytes
/
gob.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
package encoding
import (
"bytes"
"encoding/gob"
)
type GobCodec struct {
codec
}
func (c *GobCodec) Marshal(value interface{}) ([]byte, error) {
if primitive, err := c.marshalPrimitive(value); err != errNotPrimitive {
return primitive, err
}
var b bytes.Buffer
encoder := gob.NewEncoder(&b)
if err := encoder.Encode(value); err != nil {
return nil, err
}
return c.compress(b.Bytes())
}
func (c *GobCodec) Unmarshal(byt []byte, ptr interface{}) (err error) {
if err := c.unmarshalPrimitive(byt, ptr); err != errNotPrimitive {
return err
}
if byt, err = c.decompress(byt); err != nil {
return
}
b := bytes.NewBuffer(byt)
decoder := gob.NewDecoder(b)
return decoder.Decode(ptr)
}
func NewGobCodec(compressor Compressor) *GobCodec {
return &GobCodec{
codec{compressor},
}
}