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(collections): add NewJSONValueCodec #19861

Merged
merged 4 commits into from
Apr 15, 2024
Merged

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Mar 25, 2024

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...

  • included the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

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...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features
    • Simplified creation of IndexedMap.
    • Added LookupMap for enhanced data lookup.
    • Introduced Vec for dynamic array support.
    • New JSONValueCodec for improved JSON encoding/decoding.
    • Added Triple for composite key functionality.
    • Introduced Clear method for easier data removal in Map and KeySet.
    • Made Collection interface public for broader usability.
  • Enhancements
    • Enhanced decoding of values for better data handling.

Copy link
Contributor

coderabbitai bot commented Mar 25, 2024

Walkthrough

The update aims to enhance the collections library by introducing new data structures like LookupMap, Vec, and Triple, along with a new JSONValueCodec for efficient JSON data handling. It simplifies IndexedMap creation, improves iteration methods, adds a Clear method, makes the Collection interface public, and enhances value decoding. These changes collectively provide developers with more powerful and flexible tools for working with collections.

Changes

Files Change Summaries
collections/... - Simplified IndexedMap creation
- Added LookupMap, Vec, Triple
- Improved iteration methods
- Added Clear method
- Made Collection interface public
- Enhanced value decoding
collections/json.go - Introduced NewJSONValueCodec for JSON encoding/decoding

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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

Note: Auto-reply has been disabled for this repository by the repository owner. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

Commits Files that changed from the base of the PR and between 80e1a45 and 895dc94.
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: The NewJSONValueCodec function correctly implements the factory pattern for creating a new JSON value codec. It uses reflection to obtain the type name of T, which is stored for later use. This approach is efficient and follows best practices for generic type handling in Go.


16-18: The jsonValue struct is minimalistic and serves its purpose well by storing the type name of the generic type T. This design choice keeps the struct lightweight and focused on its role in the codec implementation.


20-28: The Decode method correctly implements the codec.ValueCodec interface for decoding JSON bytes into a Go value of type T. It uses the standard json.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: The Encode method correctly implements the codec.ValueCodec interface for encoding a Go value of type T into JSON bytes. It uses the standard json.Marshal function, which is the appropriate choice for JSON encoding in Go. The method is concise and correctly handles the encoding process.


50-53: The Stringify method provides a simple way to convert a value of type T into a string representation. It uses fmt.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: The ValueType method correctly implements the codec.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 of Triple 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 the Clear method for Map and KeySet, along with the introduction of AltValueCodec, are significant enhancements. The entries are clear but could benefit from a brief description of AltValueCodec'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 generic Collection 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.

Comment on lines +30 to +38
// 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
}
Copy link
Contributor

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.

Suggested change
// 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
}

Comment on lines +45 to +48
// EncodeJSON implements codec.ValueCodec.
func (jsonValue[T]) EncodeJSON(value T) ([]byte, error) {
return json.Marshal(value)
}
Copy link
Contributor

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.

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

collections/CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Member

@facundomedica facundomedica left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

Commits Files that changed from the base of the PR and between 895dc94 and 2243035.
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"

Comment on lines +36 to +39
* [#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.
Copy link
Contributor

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.

Suggested change
* [#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.

@tac0turtle tac0turtle added this pull request to the merge queue Apr 15, 2024
Merged via the queue into main with commit 5bbe468 Apr 15, 2024
62 checks passed
@tac0turtle tac0turtle deleted the julien/json-value-codec branch April 15, 2024 11:28
alpe added a commit to alpe/cosmos-sdk that referenced this pull request Apr 16, 2024
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants