-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathencode.go
43 lines (32 loc) · 979 Bytes
/
encode.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
// Copyright 2016 Tim Shannon. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package bolthold
import (
"bytes"
"encoding/gob"
)
// EncodeFunc is a function for encoding a value into bytes
type EncodeFunc func(value interface{}) ([]byte, error)
// DecodeFunc is a function for decoding a value from bytes
type DecodeFunc func(data []byte, value interface{}) error
// DefaultEncode is the default encoding func for bolthold (Gob)
func DefaultEncode(value interface{}) ([]byte, error) {
var buff bytes.Buffer
en := gob.NewEncoder(&buff)
err := en.Encode(value)
if err != nil {
return nil, err
}
return buff.Bytes(), nil
}
// DefaultDecode is the default decoding func for bolthold (Gob)
func DefaultDecode(data []byte, value interface{}) error {
var buff bytes.Buffer
de := gob.NewDecoder(&buff)
_, err := buff.Write(data)
if err != nil {
return err
}
return de.Decode(value)
}