dapper91 / pydantic-xml

python xml for humans
https://pydantic-xml.readthedocs.io
The Unlicense
141 stars 14 forks source link

Ordered search mode with List[Union[]] #116

Open shevernitskiy opened 9 months ago

shevernitskiy commented 9 months ago

I have the model, which has field with type:

children: list[
    Union[
        FieldBool,
        FieldInt8,
        FieldInt16,
        FieldInt32,
        FieldInt64,
        FieldUint8,
        FieldUint16,
        FieldUint32,
        FieldUint64,
        FieldEnum,
        FieldBitfield,
        FieldCompound,
        FieldStaticArray,
        FieldVector,
        FieldDeque,
        FieldSet,
        FieldBitVector,
        FieldPointer,
        FieldDfFlagArray,
        FieldDfStaticFlagArray,
        FieldDfArray,
        FieldDfLinkedList,
        FieldDfLinkedListType,
        FieldDfOtherVectorsType,
        FieldString,
        FieldPtrString,
        FieldStaticString,
    ]
] = element(default=None)

I need to parse tag and save the order of children in list. unordered search mode found 100 items, but ordered found only 1. I mean, i don't know exact order of children, but i want to keep it. How can i do it?

dapper91 commented 9 months ago

@shevernitskiy hi,

Could you provide the document you are trying to parse?

shevernitskiy commented 9 months ago

It is just a chunk of big document. Erase .txt.

doc.xml.txt

dapper91 commented 9 months ago

The problem is that in ordered search mode Union typed field is bound to the first matched element skipping all the elements before.

In your example FieldBool is not found, then FieldInt8 is bound to the <int8_t name='cached_glowtile_type'/> skipping all the elements before and the search continues after it. Therefore only 1 element is found in ordered search mode. In unordered search mode the elements order is not kept and right now there is not way to keep it.

shevernitskiy commented 9 months ago

The problem is that in ordered search mode Union typed field is bound to the first matched element skipping all the elements before.

In your example FieldBool is not found, then FieldInt8 is bound to the <int8_t name='cached_glowtile_type'/> skipping all the elements before and the search continues after it. Therefore only 1 element is found in ordered search mode. In unordered search mode the elements order is not kept and right now there is not way to keep it.

I see. That sad. As you can see there is cpp struct in xml, so the order matters. Could the unordered_sequenced mode be the possible future improvement?

dapper91 commented 9 months ago

Yes, I will think about it. It is not easy to implement the search keeping elements order due to internal implementation details, but I will try to find a workaround.

shevernitskiy commented 9 months ago

Thanks!