-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #213 from dapper91/dev
- custom field xml serializer/validator support added.
- Loading branch information
Showing
11 changed files
with
200 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pydantic-xml" | ||
version = "2.12.1" | ||
version = "2.13.0" | ||
description = "pydantic xml extension" | ||
authors = ["Dmitry Pershin <[email protected]>"] | ||
license = "Unlicense" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from typing import List | ||
|
||
from helpers import assert_xml_equal | ||
|
||
from pydantic_xml import BaseXmlModel, element, xml_field_serializer, xml_field_validator | ||
from pydantic_xml.element import XmlElementReader, XmlElementWriter | ||
|
||
|
||
def test_xml_field_validator(): | ||
class TestModel(BaseXmlModel, tag='model1'): | ||
element1: List[int] = element() | ||
|
||
@xml_field_validator('element1') | ||
def validate_element(cls, element: XmlElementReader, field_name: str) -> List[int]: | ||
if element := element.pop_element(field_name, search_mode=cls.__xml_search_mode__): | ||
return list(map(int, element.pop_text().split())) | ||
|
||
return [] | ||
|
||
xml = ''' | ||
<model1> | ||
<element1>1 2 3 4 5</element1> | ||
</model1> | ||
''' | ||
|
||
actual_obj = TestModel.from_xml(xml) | ||
expected_obj = TestModel( | ||
element1=[1, 2, 3, 4, 5], | ||
) | ||
|
||
assert actual_obj == expected_obj | ||
|
||
|
||
def test_xml_field_serializer(): | ||
class TestModel(BaseXmlModel, tag='model1'): | ||
element1: List[int] = element() | ||
|
||
@xml_field_serializer('element1') | ||
def serialize_element(self, element: XmlElementWriter, value: List[int], 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) | ||
|
||
expected_xml = ''' | ||
<model1> | ||
<element1>1 2 3 4 5</element1> | ||
</model1> | ||
''' | ||
|
||
obj = TestModel( | ||
element1=[1, 2, 3, 4, 5], | ||
) | ||
|
||
actual_xml = obj.to_xml() | ||
assert_xml_equal(actual_xml, expected_xml) |