cosmos-sdk/collections/values.go
testinginprod fca24b6687
feat(collections): Initialise core (Prefix, KeyEncoder, ValueEncoder, Map) (#14134)
Co-authored-by: testinginprod <testinginprod@somewhere.idk>
Co-authored-by: Aaron Craelius <aaron@regen.network>
2022-12-08 15:57:21 +00:00

30 lines
650 B
Go

package collections
import (
"encoding/binary"
"fmt"
)
var Uint64Value ValueCodec[uint64] = uint64Value{}
type uint64Value struct{}
func (u uint64Value) Encode(value uint64) ([]byte, error) {
return binary.BigEndian.AppendUint64(make([]byte, 0, 8), value), nil
}
func (u uint64Value) Decode(b []byte) (uint64, error) {
if len(b) != 8 {
return 0, fmt.Errorf("%w: uint64 value size invalid, want: 8, got: %d", ErrEncoding, len(b))
}
return binary.BigEndian.Uint64(b), nil
}
func (u uint64Value) Stringify(value uint64) string {
return Uint64Key.Stringify(value)
}
func (u uint64Value) ValueType() string {
return Uint64Key.KeyType()
}