From d7aa3d3b07ff2a2b6afcee145defffc7e5a18928 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) # Conflicts: # types/collections.go --- types/collections.go | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/types/collections.go b/types/collections.go index 41b9e18606c9..eb8408040977 100644 --- a/types/collections.go +++ b/types/collections.go @@ -32,11 +32,41 @@ var ( // IntValue represents a collections.ValueCodec to work with Int. IntValue collcodec.ValueCodec[math.Int] = intValueCodec{} +<<<<<<< HEAD +======= + // 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{} + +>>>>>>> 3bc707a5a (feat(types/collections): add `LegacyDec` collection value (#21693)) // 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 // provided in the collections package. TimeKey collcodec.KeyCodec[time.Time] = timeKeyCodec{} +<<<<<<< HEAD +======= + + // LEUint64Key is a collections KeyCodec that encodes uint64 using little endian. + // NOTE: it MUST NOT be used by other modules, distribution relies on this only for + // state backwards compatibility. + // Deprecated: use collections.Uint64Key instead. + LEUint64Key collcodec.KeyCodec[uint64] = leUint64Key{} + + // LengthPrefixedBytesKey is a collections KeyCodec to work with []byte. + // Deprecated: exists only for state compatibility reasons, should not be + // used for new storage keys using []byte. Please use the BytesKey provided + // in the collections package. + LengthPrefixedBytesKey collcodec.KeyCodec[[]byte] = lengthPrefixedBytesKey{collections.BytesKey} +) + +const ( + Int string = "math.Int" + Uint string = "math.Uint" + LegacyDec string = "math.LegacyDec" +>>>>>>> 3bc707a5a (feat(types/collections): add `LegacyDec` collection value (#21693)) ) type addressUnion interface { @@ -166,6 +196,42 @@ func (i intValueCodec) ValueType() string { return "math.Int" } +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) {