Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom field xml serializer/validator support added. #212

Merged
merged 2 commits into from
Sep 29, 2024

Conversation

dapper91
Copy link
Owner

@dapper91 dapper91 commented Sep 29, 2024

Adds support for custom field xml serialization and validation.

Example:

model.py:

import pathlib
from typing import List
from xml.etree.ElementTree import canonicalize

from pydantic_xml import BaseXmlModel, element, xml_field_serializer, xml_field_validator
from pydantic_xml.element import XmlElementReader, XmlElementWriter


class Plot(BaseXmlModel):
    x: List[float] = element()
    y: List[float] = element()

    @xml_field_validator('x', 'y')
    def validate_space_separated_list(cls, element: XmlElementReader, field_name: str) -> List[float]:
        if element := element.pop_element(field_name, search_mode=cls.__xml_search_mode__):
            return list(map(float, element.pop_text().split()))

        return []

    @xml_field_serializer('x', 'y')
    def serialize_space_separated_list(self, element: XmlElementWriter, value: List[float], field_name: str) -> None:
        sub_element = element.make_element(tag=field_name, nsmap=None)
        sub_element.set_text(' '.join(map(str, value)))

        element.append_element(sub_element)


xml_doc = pathlib.Path('./doc.xml').read_text()
plot = Plot.from_xml(xml_doc)

assert canonicalize(plot.to_xml(), strip_text=True) == canonicalize(xml_doc, strip_text=True)

doc.xml:

<Plot>
    <x>0.0 1.0 2.0 3.0 4.0 5.0</x>
    <y>0.0 3.2 5.4 4.1 2.0 -1.2</y>
</Plot>

@codecov-commenter
Copy link

codecov-commenter commented Sep 29, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.05%. Comparing base (b918ae7) to head (3de17e2).
Report is 3 commits behind head on dev.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #212      +/-   ##
==========================================
+ Coverage   91.89%   92.05%   +0.15%     
==========================================
  Files          29       29              
  Lines        1617     1648      +31     
==========================================
+ Hits         1486     1517      +31     
  Misses        131      131              
Flag Coverage Δ
unittests 92.05% <100.00%> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dapper91 dapper91 merged commit f92ab26 into dev Sep 29, 2024
5 checks passed
@dapper91 dapper91 mentioned this pull request Sep 29, 2024
Merged
@dapper91 dapper91 deleted the custom-xml-validator-serializer branch October 3, 2024 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants