media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
179 stars 60 forks source link

Using flatten with namespaces #138

Open luke-biel opened 2 years ago

luke-biel commented 2 years ago

I will take liberty with opening new issue with this one. I'm looking for an information how to use namespaces with flatten. Eg.:

use yaserde_derive::YaDeserialize;

#[derive(Default, YaDeserialize)]
#[yaserde(
    root = "Root",
    prefix = "nsa",
    namespace = "nsa: http://nsa.com",
    namespace = "nsb: http://nsb.com"
)]
struct Root {
    #[yaserde(rename = "Root.basic", prefix = "nsa", text)]
    basic: String,
    #[yaserde(flatten)]
    child: Child,
}

#[derive(Default, YaDeserialize)]
struct Child {
    #[yaserde(rename = "Child.param", prefix = "nsa", text)]
    param: String,
    #[yaserde(flatten)]
    child2: Child2,
}

#[derive(Default, YaDeserialize)]
struct Child2 {
    #[yaserde(rename = "Child2.param", prefix = "nsb", text)]
    param: String,
}

#[cfg(test)]
mod tests {
    use crate::Root;

    #[test]
    fn parses() {
        const XML: &str = r##"
        <nsa:Root xmlns:nsa="http://dupa.com" xmlns:nsb="http://nsb.com" >
            <nsa:Root.basic>Value</nsa:Root.basic>
            <nsa:Child.param>ValueC</nsa:Child.param>
            <nsb:Child2.param>ValueD</nsb:Child2.param>
        </nsa:Root>
        "##;

        let _: Root = yaserde::de::from_str(XML).unwrap();
    }
}

Doesn't work.

As a background: I'm working on rust code generated from inheritance model. I would prefer to avoid manual flattening of these structs.