|
| 1 | +import typing |
| 2 | +from typing import Any, Dict, List, Optional, Tuple |
| 3 | + |
| 4 | +from pydantic_core import core_schema as pcs |
| 5 | + |
| 6 | +from pydantic_xml import errors |
| 7 | +from pydantic_xml.element import XmlElementReader, XmlElementWriter |
| 8 | +from pydantic_xml.serializers.factories import heterogeneous |
| 9 | +from pydantic_xml.serializers.serializer import TYPE_FAMILY, SchemaTypeFamily, Serializer |
| 10 | +from pydantic_xml.typedefs import EntityLocation, Location |
| 11 | + |
| 12 | + |
| 13 | +class ElementSerializer(Serializer): |
| 14 | + @classmethod |
| 15 | + def from_core_schema(cls, schema: pcs.ArgumentsSchema, ctx: Serializer.Context) -> 'ElementSerializer': |
| 16 | + model_name = ctx.model_name |
| 17 | + computed = ctx.field_computed |
| 18 | + inner_serializers: List[Serializer] = [] |
| 19 | + for argument_schema in schema['arguments_schema']: |
| 20 | + param_schema = argument_schema['schema'] |
| 21 | + inner_serializers.append(Serializer.parse_core_schema(param_schema, ctx)) |
| 22 | + |
| 23 | + return cls(model_name, computed, tuple(inner_serializers)) |
| 24 | + |
| 25 | + def __init__(self, model_name: str, computed: bool, inner_serializers: Tuple[Serializer, ...]): |
| 26 | + self._inner_serializer = heterogeneous.ElementSerializer(model_name, computed, inner_serializers) |
| 27 | + |
| 28 | + def serialize( |
| 29 | + self, element: XmlElementWriter, value: List[Any], encoded: List[Any], *, skip_empty: bool = False, |
| 30 | + ) -> Optional[XmlElementWriter]: |
| 31 | + return self._inner_serializer.serialize(element, value, encoded, skip_empty=skip_empty) |
| 32 | + |
| 33 | + def deserialize( |
| 34 | + self, |
| 35 | + element: Optional[XmlElementReader], |
| 36 | + *, |
| 37 | + context: Optional[Dict[str, Any]], |
| 38 | + sourcemap: Dict[Location, int], |
| 39 | + loc: Location, |
| 40 | + ) -> Optional[List[Any]]: |
| 41 | + return self._inner_serializer.deserialize(element, context=context, sourcemap=sourcemap, loc=loc) |
| 42 | + |
| 43 | + |
| 44 | +def from_core_schema(schema: pcs.CallSchema, ctx: Serializer.Context) -> Serializer: |
| 45 | + arguments_schema = typing.cast(pcs.ArgumentsSchema, schema['arguments_schema']) |
| 46 | + for argument_schema in arguments_schema['arguments_schema']: |
| 47 | + param_schema = argument_schema['schema'] |
| 48 | + param_schema, ctx = Serializer.preprocess_schema(param_schema, ctx) |
| 49 | + |
| 50 | + param_type_family = TYPE_FAMILY.get(param_schema['type']) |
| 51 | + if param_type_family not in ( |
| 52 | + SchemaTypeFamily.PRIMITIVE, |
| 53 | + SchemaTypeFamily.MODEL, |
| 54 | + SchemaTypeFamily.MAPPING, |
| 55 | + SchemaTypeFamily.TYPED_MAPPING, |
| 56 | + SchemaTypeFamily.UNION, |
| 57 | + SchemaTypeFamily.IS_INSTANCE, |
| 58 | + SchemaTypeFamily.CALL, |
| 59 | + ): |
| 60 | + raise errors.ModelFieldError( |
| 61 | + ctx.model_name, ctx.field_name, "tuple item must be of primitive, model, mapping or union type", |
| 62 | + ) |
| 63 | + |
| 64 | + if param_type_family not in (SchemaTypeFamily.MODEL, SchemaTypeFamily.UNION) and ctx.entity_location is None: |
| 65 | + raise errors.ModelFieldError(ctx.model_name, ctx.field_name, "entity name is not provided") |
| 66 | + |
| 67 | + if ctx.entity_location is EntityLocation.ELEMENT: |
| 68 | + return ElementSerializer.from_core_schema(arguments_schema, ctx) |
| 69 | + elif ctx.entity_location is None: |
| 70 | + return ElementSerializer.from_core_schema(arguments_schema, ctx) |
| 71 | + elif ctx.entity_location is EntityLocation.ATTRIBUTE: |
| 72 | + raise errors.ModelFieldError(ctx.model_name, ctx.field_name, "attributes of tuple types are not supported") |
| 73 | + else: |
| 74 | + raise AssertionError("unreachable") |
0 commit comments