Skip to content

Commit daeb4f5

Browse files
committed
feat: add missing tests
1 parent 109007e commit daeb4f5

File tree

3 files changed

+419
-11
lines changed

3 files changed

+419
-11
lines changed

.coverage

0 Bytes
Binary file not shown.

json2xml/dicttoxml.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numbers
66
from collections.abc import Callable, Sequence
77
from random import SystemRandom
8-
from typing import Any, Union
8+
from typing import Any, Union, cast
99

1010
from defusedxml.minidom import parseString
1111

@@ -213,10 +213,12 @@ def get_xpath31_tag_name(val: Any) -> str:
213213
return "map"
214214
if isinstance(val, (int, float, numbers.Number)):
215215
return "number"
216-
if isinstance(val, Sequence) and not isinstance(val, str):
217-
return "array"
218216
if isinstance(val, str):
219217
return "string"
218+
if isinstance(val, (bytes, bytearray)):
219+
return "string"
220+
if isinstance(val, Sequence):
221+
return "array"
220222
return "string"
221223

222224

@@ -234,24 +236,25 @@ def convert_to_xpath31(obj: Any, parent_key: str | None = None) -> str:
234236
str: XML string in XPath 3.1 format.
235237
"""
236238
key_attr = f' key="{escape_xml(parent_key)}"' if parent_key is not None else ""
239+
tag_name = get_xpath31_tag_name(obj)
237240

238-
if obj is None:
241+
if tag_name == "null":
239242
return f"<null{key_attr}/>"
240243

241-
if isinstance(obj, bool):
244+
if tag_name == "boolean":
242245
return f"<boolean{key_attr}>{str(obj).lower()}</boolean>"
243246

244-
if isinstance(obj, (int, float, numbers.Number)):
247+
if tag_name == "number":
245248
return f"<number{key_attr}>{obj}</number>"
246249

247-
if isinstance(obj, str):
248-
return f"<string{key_attr}>{escape_xml(obj)}</string>"
250+
if tag_name == "string":
251+
return f"<string{key_attr}>{escape_xml(str(obj))}</string>"
249252

250-
if isinstance(obj, dict):
253+
if tag_name == "map":
251254
children = "".join(convert_to_xpath31(v, k) for k, v in obj.items())
252255
return f"<map{key_attr}>{children}</map>"
253256

254-
if isinstance(obj, Sequence):
257+
if tag_name == "array":
255258
children = "".join(convert_to_xpath31(item) for item in obj)
256259
return f"<array{key_attr}>{children}</array>"
257260

@@ -303,7 +306,7 @@ def convert(
303306
return convert_none(key=item_name, attr_type=attr_type, cdata=cdata)
304307

305308
if isinstance(obj, dict):
306-
return convert_dict(obj, ids, parent, attr_type, item_func, cdata, item_wrap, list_headers=list_headers)
309+
return convert_dict(cast("dict[str, Any]", obj), ids, parent, attr_type, item_func, cdata, item_wrap, list_headers=list_headers)
307310

308311
if isinstance(obj, Sequence):
309312
return convert_list(obj, ids, parent, attr_type, item_func, cdata, item_wrap, list_headers=list_headers)

0 commit comments

Comments
 (0)