Convert AnyElement to known type. #214
-
|
Hi there, is there a better way to do this ? Converting a AnyElement to a known Type ? Thank you. fn parse_get_parameter_values_response(
element: AnyElement,
) -> Result<GetParameterValuesResponse, Box<dyn std::error::Error>> {
let mut buffer = Vec::new();
let mut writer = Writer::new(&mut buffer);
element.serialize("GetParameterValuesResponse", &mut writer)?;
let xml = String::from_utf8(buffer)?;
let mut reader = SliceReader::new(xml.as_str());
let schema_response = GetParameterValuesResponse::deserialize(&mut reader)?;
Ok(schema_response)
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
Hey @sirimhrzn, you can replace the element in the containing type from For a rough setup have a look to the Here is a rough untested (!) code snippet: // Get the identifier for the target type
let target_ident = IdentTriple::from((IdentType::Type, "GetParameterValuesResponse"));
// Get the identifier for the containing type
let ident = IdentTriple::from((IdentType::Type, "ContainingType"));
let ident = ident.resolve(schemas)?;
// Get the containing type
let ty = types.items.get_mut(&ident).unwrap();
let MetaTypeVariant::Complex(ty) = &ty.variant else { panic!(); }; // I assume your type is a complex type
let content = ty.content.clone().unwrap();
// Get the content type
let ty = types.items.get_mut(&ident).unwrap();
let MetaTypeVariant::Sequence(ty) = &ty.variant else { panic!(); }; // I assume the content of your type is a sequence
// Loop through the elements
for el in &mut *ty.elements {
if el.ident.name.as_str() == "my-element" {
el.variant = ElementMetaVariant::Type { // For simplicity I assign a new `ElementMetaVariant` here
type_: target_ident, // A better approach would be to check if it is a `::Type`
mode: ElementMode::Element, // and then only assign `type_`.
}
}
}Now the generator should generate a Let me know if you need more help 😊 |
Beta Was this translation helpful? Give feedback.
-
|
Hi @Bergmann89, fn typed_envelope_header(schemas: &Schemas, mut types: MetaTypes) -> Result<MetaTypes, Error> {
let container = IdentTriple::from((
IdentType::Type,
Some(NamespaceIdent::namespace(
b"http://schemas.xmlsoap.org/soap/envelope/",
)),
"Header",
))
.resolve(schemas)
.unwrap();
let headers = [
("ID", "urn:dslforum-org:cwmp-1-0"),
("HoldRequests", "urn:dslforum-org:cwmp-1-0"),
("SessionTimeout", "urn:dslforum-org:cwmp-1-2"),
("SupportedCWMPVersions", "urn:dslforum-org:cwmp-1-2"),
("UseCWMPVersion", "urn:dslforum-org:cwmp-1-2"),
]
.into_iter()
.map(|(name, ns)| {
IdentTriple::from((
IdentType::ElementType,
Some(NamespaceIdent::namespace(ns.as_bytes())),
name,
))
.resolve(schemas)
.unwrap()
})
.map(|ident| {
ElementMeta::new(
ident.clone(),
ident.clone(),
ElementMode::Element,
FormChoiceType::Qualified,
)
})
.collect::<Vec<_>>();
let ty = types.items.get_mut(&container).unwrap();
ty.variant = MetaTypeVariant::Choice(GroupMeta {
is_mixed: false,
elements: ElementsMeta(headers),
});
Ok(types)
}With this, I was able to generate enums as I needed to. // generated type
pub enum HeaderType {
Id(super::tns::IdElementType),
HoldRequests(super::tns::HoldRequestsElementType),
SessionTimeout(super::cwmp_13::SessionTimeoutElementType),
SupportedCwmpVersions(super::cwmp_14::SupportedCwmpVersionsElementType),
UseCwmpVersion(super::cwmp_14::UseCwmpVersionElementType),
}Honestly, thank you so much for helping out and for this library as well. |
Beta Was this translation helpful? Give feedback.
Hey @sirimhrzn,
you can replace the element in the containing type from
AnyElementtoGetParameterValuesResponse.For a rough setup have a look to the
custom_namesexample. But instead of changing thedisplay_nameof your type, you can loop through it's elements and change the type to what you need.Here is a rough untested (!) code snippet: