Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types/collections): add LegacyDec collection value (backport #21693) #21723

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions types/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Loading