mirror of
https://github.com/timshannon/bolthold.git
synced 2025-12-24 13:37:58 +08:00
44 lines
979 B
Go
44 lines
979 B
Go
// 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)
|
|
}
|