-
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 #144 from dapper91/dev
Dev
- Loading branch information
Showing
8 changed files
with
257 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from typing import List, Optional, Tuple | ||
|
||
from pydantic_xml import BaseXmlModel, RootXmlModel, attr | ||
|
||
|
||
# [model-start] | ||
class Product(BaseXmlModel, tag='product'): | ||
status: str = attr() | ||
title: str | ||
|
||
|
||
class Launch(RootXmlModel[int], tag='launched'): | ||
pass | ||
|
||
|
||
class Products(RootXmlModel): | ||
root: List[Tuple[Product, Optional[Launch]]] | ||
# [model-end] | ||
|
||
|
||
# [xml-start] | ||
xml_doc = ''' | ||
<Products> | ||
<product status="running">Several launch vehicles</product> | ||
<launched>2013</launched> | ||
<product status="running">Starlink</product> | ||
<launched>2019</launched> | ||
<product status="development">Starship</product> | ||
</Products> | ||
''' # [xml-end] | ||
|
||
# [json-start] | ||
json_doc = ''' | ||
[ | ||
[ | ||
{ | ||
"title": "Several launch vehicles", | ||
"status": "running" | ||
}, | ||
2013 | ||
], | ||
[ | ||
{ | ||
"title": "Starlink", | ||
"status": "running" | ||
}, | ||
2019 | ||
], | ||
[ | ||
{ | ||
"title": "Starship", | ||
"status": "development" | ||
}, | ||
null | ||
] | ||
] | ||
''' # [json-end] | ||
|
||
products = Products.from_xml(xml_doc) | ||
assert products == Products.model_validate_json(json_doc) |
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,43 @@ | ||
from typing import List, Optional, Tuple | ||
|
||
from pydantic_xml import RootXmlModel, element | ||
|
||
|
||
# [model-start] | ||
class Products(RootXmlModel): | ||
root: List[Tuple[str, Optional[int]]] = element(tag='info') | ||
# [model-end] | ||
|
||
|
||
# [xml-start] | ||
xml_doc = ''' | ||
<Products> | ||
<info type="status">running</info> | ||
<info type="launched">2013</info> | ||
<info type="status">running</info> | ||
<info type="launched">2019</info> | ||
<info type="status">development</info> | ||
<info type="launched"></info> | ||
</Products> | ||
''' # [xml-end] | ||
|
||
# [json-start] | ||
json_doc = ''' | ||
[ | ||
[ | ||
"running", | ||
2013 | ||
], | ||
[ | ||
"running", | ||
2019 | ||
], | ||
[ | ||
"development", | ||
null | ||
] | ||
] | ||
''' # [json-end] | ||
|
||
products = Products.from_xml(xml_doc) | ||
assert products == Products.model_validate_json(json_doc) |
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.4.0" | ||
version = "2.5.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