-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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(collections): add NewJSONValueCodec
#19861
Conversation
WalkthroughThe update aims to enhance the collections library by introducing new data structures like Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
Review Status
Configuration used: .coderabbit.yml
Files selected for processing (2)
- collections/CHANGELOG.md (2 hunks)
- collections/json.go (1 hunks)
Additional Context Used
Path-based Instructions (2)
collections/json.go (1)
**/*.go
:
Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.collections/CHANGELOG.md (1)
**/*.md
:
Assess the documentation for misspellings, grammatical errors, missing documentation and correctness
Additional comments not posted (9)
collections/json.go (6)
10-14
: TheNewJSONValueCodec
function correctly implements the factory pattern for creating a new JSON value codec. It uses reflection to obtain the type name ofT
, which is stored for later use. This approach is efficient and follows best practices for generic type handling in Go.
16-18
: ThejsonValue
struct is minimalistic and serves its purpose well by storing the type name of the generic typeT
. This design choice keeps the struct lightweight and focused on its role in the codec implementation.
20-28
: TheDecode
method correctly implements thecodec.ValueCodec
interface for decoding JSON bytes into a Go value of typeT
. It uses the standardjson.Unmarshal
function, which is the correct choice for JSON decoding in Go. Error handling is appropriately implemented, returning the error to the caller if unmarshalling fails.
40-43
: TheEncode
method correctly implements thecodec.ValueCodec
interface for encoding a Go value of typeT
into JSON bytes. It uses the standardjson.Marshal
function, which is the appropriate choice for JSON encoding in Go. The method is concise and correctly handles the encoding process.
50-53
: TheStringify
method provides a simple way to convert a value of typeT
into a string representation. It usesfmt.Sprintf
with the%v
verb, which is a reasonable choice for a generic string representation. This method enhances the usability of the codec by allowing easy logging or debugging of values.
55-58
: TheValueType
method correctly implements thecodec.ValueCodec
interface, providing a string representation of the value type handled by the codec. It combines the "json" prefix with the stored type name, offering clear and informative output about the codec's target type.collections/CHANGELOG.md (3)
45-49
: The introduction ofTriple
as a composite key and the clarification on collections iteration methods are well-documented. These entries are clear, link to the relevant pull requests, and correctly categorized under "Features" and "API Breaking" respectively.
59-62
: The addition of theClear
method forMap
andKeySet
, along with the introduction ofAltValueCodec
, are significant enhancements. The entries are clear but could benefit from a brief description ofAltValueCodec
's purpose and use cases for clarity.Consider adding a brief description of
AltValueCodec
to clarify its purpose and use cases.
68-72
: The decision to make the genericCollection
interface public is a notable change, correctly documented under "Features" and "API Breaking". The entries are clear and provide direct links to the relevant pull requests, facilitating easy access to more detailed information.
// 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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DecodeJSON
method appears to be a duplicate of the Decode
method, performing the same operation. This redundancy could lead to confusion and maintenance issues. Consider removing DecodeJSON
or clarifying its intended use case if it differs from Decode
.
- // 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
- }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
// 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 | |
} |
// EncodeJSON implements codec.ValueCodec. | ||
func (jsonValue[T]) EncodeJSON(value T) ([]byte, error) { | ||
return json.Marshal(value) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The EncodeJSON
method, similar to DecodeJSON
, appears to be a duplicate of the Encode
method, performing the same operation. This redundancy could lead to confusion and maintenance issues. Consider removing EncodeJSON
or clarifying its intended use case if it differs from Encode
.
- // EncodeJSON implements codec.ValueCodec.
- func (jsonValue[T]) EncodeJSON(value T) ([]byte, error) {
- return json.Marshal(value)
- }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
// EncodeJSON implements codec.ValueCodec. | |
func (jsonValue[T]) EncodeJSON(value T) ([]byte, error) { | |
return json.Marshal(value) | |
} |
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review Status
Configuration used: .coderabbit.yml
Files selected for processing (1)
- collections/CHANGELOG.md (2 hunks)
Additional Context Used
Path-based Instructions (1)
collections/CHANGELOG.md (1)
Pattern
**/*.md
: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"
* [#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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure consistency in spacing after the PR links in the changelog entries.
- * [#19343](https://github.com/cosmos/cosmos-sdk/pull/19343) Simplify IndexedMap creation by allowing to infer indexes through reflection.
+ * [#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.
+ * [#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.
+ * [#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.
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
* [#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. | |
* [#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. |
* main: refactor(x/simulation)!: remove accounts string (cosmos#20056) fix(baseapp): don't share global gas meter in tx execution (cosmos#19616) feat(x/accounts): use router service from env (cosmos#20003) refactor(x): remove Address.String() (cosmos#20048) build(deps): Bump github.com/jhump/protoreflect from 1.15.6 to 1.16.0 in /tests (cosmos#20040) feat(collections): add `NewJSONValueCodec` (cosmos#19861)
Description
ref: #19788 (comment)
Author Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
!
in the type prefix if API or client breaking changeCHANGELOG.md
Reviewers Checklist
All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.
I have...
Summary by CodeRabbit
IndexedMap
.LookupMap
for enhanced data lookup.Vec
for dynamic array support.JSONValueCodec
for improved JSON encoding/decoding.Triple
for composite key functionality.Clear
method for easier data removal inMap
andKeySet
.Collection
interface public for broader usability.