def _serialize_xml_model()

in alibabacloud_oss_v2/serde.py [0:0]


def _serialize_xml_model(obj, root: Optional[str] = None) -> ET.Element:
    """serialize xml model
    """

    if root is not None and len(root) > 0:
        name = root
    else:
        name = obj.__class__.__name__
        xml_map = getattr(obj, '_xml_map', None)
        if xml_map is not None:
            name = xml_map.get('name', name)

    elem = ET.Element(name)

    attributes = getattr(obj, '_attribute_map')
    for attr, attr_desc in attributes.items():
        if attr_desc.get('tag', '') != 'xml':
            continue
        attr_value = getattr(obj, attr)
        attr_key = attr_desc.get('rename', attr)
        attr_type = attr_desc.get('type', '')
        if attr_value is not None:
            if isinstance(attr_value, Model):
                model = cast(Model, attr_value)
                elem.append(_serialize_xml_model(model))
            elif isinstance(attr_value, list):
                elem.extend([_serialize_xml_any(attr_key, a, attr_type)
                            for a in attr_value])
            else:
                elem.append(_serialize_xml_any(
                    attr_key, attr_value, attr_type))
    return elem