pydantic-xml icon indicating copy to clipboard operation
pydantic-xml copied to clipboard

Ordered search mode with List[Union[]]

Open shevernitskiy opened this issue 1 year ago • 6 comments

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?

shevernitskiy avatar Sep 16 '23 16:09 shevernitskiy

@shevernitskiy hi,

Could you provide the document you are trying to parse?

dapper91 avatar Sep 16 '23 17:09 dapper91

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

doc.xml.txt

shevernitskiy avatar Sep 16 '23 17:09 shevernitskiy

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.

dapper91 avatar Sep 18 '23 19:09 dapper91

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?

shevernitskiy avatar Sep 19 '23 04:09 shevernitskiy

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.

dapper91 avatar Sep 20 '23 19:09 dapper91

Thanks!

shevernitskiy avatar Sep 21 '23 04:09 shevernitskiy