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: impl json object #128

Merged
merged 7 commits into from
Sep 20, 2024
Merged

feat: impl json object #128

merged 7 commits into from
Sep 20, 2024

Conversation

beer-1
Copy link
Member

@beer-1 beer-1 commented Sep 20, 2024

Description

Closes: #XXXX


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, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • 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

    • Introduced support for JSONValue and JSONObject types in JSON deserialization and conversion processes.
    • Added public functions for manipulating JSON objects, including keys, get_elem, and set_elem.
    • Enhanced the ability to convert Move values to JSON representations and vice versa.
    • Improved the structure of the initia_std::json and minitia_std::json modules for better JSON data handling.
  • Bug Fixes

    • Removed unsupported conversion logic for certain JSON types, simplifying the conversion process.
  • Tests

    • Expanded test cases to validate new functionalities and ensure correct behavior of JSON manipulations.

@beer-1 beer-1 requested a review from a team as a code owner September 20, 2024 08:33
Copy link

coderabbitai bot commented Sep 20, 2024

Warning

Rate limit exceeded

@beer-1 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 11 minutes and 31 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between d3bc551 and a7c8401.

Walkthrough

The changes involve significant enhancements to the JSON handling capabilities within the codebase. New structures such as JSONValue, JSONObject, and Element have been introduced to facilitate JSON data representation. Functions for converting between JSON and Move types have been added or modified, and existing commented-out code has been removed. Additionally, test cases have been expanded to validate the new functionalities, ensuring proper serialization and deserialization of JSON data.

Changes

Files Change Summary
crates/json/src/json_to_move.rs Removed unsupported commented-out code related to JSONValue and JSONObject. Minor formatting changes for readability in the testing module.
crates/json/src/json_to_value.rs Introduced functionality for deserializing JSONValue and JSONObject. Added test cases for validating deserialization.
crates/json/src/move_to_json.rs Added functions for converting Move values to JSON values, specifically handling JSONValue and JSONObject. Updated tests to cover new functionality.
precompile/modules/initia_stdlib/sources/json.move Introduced new structs (JSONValue, JSONObject, Element) and public functions for manipulating JSON data. Expanded test cases for marshaling and unmarshaling JSON.
precompile/modules/minitia_stdlib/sources/json.move Similar enhancements as in initia_std, with new structures and functions for JSON manipulation, including keys, get_elem, and set_elem. Expanded test cases for validation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant JSONHandler
    participant MoveConverter

    User->>JSONHandler: Send JSON data
    JSONHandler->>MoveConverter: Convert JSON to Move types
    MoveConverter->>JSONHandler: Return Move representation
    JSONHandler->>User: Send converted Move data
Loading

🐇 "In the land of bytes and keys,
JSON dances with such ease.
Move and JSON, hand in hand,
Together they make a grand stand.
With structs and functions, oh what a sight,
Hopping through code, everything feels right!" 🐇


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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @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.
    -- @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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

@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: 7

Outside diff range and nitpick comments (5)
precompile/modules/initia_stdlib/sources/json.move (1)

35-35: Simplify comparison by removing unnecessary dereferencing.

In the comparisons elem.key == *key_bytes, the dereferencing operator * is used on key_bytes, which is already a vector<u8>. Since key_bytes is not a reference, the * operator may be unnecessary. Consider removing it to improve code clarity.

Apply this diff to remove the unnecessary dereferencing:

- elem.key == *key_bytes
+ elem.key == key_bytes

Also applies to: 51-51

precompile/modules/minitia_stdlib/sources/json.move (3)

12-14: Consider using a map data structure for efficient key-value storage

The JSONObject currently uses a vector<Element> to store its elements, which results in linear time complexity for get_elem and set_elem operations. To enhance performance, especially for large JSON objects, consider utilizing a map or hash table structure that provides constant-time complexity for insertions and lookups.


65-68: Review the necessity of the use_elem function

The use_elem inline function exists to prevent compile errors due to type inference issues with the compiler v1. If possible, investigate whether compiler updates or code refactoring could eliminate the need for this workaround, enhancing code clarity.


181-187: Handle absence of keys when extracting values in tests

In the test cases where get_elem is used, option::extract is called without checking if the option is some. While tests are expected to pass, consider pattern matching on the Option to handle the none case explicitly. This makes the test more robust and avoids potential panics if the key is absent.

Apply this diff to handle the none case:

-assert!(option::extract(&mut get_elem<u64>(&json_obj, string::utf8(b"a"))) == 42, 4);
+match get_elem<u64>(&json_obj, string::utf8(b"a")) {
+    option::some(value) => assert!(value == 42, 4),
+    option::none() => assert!(false, 4),
+}
crates/json/src/move_to_json.rs (1)

517-616: Expand test cases for JSON serialization

The added tests for JSONValue and JSONObject are valuable. To ensure robustness, consider adding test cases for:

  • Nested JSON structures.
  • Empty JSON arrays and objects.
  • Invalid or malformed JSON data.
  • Special characters and Unicode in strings.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 44a7cff and 635cc44.

