mirror of https://github.com/boa-dev/boa.git
Browse Source
* Allow deserialization of missing objects properties into Option<> Currently the TryFromJs derive macro requires that all properties be defined in the object, whether they are declared as Option<> or not. This commit makes it so if the field is not found in the JsObject, we try to deserialize undefined. This is more in line with JavaScript behaviour. * Fix cargo fmt * Fix clippies * Empty commit to trigger rerun of workflow * Remove circular dependency between boa_engine and boa_macros * Missing "::" in macro --------- Co-authored-by: jedel1043 <jedel0124@gmail.com>pull/3775/head
Hans Larsen
8 months ago
committed by
GitHub
6 changed files with 78 additions and 24 deletions
@ -0,0 +1,67 @@ |
|||||||
|
#![allow(unused_crate_dependencies)] |
||||||
|
|
||||||
|
use boa_engine::value::TryFromJs; |
||||||
|
use boa_engine::Source; |
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, TryFromJs)] |
||||||
|
struct Deserialize { |
||||||
|
required: String, |
||||||
|
optional: Option<String>, |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn optional_missing_try_from_js() { |
||||||
|
let mut context = boa_engine::Context::default(); |
||||||
|
let value = context |
||||||
|
.eval(Source::from_bytes( |
||||||
|
r#" |
||||||
|
let empty = { |
||||||
|
"required":"foo", |
||||||
|
}; |
||||||
|
empty |
||||||
|
"#, |
||||||
|
)) |
||||||
|
.unwrap(); |
||||||
|
|
||||||
|
let deserialized: Deserialize = Deserialize::try_from_js(&value, &mut context).unwrap(); |
||||||
|
assert_eq!(deserialized.required, "foo"); |
||||||
|
assert_eq!(deserialized.optional, None); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn optional_try_from_js() { |
||||||
|
let mut context = boa_engine::Context::default(); |
||||||
|
let value = context |
||||||
|
.eval(Source::from_bytes( |
||||||
|
r#" |
||||||
|
let empty = { |
||||||
|
"required": "foo", |
||||||
|
"optional": "bar", |
||||||
|
}; |
||||||
|
empty |
||||||
|
"#, |
||||||
|
)) |
||||||
|
.unwrap(); |
||||||
|
|
||||||
|
let deserialized: Deserialize = Deserialize::try_from_js(&value, &mut context).unwrap(); |
||||||
|
assert_eq!(deserialized.required, "foo"); |
||||||
|
assert_eq!(deserialized.optional, Some("bar".to_string())); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn required_missing_try_from_js() { |
||||||
|
let mut context = boa_engine::Context::default(); |
||||||
|
let value = context |
||||||
|
.eval(Source::from_bytes( |
||||||
|
r" |
||||||
|
let value = {}; |
||||||
|
value |
||||||
|
", |
||||||
|
)) |
||||||
|
.unwrap(); |
||||||
|
|
||||||
|
assert!( |
||||||
|
Deserialize::try_from_js(&value, &mut context).is_err(), |
||||||
|
"foo" |
||||||
|
); |
||||||
|
} |
Loading…
Reference in new issue