Merge pull request 'serde/fix: round trip string sanitizing' (#2034) from hsjobeki/clan-core:hsjobeki-main into main
This commit is contained in:
@@ -155,13 +155,12 @@ def construct_value(
|
||||
|
||||
return Path(field_value)
|
||||
|
||||
# Trivial values
|
||||
if t is str:
|
||||
if not isinstance(field_value, str):
|
||||
msg = f"Expected string, got {field_value}"
|
||||
raise ClanError(msg, location=f"{loc}")
|
||||
|
||||
return field_value
|
||||
return json.loads(f'"{field_value}"')
|
||||
|
||||
if t is int and not isinstance(field_value, str):
|
||||
return int(field_value) # type: ignore
|
||||
|
||||
@@ -265,6 +265,18 @@ def test_none_or_string() -> None:
|
||||
assert checked3 is None
|
||||
|
||||
|
||||
def test_roundtrip_escape() -> None:
|
||||
assert from_dict(str, "\\n") == "\n"
|
||||
assert dataclass_to_dict("\n") == "\\n"
|
||||
|
||||
# Test that the functions are inverses of each other
|
||||
# f(g(x)) == x
|
||||
# and
|
||||
# g(f(x)) == x
|
||||
assert from_dict(str, dataclass_to_dict("\n")) == "\n"
|
||||
assert dataclass_to_dict(from_dict(str, "\\n")) == "\\n"
|
||||
|
||||
|
||||
def test_path_field() -> None:
|
||||
@dataclass
|
||||
class Person:
|
||||
|
||||
Reference in New Issue
Block a user