cosmos-sdk/collections/values.go
Aaron Craelius b3c750ca27
feat(collections): genesis support (#14331)
Co-authored-by: testinginprod <testinginprod@somewhere.idk>
Co-authored-by: testinginprod <98415576+testinginprod@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Rafael Tenfen <rafaeltenfen.rt@gmail.com>
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Matt Kocubinski <mkocubinski@gmail.com>
Co-authored-by: Jacob Gadikian <faddat@users.noreply.github.com>
Co-authored-by: samricotta <37125168+samricotta@users.noreply.github.com>
Co-authored-by: Noel Ukwa <noeluchechukwu@gmail.com>
Co-authored-by: JayT106 <JayT106@users.noreply.github.com>
Co-authored-by: Julián Toledano <JulianToledano@users.noreply.github.com>
Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
Co-authored-by: Denver <aeharvlee@gmail.com>
Co-authored-by: Hyunwoo Lee <denver@Hyunwoos-MacBook-Pro-2.local>
Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com>
Co-authored-by: Vladislav Varadinov <vladislav.varadinov@gmail.com>
Co-authored-by: cipherZ <dev@cipherz.com>
2023-01-17 11:25:10 +00:00

91 lines
2.0 KiB
Go

package collections
import (
"encoding/binary"
"encoding/json"
"fmt"
"strconv"
)
var (
// Uint64Value implements a ValueCodec for uint64. It converts the uint64 to big endian bytes.
// The JSON representation is the string format of uint64.
Uint64Value ValueCodec[uint64] = uint64Value{}
// StringValue implements a ValueCodec for string.
StringValue ValueCodec[string] = stringValue{}
)
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()
}
func (u uint64Value) EncodeJSON(value uint64) ([]byte, error) {
return uint64EncodeJSON(value)
}
func (u uint64Value) DecodeJSON(b []byte) (uint64, error) {
return uint64DecodeJSON(b)
}
func uint64EncodeJSON(value uint64) ([]byte, error) {
str := `"` + strconv.FormatUint(value, 10) + `"`
return []byte(str), nil
}
func uint64DecodeJSON(b []byte) (uint64, error) {
var str string
err := json.Unmarshal(b, &str)
if err != nil {
return 0, err
}
return strconv.ParseUint(str, 10, 64)
}
type stringValue struct{}
func (stringValue) Encode(value string) ([]byte, error) {
return []byte(value), nil
}
func (stringValue) Decode(b []byte) (string, error) {
return string(b), nil
}
func (stringValue) EncodeJSON(value string) ([]byte, error) {
return json.Marshal(value)
}
func (stringValue) DecodeJSON(b []byte) (string, error) {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return "", err
}
return s, nil
}
func (stringValue) Stringify(value string) string {
return value
}
func (stringValue) ValueType() string {
return "string"
}