Skip to content

Commit

Permalink
feat(collections): add NewJSONValueCodec (cosmos#19861)
Browse files Browse the repository at this point in the history
Co-authored-by: Facundo Medica <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 15, 2024
1 parent d385402 commit 5bbe468
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
21 changes: 11 additions & 10 deletions collections/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* [#19343](https://github.com/cosmos/cosmos-sdk/pull/19343) – Simplify IndexedMap creation by allowing to infer indexes through reflection.
* [#18933](https://github.com/cosmos/cosmos-sdk/pull/18933) – Add LookupMap implementation. It is basic wrapping of the standard Map methods but is not iterable.
* [#17656](https://github.com/cosmos/cosmos-sdk/pull/17656) – Introduces `Vec`, a collection type that allows to represent a growable array on top of a KVStore.
* [#19343](https://github.com/cosmos/cosmos-sdk/pull/19343) Simplify IndexedMap creation by allowing to infer indexes through reflection.
* [#18933](https://github.com/cosmos/cosmos-sdk/pull/18933) Add LookupMap implementation. It is basic wrapping of the standard Map methods but is not iterable.
* [#17656](https://github.com/cosmos/cosmos-sdk/pull/17656) Introduces `Vec`, a collection type that allows to represent a growable array on top of a KVStore.
* [#19861](https://github.com/cosmos/cosmos-sdk/pull/19861) Add `NewJSONValueCodec` value codec as an alternative for `codec.CollValue` from the SDK for non protobuf types.

## [v0.4.0](https://github.com/cosmos/cosmos-sdk/releases/tag/collections%2Fv0.4.0)

### Features

* [#17024](https://github.com/cosmos/cosmos-sdk/pull/17024) - Introduces `Triple`, a composite key with three keys.
* [#17024](https://github.com/cosmos/cosmos-sdk/pull/17024) Introduces `Triple`, a composite key with three keys.

### API Breaking

* [#17290](https://github.com/cosmos/cosmos-sdk/pull/17290) - Collections iteration methods (Iterate, Walk) will not error when the collection is empty.
* [#17290](https://github.com/cosmos/cosmos-sdk/pull/17290) Collections iteration methods (Iterate, Walk) will not error when the collection is empty.

### Improvements

Expand All @@ -55,20 +56,20 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* [#16074](https://github.com/cosmos/cosmos-sdk/pull/16607) - Introduces `Clear` method for `Map` and `KeySet`
* [#16074](https://github.com/cosmos/cosmos-sdk/pull/16607) Introduces `Clear` method for `Map` and `KeySet`
* [#16773](https://github.com/cosmos/cosmos-sdk/pull/16773)
* Adds `AltValueCodec` which provides a way to decode a value in two ways.
* Adds the possibility to specify an alternative way to decode the values of `KeySet`, `indexes.Multi`, `indexes.ReversePair`.
* Adds `AltValueCodec` which provides a way to decode a value in two ways.
* Adds the possibility to specify an alternative way to decode the values of `KeySet`, `indexes.Multi`, `indexes.ReversePair`.

## [v0.2.0](https://github.com/cosmos/cosmos-sdk/releases/tag/collections%2Fv0.2.0)

### Features

* [#16074](https://github.com/cosmos/cosmos-sdk/pull/16074) Makes the generic Collection interface public, still highly unstable.
* [#16074](https://github.com/cosmos/cosmos-sdk/pull/16074) Makes the generic Collection interface public, still highly unstable.

### API Breaking

* [#16127](https://github.com/cosmos/cosmos-sdk/pull/16127) In the `Walk` method the call back function being passed is allowed to error.
* [#16127](https://github.com/cosmos/cosmos-sdk/pull/16127) In the `Walk` method the call back function being passed is allowed to error.

## [v0.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/collections%2Fv0.1.0)

Expand Down
58 changes: 58 additions & 0 deletions collections/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package collections

import (
"encoding/json"
"fmt"

"cosmossdk.io/collections/codec"
)

func NewJSONValueCodec[T any]() codec.ValueCodec[T] {
return jsonValue[T]{
typeName: fmt.Sprintf("%T", new(T)),
}
}

type jsonValue[T any] struct {
typeName string
}

// Decode implements codec.ValueCodec.
func (jsonValue[T]) Decode(b []byte) (T, error) {
var t T
if err := json.Unmarshal(b, &t); err != nil {
return t, err
}

return t, nil
}

// DecodeJSON implements codec.ValueCodec.
func (jsonValue[T]) DecodeJSON(b []byte) (T, error) {
var t T
if err := json.Unmarshal(b, &t); err != nil {
return t, err
}

return t, nil
}

// Encode implements codec.ValueCodec.
func (jsonValue[T]) Encode(value T) ([]byte, error) {
return json.Marshal(value)
}

// EncodeJSON implements codec.ValueCodec.
func (jsonValue[T]) EncodeJSON(value T) ([]byte, error) {
return json.Marshal(value)
}

// Stringify implements codec.ValueCodec.
func (jsonValue[T]) Stringify(value T) string {
return fmt.Sprintf("%v", value)
}

// ValueType implements codec.ValueCodec.
func (jv jsonValue[T]) ValueType() string {
return fmt.Sprintf("json(%s)", jv.typeName)
}

0 comments on commit 5bbe468

Please sign in to comment.