Skip to content

Commit 5bbe468

Browse files
julienrbrtfacundomedicacoderabbitai[bot]
authored
feat(collections): add NewJSONValueCodec (#19861)
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent d385402 commit 5bbe468

2 files changed

Lines changed: 69 additions & 10 deletions

File tree

collections/CHANGELOG.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,20 @@ Ref: https://keepachangelog.com/en/1.0.0/
3333

3434
### Features
3535

36-
* [#19343](https://github.com/cosmos/cosmos-sdk/pull/19343) – Simplify IndexedMap creation by allowing to infer indexes through reflection.
37-
* [#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.
38-
* [#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.
36+
* [#19343](https://github.com/cosmos/cosmos-sdk/pull/19343) Simplify IndexedMap creation by allowing to infer indexes through reflection.
37+
* [#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.
38+
* [#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.
39+
* [#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.
3940

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

4243
### Features
4344

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

4647
### API Breaking
4748

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

5051
### Improvements
5152

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

5657
### Features
5758

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

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

6566
### Features
6667

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

6970
### API Breaking
7071

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

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

collections/json.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package collections
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"cosmossdk.io/collections/codec"
8+
)
9+
10+
func NewJSONValueCodec[T any]() codec.ValueCodec[T] {
11+
return jsonValue[T]{
12+
typeName: fmt.Sprintf("%T", new(T)),
13+
}
14+
}
15+
16+
type jsonValue[T any] struct {
17+
typeName string
18+
}
19+
20+
// Decode implements codec.ValueCodec.
21+
func (jsonValue[T]) Decode(b []byte) (T, error) {
22+
var t T
23+
if err := json.Unmarshal(b, &t); err != nil {
24+
return t, err
25+
}
26+
27+
return t, nil
28+
}
29+
30+
// DecodeJSON implements codec.ValueCodec.
31+
func (jsonValue[T]) DecodeJSON(b []byte) (T, error) {
32+
var t T
33+
if err := json.Unmarshal(b, &t); err != nil {
34+
return t, err
35+
}
36+
37+
return t, nil
38+
}
39+
40+
// Encode implements codec.ValueCodec.
41+
func (jsonValue[T]) Encode(value T) ([]byte, error) {
42+
return json.Marshal(value)
43+
}
44+
45+
// EncodeJSON implements codec.ValueCodec.
46+
func (jsonValue[T]) EncodeJSON(value T) ([]byte, error) {
47+
return json.Marshal(value)
48+
}
49+
50+
// Stringify implements codec.ValueCodec.
51+
func (jsonValue[T]) Stringify(value T) string {
52+
return fmt.Sprintf("%v", value)
53+
}
54+
55+
// ValueType implements codec.ValueCodec.
56+
func (jv jsonValue[T]) ValueType() string {
57+
return fmt.Sprintf("json(%s)", jv.typeName)
58+
}

0 commit comments

Comments
 (0)