From 2d5791628d100f79db5dde54b920607d82026827 Mon Sep 17 00:00:00 2001 From: John Letey Date: Fri, 13 Sep 2024 15:01:36 -0400 Subject: [PATCH] feat(types/collections): add `LegacyDec` collection value (#21693) Co-authored-by: Luca Graziotti (cherry picked from commit 3bc707a5a214dde7370102a5c3bebf149ea49fb1) --- types/collections.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/types/collections.go b/types/collections.go index cf7ad0cb2257..47d975e89bf3 100644 --- a/types/collections.go +++ b/types/collections.go @@ -36,6 +36,9 @@ var ( // UintValue represents a collections.ValueCodec to work with Uint. UintValue collcodec.ValueCodec[math.Uint] = uintValueCodec{} + // LegacyDecValue represents a collections.ValueCodec to work with LegacyDec. + LegacyDecValue collcodec.ValueCodec[math.LegacyDec] = legacyDecValueCodec{} + // TimeKey represents a collections.KeyCodec to work with time.Time // Deprecated: exists only for state compatibility reasons, should not // be used for new storage keys using time. Please use the time KeyCodec @@ -56,8 +59,9 @@ var ( ) const ( - Int string = "math.Int" - Uint string = "math.Uint" + Int string = "math.Int" + Uint string = "math.Uint" + LegacyDec string = "math.LegacyDec" ) type addressUnion interface { @@ -245,6 +249,42 @@ func (i uintValueCodec) ValueType() string { return Uint } +type legacyDecValueCodec struct{} + +func (i legacyDecValueCodec) Encode(value math.LegacyDec) ([]byte, error) { + return value.Marshal() +} + +func (i legacyDecValueCodec) Decode(b []byte) (math.LegacyDec, error) { + v := new(math.LegacyDec) + err := v.Unmarshal(b) + if err != nil { + return math.LegacyDec{}, err + } + return *v, nil +} + +func (i legacyDecValueCodec) EncodeJSON(value math.LegacyDec) ([]byte, error) { + return value.MarshalJSON() +} + +func (i legacyDecValueCodec) DecodeJSON(b []byte) (math.LegacyDec, error) { + v := new(math.LegacyDec) + err := v.UnmarshalJSON(b) + if err != nil { + return math.LegacyDec{}, err + } + return *v, nil +} + +func (i legacyDecValueCodec) Stringify(value math.LegacyDec) string { + return value.String() +} + +func (i legacyDecValueCodec) ValueType() string { + return LegacyDec +} + type timeKeyCodec struct{} func (timeKeyCodec) Encode(buffer []byte, key time.Time) (int, error) {