-
Notifications
You must be signed in to change notification settings - Fork 14
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
Conversation
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 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. WalkthroughThe changes involve significant enhancements to the JSON handling capabilities within the codebase. New structures such as Changes
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
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 using PR comments)
Other keywords and placeholders
CodeRabbit Configuration 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: 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 onkey_bytes
, which is already avector<u8>
. Sincekey_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_bytesAlso applies to: 51-51
precompile/modules/minitia_stdlib/sources/json.move (3)
12-14
: Consider using a map data structure for efficient key-value storageThe
JSONObject
currently uses avector<Element>
to store its elements, which results in linear time complexity forget_elem
andset_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 theuse_elem
functionThe
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 testsIn the test cases where
get_elem
is used,option::extract
is called without checking if the option issome
. While tests are expected to pass, consider pattern matching on theOption
to handle thenone
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 serializationThe added tests for
JSONValue
andJSONObject
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
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 ofJSONValue
andJSONObject
support.The commented-out code handling the conversion of
JSONValue
andJSONObject
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
andJSONObject
types:precompile/modules/minitia_stdlib/sources/json.move (2)
23-28
: Functionkeys
is correctly implementedThe
keys
function effectively retrieves the list of keys from theJSONObject
. The use ofvector::map_ref
andstring::utf8
correctly transforms the keys intoString
types.
171-179
: Tests verify JSON marshaling and unmarshaling accuratelyThe tests at the end of the file correctly verify that marshaling and unmarshaling functions for
JSONValue
andJSONObject
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 handleJSONValue
andJSONObject
typesThe addition of checks for
is_json_value
andis_json_object
enhances the conversion functionality to support JSON types appropriately.
284-295
: Addition of helper functionsis_json_value
andis_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 ofJSONValue
andJSONObject
Deserialization Looks GoodThe code correctly handles deserialization of
JSONValue
andJSONObject
by serializing JSON values into byte vectors and constructing the appropriate Move structs. Error handling is properly managed usingmap_err
with descriptive messages.
623-646
: Unit Test forJSONValue
Deserialization is CorrectThe unit test
test_deserialize_json_to_value_json_value
accurately verifies the deserialization of aJSONValue
. 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 forJSONObject
Deserialization is AccurateThe unit test
test_deserialize_json_to_value_json_object
effectively tests the deserialization of aJSONObject
. 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.
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: 2
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
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
, andElement
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 theuse_elem
workaround is still necessary.The
keys
function correctly retrieves the keys from theJSONObject
and returns them as a vector of strings. The usage ofvector::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 theJSONObject
and returns it as anOption<T>
. The usage ofvector::find
is appropriate for searching the elements for the matching key, and the function handles the case when the key is not found by returningNone
.However, the function calls
unmarshal<T>(elem.value)
, which may fail ifelem.value
cannot be deserialized into typeT
. To enhance robustness, consider handling possible errors fromunmarshal
and returning an appropriate value in such cases.
46-63
: LGTM!The
set_elem
function correctly sets or overwrites an element in theJSONObject
based on the provided key and value. The usage ofvector::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 theJSONObject
.
66-68
: Verify if theuse_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 forget_elem
andset_elem
ensure that the functions work as expected. Overall, the test cases provide good coverage for the functionality implemented in thejson
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
, andElement
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
andset_elem
functions effectively.
31-44
: Consider usingvector::equals
for reliable key comparison.In the
get_elem
function, the comparisonelem.key == *key_bytes
relies on direct vector comparison. While this may work if vector equality compares contents, it's safer to usevector::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
andJSONObject
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 valuesSince
bytes_from_move_value
now returns aVMResult<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
andis_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
: Replaceunreachable!()
with error handling inbytes_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 theVMResult
.
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.
LGTM
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...
!
in the type prefix if API or client breaking changeReviewers 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
New Features
JSONValue
andJSONObject
types in JSON deserialization and conversion processes.keys
,get_elem
, andset_elem
.initia_std::json
andminitia_std::json
modules for better JSON data handling.Bug Fixes
Tests