refactor: moving value codecs to a single place (#14912)

This commit is contained in:
Marko
2023-02-06 09:38:20 +01:00
committed by GitHub
parent 41a3dfeced
commit 8cf814fb8c
5 changed files with 47 additions and 549 deletions
+43
View File
@@ -1,6 +1,8 @@
package types
import (
"cosmossdk.io/collections"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
)
@@ -20,3 +22,44 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterInterface(MsgInterfaceProtoName, (*Msg)(nil))
}
// Collection Codecs
// IntValue represents a collections.ValueCodec to work with Int.
var IntValue collections.ValueCodec[math.Int] = intValueCodec{}
type intValueCodec struct{}
func (i intValueCodec) Encode(value math.Int) ([]byte, error) {
return value.Marshal()
}
func (i intValueCodec) Decode(b []byte) (math.Int, error) {
v := new(Int)
err := v.Unmarshal(b)
if err != nil {
return Int{}, err
}
return *v, nil
}
func (i intValueCodec) EncodeJSON(value math.Int) ([]byte, error) {
return value.MarshalJSON()
}
func (i intValueCodec) DecodeJSON(b []byte) (Int, error) {
v := new(Int)
err := v.UnmarshalJSON(b)
if err != nil {
return Int{}, err
}
return *v, nil
}
func (i intValueCodec) Stringify(value Int) string {
return value.String()
}
func (i intValueCodec) ValueType() string {
return "math.Int"
}