mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-06 01:32:18 +00:00
eae2268dd1
* Prevent encoding a nil message * Merge refs/heads/master into prevent-saving-nil-msg
32 lines
537 B
Go
32 lines
537 B
Go
package kv
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"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 {
|
|
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
|
|
}
|