-
-
Notifications
You must be signed in to change notification settings - Fork 791
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
#[serde(flatten)] attribute support for enum variants #1402
Comments
It looks like this is similar to #1350 where you suggest manually flattening, but that would be fairly cumbersome in this case ( |
Seems reasonable! I would be interested in supporting this better. I have also occasionally wanted #[derive(Deserialize)]
#[serde(tag = "type")]
pub enum ForStatementInit {
VariableDeclaration { kind: String, ... },
#[serde(untagged)]
Expression(Expression),
} |
@dtolnay, what would it take to implement |
I haven't thought through how the feature would work. I would first want to see a handwritten Deserialize impl that behaves the way we would want the code generated for an untagged enum variant to behave. I would schedule 2-6 hours to put together some fully fleshed out handwritten impls to discover any edge cases, depending on your level of familiarity with the deserialization API. |
Meaning a Deserialize impl that enables it without any variant macro? Would tweaking serde-yaml in a branch be sufficient? |
@dtolnay, did you see my previous questions? |
Correct, handwritten meaning without a macro involved. There shouldn't need to be changes to serde-yaml or any other format because you would be touching the enum's Deserialize impl only. |
Ahh, my mistake. I was thinking |
@dtolnay, I threw together a few Deserialize impls. What edge cases am I lacking? |
Trait bounds make this somewhat more complicated. For example: trait Trait<T> {
type Associated;
}
enum E<A, B, C> where A: Trait<C> {
A(A::Associated),
// #[serde(untagged)]
B(B),
}
// not a legal type
#[allow(non_snake_case)]
struct AHelper<A, B, C> where A: Trait<C> {
A: A::Associated,
} Otherwise looks good to me. |
This comment was marked as spam.
This comment was marked as spam.
Hiya, is the last edge case necessary to be supported for this feature to be supported? #[allow(non_snake_case)]
struct AHelper<A, B, C, A_Associated>
where
A: Trait<Associated = A_Associated, C>,
{
A: A_Associated,
} |
The solution could be simpler if toml-rs/toml-rs#334 were fixed. It would be even simpler if `serde` supported flattening enum variants, but it doesn't: serde-rs/serde#1402. Also: - Rename config file field from `allowed_foreign_origins` to `allowed_origins` since being under the CORS section already indicates that it refers to foreign origins.
The solution could be simpler if toml-rs/toml-rs#334 were fixed. It would be even simpler if `serde` supported flattening enum variants, but it doesn't: serde-rs/serde#1402. Also: - Rename config file field from `allowed_foreign_origins` to `allowed_origins` since being under the CORS section already indicates that it refers to foreign origins.
This comment was marked as spam.
This comment was marked as spam.
Which part should I look into to implement such a feature ? |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
Unfortunately, flattening enums with serde is blocked on serde-rs/serde#1402. Signed-off-by: Pablo Sichert <mail@pablosichert.com>
Unfortunately, flattening enums with serde is blocked on serde-rs/serde#1402. Signed-off-by: Pablo Sichert <mail@pablosichert.com>
Hi everyone, we have a PR for resolving this issue that I believe covers all the mentioned use cases: #2403 If one or two ppl could try this PR against their crates and see if their use cases are covered, that would be great! |
Solves my use case. For anyone who didn't know, [patch.crates-io.serde]
git = "https://github.com/dewert99/serde"
branch = "partially_untagged" |
Solved for my case. Really love how much code it saves! |
Thanks! |
Hi!
I have the following use-case for a feature similar to
#[serde(flatten)]
, but for enum variants rather than struct fields. Generally happy to dive intoserde_derive
myself in case you think this would be worth having, but it could also be that I'm trying to model this the wrong way.I'm trying to parse JSON values in the estree format. The format specifies an ECMAScript syntax tree using an inhertance relationship, where generally only the nodes at the bottom of the hierarchy are tagged.
For example, one type of node is
Expression
, but there doesn't exist any node with the "Expression" tag, because the tag is assigned to the "concrete" expression nodes such asIdentifier
orLiteral
. The representation of theExpression
node is basically just a union of all possible JSON representations of its descendant node types.So far, I've been modelling it like this, which has been working fine for the most part:
Now, the difficulty I've been having comes in when I try to mix types that are at the bottom of the node hierarchy with node types that have sub-types (such as
Expression
):In this case, what I really want is for the
ForStatementInit
type to be the result of "adding" the node of typeVariableDeclaration
to the set of representations forExpression
. What ends up happening though of course is that it expects a value tagged with eitherVariableDeclaration
orExpression
.I can work around this by either nesting
VariableDeclaration
into a single-variant enum, and using#[serde(untagged)]
on theForStatementInit
enum, or by manually "flattening" the expression types, like so:However, what I think would be really useful is to be able to do something like this instead, which would apply the same "flattening" transformation in the example above:
Hope this makes somewhat sense, keen to hear if this is something that you think is worth solving in serde itself!
The text was updated successfully, but these errors were encountered: