Open naftulikay opened 6 years ago
Based on https://github.com/serde-rs/serde/issues/1098#issuecomment-346706565, you can do this:
#[derive(Deserialize)]
struct Test {
#[serde(default, deserialize_with = "nullable_default")]
default_field: DefaultField,
}
#[derive(Deserialize, Default)]
struct DefaultField {
u8: u8,
}
fn nullable_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: Default + Deserialize<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_else(T::default))
}
This will use default for type if the field is missing or null.
I apologize for my delay on this, new job, excuses, etc.
Is there anyone I can help move this forward? I'd love to help
I've recently spent some time doing this for rust-aws-lambda
over here: https://github.com/srijs/rust-aws-lambda/tree/master/aws_lambda_gateway, which then led to cool things like being able to run tower-web
apps on lambda.
If you're interested, I'd be happy to see if this can be generalized so that it's useful for crowbar as well. Between this and what's being discussed in #32, maybe we can find a way to have an ecosystem of crates that can be used from either runtime?
Tests are broken until I can get a deserializer written which can deserialize
Value::Null
intoDefault::default()
forT: Default
.