Files selected for processing (5)
  • crates/json/src/json_to_move.rs (3 hunks)
  • crates/json/src/json_to_value.rs (2 hunks)
  • crates/json/src/move_to_json.rs (4 hunks)
  • precompile/modules/initia_stdlib/sources/json.move (2 hunks)
  • precompile/modules/minitia_stdlib/sources/json.move (2 hunks)
Additional comments not posted (8)
crates/json/src/json_to_move.rs (1)

140-153: Verify the removal of JSONValue and JSONObject support.

The commented-out code handling the conversion of JSONValue and JSONObject types has been removed, indicating that these types are no longer supported as entry function arguments.

Please confirm that this removal is intentional and the potential impact on existing code has been assessed.

Run the following script to verify the usage of JSONValue and JSONObject types:

precompile/modules/minitia_stdlib/sources/json.move (2)

23-28: Function keys is correctly implemented

The keys function effectively retrieves the list of keys from the JSONObject. The use of vector::map_ref and string::utf8 correctly transforms the keys into String types.


171-179: Tests verify JSON marshaling and unmarshaling accurately

The tests at the end of the file correctly verify that marshaling and unmarshaling functions for JSONValue and JSONObject work as intended. They ensure that the process is lossless and that the original and resulting JSON strings are identical.

crates/json/src/move_to_json.rs (2)

84-88: Properly handle JSONValue and JSONObject types

The addition of checks for is_json_value and is_json_object enhances the conversion functionality to support JSON types appropriately.


284-295: Addition of helper functions is_json_value and is_json_object

The new helper functions improve code readability and maintainability by clearly identifying JSON-related types.

crates/json/src/json_to_value.rs (3)

113-134: Implementation of JSONValue and JSONObject Deserialization Looks Good

The code correctly handles deserialization of JSONValue and JSONObject by serializing JSON values into byte vectors and constructing the appropriate Move structs. Error handling is properly managed using map_err with descriptive messages.


623-646: Unit Test for JSONValue Deserialization is Correct

The unit test test_deserialize_json_to_value_json_value accurately verifies the deserialization of a JSONValue. It checks that the JSON string is correctly serialized into a byte vector and wrapped in the expected Move struct format.


648-698: Unit Test for JSONObject Deserialization is Accurate

The unit test test_deserialize_json_to_value_json_object effectively tests the deserialization of a JSONObject. It ensures that key-value pairs are properly converted into structs with byte vectors for both keys and values, and that they are correctly assembled into the expected Move struct layout.

crates/json/src/move_to_json.rs Outdated Show resolved Hide resolved
crates/json/src/move_to_json.rs Outdated Show resolved Hide resolved
crates/json/src/move_to_json.rs Outdated Show resolved Hide resolved
Copy link

@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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 635cc44 and d3bc551.

Files ignored due to path filters (2)
  • api/libcompiler.dylib is excluded by !**/*.dylib
  • api/libmovevm.dylib is excluded by !**/*.dylib
Files selected for processing (11)
  • crates/e2e-move-tests/src/tests/args.rs (1 hunks)
  • crates/e2e-move-tests/src/tests/move_unit.rs (1 hunks)
  • crates/e2e-move-tests/src/tests/view_output.rs (1 hunks)
  • crates/json/src/move_to_json.rs (4 hunks)
  • crates/natives/src/query.rs (1 hunks)
  • crates/natives/src/staking.rs (1 hunks)
  • crates/types/src/staking_change_set.rs (1 hunks)
  • crates/vm/src/verifier/transaction_arg_validation.rs (1 hunks)
  • libcompiler/src/interface.rs (4 hunks)
  • precompile/modules/initia_stdlib/sources/json.move (2 hunks)
  • precompile/modules/minitia_stdlib/sources/json.move (2 hunks)
Files skipped from review due to trivial changes (7)
  • crates/e2e-move-tests/src/tests/args.rs
  • crates/e2e-move-tests/src/tests/view_output.rs
  • crates/natives/src/query.rs
  • crates/natives/src/staking.rs
  • crates/types/src/staking_change_set.rs
  • crates/vm/src/verifier/transaction_arg_validation.rs
  • libcompiler/src/interface.rs
Additional comments not posted (16)
crates/e2e-move-tests/src/tests/move_unit.rs (1)

7-10: LGTM!

The changes to the import statements improve readability without affecting the functionality or logic of the code. The reorganization of the import order and the reformatting to span multiple lines enhance clarity.

precompile/modules/initia_stdlib/sources/json.move (7)

2-4: LGTM!

The imports are relevant and required for the functionality implemented in this module.


6-20: LGTM!

The new structs JSONValue, JSONObject, and Element are well-defined and serve the purpose of representing JSON data structures. The comments provide clear explanations of their purposes, and the structs have appropriate abilities for their intended usage.


22-28: LGTM, but verify if the use_elem workaround is still necessary.

The keys function correctly retrieves the keys from the JSONObject and returns them as a vector of strings. The usage of vector::map_ref is appropriate for mapping over the elements and converting the keys.

However, the use_elem function is included to prevent compile errors related to type inference in compiler v1. Please check if this workaround is still required with the current compiler version. If not, consider removing it to simplify the code.


