Skip to content

Commit

Permalink
json option none == null
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Apr 26, 2024
1 parent 4bf477e commit 7acc926
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
6 changes: 3 additions & 3 deletions crates/e2e-move-tests/src/tests/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn test_output() {
vec![],
ExpectedOutput::new(
VMStatus::Executed,
Some("[\"123\"]".to_string()),
Some("\"123\"".to_string()),
None,
None,
),
Expand All @@ -72,7 +72,7 @@ fn test_output() {
vec![],
ExpectedOutput::new(
VMStatus::Executed,
Some("[[\"123\"]]".to_string()),
Some("[\"123\"]".to_string()),
None,
None,
),
Expand All @@ -84,7 +84,7 @@ fn test_output() {
"0x2::test::option_none",
vec![],
vec![],
ExpectedOutput::new(VMStatus::Executed, Some("[]".to_string()), None, None),
ExpectedOutput::new(VMStatus::Executed, Some("null".to_string()), None, None),
);
tests.push(test_option_none);

Expand Down
28 changes: 10 additions & 18 deletions crates/vm/src/json/json_to_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,16 @@ pub(crate) fn convert_json_value_to_move_value<S: StateView>(
let full_name = format!("{}::{}", id.module_id.short_str_lossless(), id.name);
match full_name.as_str() {
"0x1::option::Option" => {
let json_vals = json_val
.as_array()
.ok_or_else(deserialization_error)?
.to_owned();

if json_vals.is_empty() {
if json_val.is_null() {
return Ok(MoveValue::Vector(vec![]));
} else if json_vals.len() == 1 {
let json_val = json_vals.into_iter().next().unwrap();
return Ok(MoveValue::Vector(vec![convert_json_value_to_move_value(
state_view,
ty,
json_val,
depth + 1,
)?]));
}

return Err(deserialization_error_with_msg("invalid option value"));
return Ok(MoveValue::Vector(vec![convert_json_value_to_move_value(
state_view,
ty,
json_val,
depth + 1,
)?]));
}
"0x1::object::Object" => {
let addr = AccountAddress::from_hex_literal(
Expand Down Expand Up @@ -614,15 +606,15 @@ mod json_arg_testing {
ty_args: Arc::new(vec![Type::Address]),
ability: AbilityInfo::struct_(AbilitySet::ALL),
};
let arg = b"[\"0x1\"]";
let arg = b"\"0x1\"";
let result = deserialize_json_args(&state_view, &ty, arg).unwrap();
assert_eq!(
result,
bcs::to_bytes(&vec!["0x1".parse::<AccountAddress>().unwrap()]).unwrap()
);

// invalid inner value
let arg = b"[\"0xgg\"]";
let arg = b"\"0xgg\"";
_ = deserialize_json_args(&state_view, &ty, arg).unwrap_err();
}

Expand All @@ -639,7 +631,7 @@ mod json_arg_testing {
ty_args: Arc::new(vec![Type::Address]),
ability: AbilityInfo::struct_(AbilitySet::ALL),
};
let arg = b"[]";
let arg = b"null";
let result = deserialize_json_args(&state_view, &ty, arg).unwrap();
assert_eq!(
result,
Expand Down
11 changes: 4 additions & 7 deletions crates/vm/src/json/move_to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,9 @@ fn convert_option_to_json_value(val: &MoveValue, depth: usize) -> VMResult<JSONV
Ok(match val {
MoveValue::Vector(elem) => {
if elem.is_empty() {
JSONValue::Array(vec![])
JSONValue::Null
} else {
JSONValue::Array(vec![convert_move_value_to_json_value(
elem.first().unwrap(),
depth + 1,
)?])
convert_move_value_to_json_value(elem.first().unwrap(), depth + 1)?
}
}
_ => unreachable!(),
Expand Down Expand Up @@ -267,7 +264,7 @@ mod move_to_json_tests {
)],
});
let val = convert_move_value_to_json_value(&mv, 1).unwrap();
assert_eq!(val, json!([123u8]));
assert_eq!(val, json!(123u8));

// option none
let mv = MoveValue::Struct(MoveStruct::WithTypes {
Expand All @@ -280,7 +277,7 @@ mod move_to_json_tests {
fields: vec![(ident_str!("vec").into(), MoveValue::Vector(vec![]))],
});
let val = convert_move_value_to_json_value(&mv, 1).unwrap();
assert_eq!(val, json!([]));
assert_eq!(val, json!(null));

// decimal128
const DECIMAL_SCALE: u128 = 1_000_000_000_000_000_000;
Expand Down
Binary file modified precompile/binaries/stdlib/minitswap.mv
Binary file not shown.

0 comments on commit 7acc926

Please sign in to comment.