Skip to content
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

duplicate field xxxx #427

Closed
CrayfishGo opened this issue Jul 18, 2022 · 5 comments
Closed

duplicate field xxxx #427

CrayfishGo opened this issue Jul 18, 2022 · 5 comments
Labels
question serde Issues related to mapping from Rust types to XML

Comments

@CrayfishGo
Copy link

CrayfishGo commented Jul 18, 2022

Here is my xml code

<?xml version="1.0" encoding="utf-8" ?>
<TypeDictionary Name="TypeDictionary">
    <Import Namespace="http://opcfoundation.org/BinarySchema/"/>
    <StructuredType Name="XmlElement">
        <Documentation>An XML element encoded as a UTF-8 string.</Documentation>
        <Field Name="Length" TypeName="Int32"/>
        <Field Name="Value" TypeName="Char" LengthField="Length"/>
    </StructuredType>
    <EnumeratedType Name="NodeIdType" LengthInBits="6">
        <Documentation>The possible encodings for a NodeId value.</Documentation>
        <EnumeratedValue Name="TwoByte" Value="0"/>
        <EnumeratedValue Name="FourByte" Value="1"/>
        <EnumeratedValue Name="Numeric" Value="2"/>
        <EnumeratedValue Name="String" Value="3"/>
        <EnumeratedValue Name="Guid" Value="4"/>
        <EnumeratedValue Name="ByteString" Value="5"/>
    </EnumeratedType>
    <StructuredType Name="TwoByteNodeId">
        <Field Name="Identifier" TypeName="Byte"/>
    </StructuredType>
</TypeDictionary>

Here is my some struts

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct TypeDictionary {
    #[serde(rename = "Name")]
    name: String,

    #[serde(rename = "StructuredType")]
    struct_types: Vec<StructType>,

    #[serde(rename = "EnumeratedType")]
    enum_types: Vec<EnumType>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
 struct StructType {
    #[serde(rename = "Name")]
    name: String,

    #[serde(rename = "Documentation")]
    documentation: String,

    #[serde(rename = "BaseType")]
    base_type: Option<String>,

    #[serde(rename = "Field")]
    fields: Vec<StructField>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct StructField {
    #[serde(rename = "Name")]
    name: String,

    #[serde(rename = "TypeName")]
    type_name: String,

    #[serde(rename = "Length")]
    length: Option<String>,

    #[serde(rename = "LengthField")]
    length_field: Option<String>,

    #[serde(rename = "SwitchField")]
    switch_field: Option<String>,

    #[serde(rename = "SwitchValue")]
    switch_value: Option<String>,

    #[serde(skip)]
    is_enum: bool,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct EnumType {
    #[serde(rename = "Name")]
    name: String,

    #[serde(rename = "Documentation")]
    documentation: String,

    #[serde(rename = "LengthInBits")]
    length_in_bits: Option<String>,

    #[serde(rename = "EnumeratedValue")]
    enumerated_values: Vec<EnumValue>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct EnumValue {
    #[serde(rename = "Name")]
    name: String,

    #[serde(rename = "Value")]
    value: String,
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::Path;
    use quick_xml::DeError;
    use crate::bsd::code_gen::{TypeDictionary};

    #[test]
    fn it_works() {
        let p = Path::new("../schemas/1.0.4/Opc.Ua.Types.bsd.xml");
        let data = fs::read_to_string(p);
        let a: Result<TypeDictionary, DeError> = quick_xml::de::from_str(data.unwrap().as_str());
        println!("{:#?}", a);
        ()
    }
}

But there is an error:

Err(
Custom(
"duplicate field StructuredType",
),
)

So, could you help me how to fix this ?

@Mingun
Copy link
Collaborator

Mingun commented Jul 18, 2022

What version you are using? This error should be fixed in master, but to correctly process your XML you need to enable the overlapped-lists feature. That test is example of your case:

quick-xml/tests/serde-de.rs

Lines 1640 to 1671 in 068b36e

#[test]
fn overlapped() {
let data = from_str::<Pair>(
r#"
<root>
<item/>
<element/>
<item/>
<element/>
<item/>
</root>
"#,
);
#[cfg(feature = "overlapped-lists")]
assert_eq!(
data.unwrap(),
Pair {
item: vec![(), (), ()],
element: vec![(), ()],
}
);
#[cfg(not(feature = "overlapped-lists"))]
match data {
Err(DeError::Custom(e)) => assert_eq!(e, "duplicate field `item`"),
e => panic!(
r#"Expected Err(Custom("duplicate field `item`")), got {:?}"#,
e
),
}
}

@Mingun Mingun added question serde Issues related to mapping from Rust types to XML labels Jul 18, 2022
@CrayfishGo
Copy link
Author

Here is my dependencies

[dependencies]
serde_derive = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
quick-xml = { version = "0.23.0-alpha3", features = [ "serialize" ] }

@Mingun
Copy link
Collaborator

Mingun commented Jul 18, 2022

So for now you have only one variant -- use the git version (it is better to use some concrete hash, because master change frequently and in incompatible way nowadays). The 0.24 release should be soon, I hope it's a matter of a couple of weeks.

I close this issue as it should be fixed in master. If you have additional questions, feel free to ask them.

@Mingun Mingun closed this as completed Jul 18, 2022
@CrayfishGo
Copy link
Author

Thanks very much!!!

It works now

[dependencies]
serde_derive = "1.0"
serde = { version = "1.0", features = ["derive"] }
quick-xml = { git = "https://github.com/tafia/quick-xml", features = ["serialize", "overlapped-lists"] }

@Mingun
Copy link
Collaborator

Mingun commented Jul 18, 2022

Glad to hear. As I said, the master branch changes their API almost every day nowadays, so I recommend you to point out to some specific commit, until we release 0.24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants