Files
AI/참고/guardrails-main/guardrails/utils/xml_utils.py

38 lines
966 B
Python
Raw Normal View History

2026-05-12 19:40:31 +09:00
from typing import Optional, Union, cast
# TODO: Remove after DataTypes and ValidatorsAttr is removed
def cast_xml_to_string(xml_value: Union[memoryview, bytes, bytearray, str]) -> str:
"""Cast XML value to a string.
Args:
xml_value (Union[memoryview, bytes, bytearray, str]): The XML value to cast.
Returns:
str: The XML value as a string.
"""
return cast(str, xml_to_string(xml_value))
def xml_to_string(
xml: Optional[Union[memoryview, bytes, bytearray, str]],
) -> Optional[str]:
"""Convert XML value to a string.
Args:
xml_value (Union[memoryview, bytes, bytearray, str]): The XML value to cast.
Returns:
str: The XML value as a string.
"""
if xml is None:
return None
string = xml
if isinstance(xml, memoryview):
string = xml.tobytes().decode()
elif isinstance(xml, (bytes, bytearray)):
string = xml.decode()
return cast(str, string)