mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-26 05:17:22 +00:00
4072eb711f
* More consistent nil return for state * Merge refs/heads/master into nil-state * Add a check for encode(nil) * Merge branch 'nil-state' of github.com:prysmaticlabs/prysm into nil-state * fix test, thanks @rauljordan * fix tests * gofmt
33 lines
580 B
Go
33 lines
580 B
Go
package kv
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
"github.com/golang/snappy"
|
|
)
|
|
|
|
func decode(data []byte, dst proto.Message) error {
|
|
data, err := snappy.Decode(nil, data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := proto.Unmarshal(data, dst); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func encode(msg proto.Message) ([]byte, error) {
|
|
if msg == nil || reflect.ValueOf(msg).IsNil() {
|
|
return nil, errors.New("cannot encode nil message")
|
|
}
|
|
enc, err := proto.Marshal(msg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return snappy.Encode(nil, enc), nil
|
|
}
|