-
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
Allow string literals for meta items of type syn::Expr #229
Comments
I believe I'm running into this right now. I have these attributes:
But they are being parsed as Edit: Looks like this is by design? Line 279 in 6f15cc3
|
Been thinking about this a bit more. What if there was another derive, like For context, I'm using |
I'm leaning towards a solution that uses I don't want to forbid quotation marks in places where they don't cause ambiguity; if nothing else, as long as serde has quotation marks around its paths I'm going to keep typing them by reflex, and breaking that unnecessarily would annoy maintainers and users of darling-powered macros. That means the I think that syn::Expr is unique in having this problem: syn::Lit should be fine since it was previously valid in this position in syn 1, so we didn't have this issue of needing to "smuggle" content with quotation marks. @milesj how about we aim to introduce a helper function in 0.20.1 that you can use to get unstuck, and then continue exploring the problem space to find the best long-term default? |
@milesj can you try these out for your use-case and make sure they work as-expected? use syn::{Expr, Meta};
use crate::{Error, FromMeta};
pub fn preserve_str_literal(meta: &Meta) -> crate::Result<Expr> {
match meta {
Meta::Path(_) => return Err(Error::unsupported_format("path").with_span(meta)),
Meta::List(_) => return Err(Error::unsupported_format("list").with_span(meta)),
Meta::NameValue(nv) => Ok(nv.value.clone()),
}
}
pub fn parse_str_literal(meta: &Meta) -> crate::Result<Expr> {
match meta {
Meta::Path(_) => return Err(Error::unsupported_format("path").with_span(meta)),
Meta::List(_) => return Err(Error::unsupported_format("list").with_span(meta)),
Meta::NameValue(nv) => {
if let Expr::Lit(expr_lit) = &nv.value {
Expr::from_value(&expr_lit.lit)
} else {
Ok(nv.value.clone())
}
}
}
} The expectation here would be that you add |
@TedDriggs Looks to work 🎉 I did have to wrap the return in |
@milesj ugh, that's kind of annoying - I don't really want to make people have to create one of these per wrapper type they want (e.g. Edit: This seems to be okay, since the #[darling(with = "darling::util::parse_expr::preserve_str_literal", map = "Some")] |
Just chiming in that it's been working great. Thanks for the quick turn around 👍 |
At the moment I am rewriting nutype parsing logic to use |
@greyblake have you tried the |
@TedDriggs Thanks, I overlooked that. So having this pub fn preserve_str_literal(meta: &Meta) -> darling::Result<Option<Expr>> {
match meta {
Meta::Path(_) => return Err(darling::Error::unsupported_format("path").with_span(meta)),
Meta::List(_) => return Err(darling::Error::unsupported_format("list").with_span(meta)),
Meta::NameValue(nv) => Ok(Some(nv.value.clone())),
}
} and #[darling(with = "preserve_str_literal")]
pub default: Option<Expr>, Works together as I wish, string literals are correctly parsed 👍 |
…rn = ...))]` For compatibility with the validator crate, when the `pattern` form is given a string literal, that string's contents will be parsed as an expression. This is due to the behaviour of the darling crate (TedDriggs/darling#229). I'm concerned that this behaviour may change in a future version of darling/validator, so to minimise its exposure in schemars's public API, I'm restricting its usage now.
Right now, when
darling
encounters a string literal for a field of typesyn::Expr
, it will attempt to parse the string contents and put that result into the struct. This means it's impossible to have a field that takes an arbitrary expression that may happen to be a string literal.Proposed Fix
None yet; please comment on this issue if you're relying on the current behavior or want to rely on the behavior described above.
The text was updated successfully, but these errors were encountered: