refactor: moving value codecs to a single place (#14912)
This commit is contained in:
@@ -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"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user