30-44: LGTM, but consider handling potential deserialization errors.

The get_elem function correctly retrieves the value of a given key from the JSONObject and returns it as an Option<T>. The usage of vector::find is appropriate for searching the elements for the matching key, and the function handles the case when the key is not found by returning None.

However, the function calls unmarshal<T>(elem.value), which may fail if elem.value cannot be deserialized into type T. To enhance robustness, consider handling possible errors from unmarshal and returning an appropriate value in such cases.


46-63: LGTM!

The set_elem function correctly sets or overwrites an element in the JSONObject based on the provided key and value. The usage of vector::find is appropriate for searching the elements for the matching key, and the function handles both cases: updating an existing element and adding a new element. The function also marshals the provided value before storing it in the JSONObject.


66-68: Verify if the use_elem workaround is still necessary.

The use_elem function is included to prevent compile errors related to type inference in compiler v1. Please check if this workaround is still required with the current compiler version. If not, consider removing it to simplify the code.


171-190: LGTM!

The test cases in the test_marshal_unmarshal function cover important scenarios for marshaling and unmarshaling JSON data. The assertions verify that the marshaled JSON matches the expected JSON string and that the unmarshaled data matches the original data. The test cases for get_elem and set_elem ensure that the functions work as expected. Overall, the test cases provide good coverage for the functionality implemented in the json module.

precompile/modules/minitia_stdlib/sources/json.move (4)

2-4: LGTM!

The imports are necessary and look good.


6-20: LGTM!

The new structs JSONValue, JSONObject, and Element are well-defined and serve the purpose of representing JSON data effectively. The struct abilities and field types are appropriate.


171-190: LGTM!

The test cases cover a good range of scenarios to validate the JSON marshaling and unmarshaling functionality. The assertions are well-defined and check for the expected values. The test cases also validate the functionality of get_elem and set_elem functions effectively.


31-44: Consider using vector::equals for reliable key comparison.

In the get_elem function, the comparison elem.key == *key_bytes relies on direct vector comparison. While this may work if vector equality compares contents, it's safer to use vector::equals to compare the contents of the byte vectors explicitly. This ensures accurate key matching regardless of how vector equality is implemented.

Apply this diff to enhance the key comparison:

 let (found, idx) = vector::find(&obj.elems, |elem| {
     use_elem(elem);
-    elem.key == *key_bytes
+    vector::equals(&elem.key, key_bytes)
 });

Likely invalid or redundant comment.

crates/json/src/move_to_json.rs (4)

84-88: LGTM!

The changes to handle JSONValue and JSONObject types are implemented correctly. The function now checks for these types before checking for UTF-8 strings and calls the appropriate conversion functions.


144-147: Handle errors when converting JSON values

Since bytes_from_move_value now returns a VMResult<Vec<u8>>, ensure that the result is properly handled to capture potential errors.

Update the function to handle the result:

 fn convert_json_value_to_json_value(val: &MoveValue) -> VMResult<JSONValue> {
-    let bz = bytes_from_move_value(val);
+    let bz = bytes_from_move_value(val)?;
     serde_json::from_slice(&bz).map_err(deserialization_error_with_msg)
 }

284-294: LGTM!

The is_json_value and is_json_object functions are implemented correctly. They check if the struct tag represents a JSON value or JSON object by comparing the address, module, and name with the expected values.


131-142: Replace unreachable!() with error handling in bytes_from_move_value

Using unreachable!() may cause the program to panic if unexpected data is encountered. It's safer to handle these cases by returning an appropriate error to prevent potential crashes.

Apply this diff to improve error handling:

-fn bytes_from_move_value(val: &MoveValue) -> Vec<u8> {
+fn bytes_from_move_value(val: &MoveValue) -> VMResult<Vec<u8>> {
     match val {
         MoveValue::Vector(bytes_val) => bytes_val
             .iter()
             .map(|byte_val| match byte_val {
-                MoveValue::U8(byte) => *byte,
-                _ => unreachable!(),
+                MoveValue::U8(byte) => Ok(*byte),
+                _ => Err(deserialization_error_with_msg("Expected U8 in vector")),
             })
-            .collect::<Vec<u8>>(),
-        _ => unreachable!(),
+            .collect::<VMResult<Vec<u8>>>(),
+        _ => Err(deserialization_error_with_msg("Expected vector of U8s")),
     }
 }

Also, update the calls to bytes_from_move_value to handle the VMResult.

crates/json/src/move_to_json.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@sh-cha sh-cha left a comment

Choose a reason for hiding this comment

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

LGTM

@beer-1 beer-1 merged commit d4b03a1 into main Sep 20, 2024
6 checks passed
@beer-1 beer-1 deleted the feat/json-object branch September 20, 2024 09:13
@coderabbitai coderabbitai bot mentioned this pull request Oct 11, 2024
@coderabbitai coderabbitai bot mentioned this pull request Nov 1, 2024
11 tasks
@coderabbitai coderabbitai bot mentioned this pull request Nov 14, 2024
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants