-
Notifications
You must be signed in to change notification settings - Fork 68
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
Option
handling is verbose
#161
Comments
In
When I started |
Thanks for the response. I just checked on the playground, at least with #[derive(Debug, serde::Deserialize)]
struct Foo {
item: Option<i32>,
}
fn main() {
dbg!(serde_json::from_str::<Foo>("{}")
.unwrap());
} This succeeds, printing out |
This is in support of #161, illustrating how defaults combine.
Today I learned. Knowing that, I'm certainly willing to entertain a PR that adds this, though I may sit agonizing about it for a week before merging. A couple things to consider:
#[derive(FromMeta)]
#[darling(default)]
struct Example {
name: Option<String>,
age: u8,
}
impl Default for Example {
fn default() -> Self {
Self {
name: Some("Alice".into()),
age: 0,
}
}
} If given I've added some tests that illustrate behavior of stacking defaults, and the PR will need to extend those to show it behaves properly in those cases. |
I'd suggest implementing this as a new method on impl Initializer {
fn option_aware_default_expression(&self) -> Option<Cow<'a, DefaultExpression>> {
self.0.default_expression.map(Cow::Borrowed).or_else(|| /* ... */)
}
} The You'd then replace the second reference to Edit: I realized that the |
This is in support of #161, illustrating how defaults combine.
I can help with that part. The derive creates an internal struct which wraps every field in one extra layer of |
@jonasbb that's extremely helpful; thanks for sharing it. As usual, I suppose we could map that to |
This removes the need to use #[darling(default)] on options in deriving structs. Fixes #161
The change for this is in review. I'm trying to decide if |
This removes the need to use #[darling(default)] on options in deriving structs. Fixes #161
Expectations:
Based on the phrasing in the documentation and the fact that the crate says it is inspired by serde,
Option
would default toNone
in a derive if the item isn't present.Reality:
Missing items error, and need an explicit
#[darling(default)]
on them. This seems non-obvious, and at the least I think the documentation forOption
inFromMeta
should be clarified, as while the current documentation technically describes the behavior accurately, I misinterpreted it as I assumed it would work the same assyn
.The text was updated successfully, but these errors were encountered: