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

Allow deserialization of missing objects properties into Option<> #3767

Merged
merged 6 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ serde = "1.0.197"
static_assertions = "1.1.0"
textwrap = "0.16.0"
thin-vec = "0.2.13"
time = {version = "0.3.34", no-default-features = true, features = ["local-offset", "large-dates", "wasm-bindgen", "parsing", "formatting", "macros"]}
time = {version = "0.3.34", default-features = false, features = ["local-offset", "large-dates", "wasm-bindgen", "parsing", "formatting", "macros"]}
tinystr = "0.7.5"
log = "0.4.21"
simple_logger = "4.3.3"
Expand Down
31 changes: 10 additions & 21 deletions core/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,30 +418,19 @@ fn generate_conversion(fields: FieldsNamed) -> Result<proc_macro2::TokenStream,
.map_err(|err| vec![err])?;
}

final_fields.push(quote! {
let #name = match props.get(&::boa_engine::js_string!(#name_str).into()) {
Some(pd) => pd.value().ok_or_else(|| ::boa_engine::JsError::from(
boa_engine::JsNativeError::typ().with_message(#error_str)
hansl marked this conversation as resolved.
Show resolved Hide resolved
))?.clone().try_js_into(context)?,
None => ::boa_engine::JsValue::undefined().try_js_into(context)?,
};
});

if let Some(method) = from_js_with {
let ident = Ident::new(&method.value(), method.span());
final_fields.push(quote! {
let #name = #ident(props.get(&::boa_engine::js_string!(#name_str).into()).ok_or_else(|| {
::boa_engine::JsError::from(
boa_engine::JsNativeError::typ().with_message(#error_str)
)
})?.value().ok_or_else(|| {
::boa_engine::JsError::from(
boa_engine::JsNativeError::typ().with_message(#error_str)
)
})?, context)?;
});
} else {
final_fields.push(quote! {
let #name = props.get(&::boa_engine::js_string!(#name_str).into()).ok_or_else(|| {
::boa_engine::JsError::from(
boa_engine::JsNativeError::typ().with_message(#error_str)
)
})?.value().ok_or_else(|| {
::boa_engine::JsError::from(
boa_engine::JsNativeError::typ().with_message(#error_str)
)
})?.clone().try_js_into(context)?;
let #name = #ident(&#name, context)?;
});
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ rust-version.workspace = true

[dev-dependencies]
trybuild = "1.0.90"
boa_macros.workspace = true
boa_engine.workspace = true

[lints]
Expand Down
File renamed without changes.
67 changes: 67 additions & 0 deletions tests/macros/tests/optional.rs
Original file line number Diff line number Diff line change
@@ -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