Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support decoding empty strings as unset for all field types #685

Open
aolesky opened this issue May 13, 2024 · 0 comments
Open

Support decoding empty strings as unset for all field types #685

aolesky opened this issue May 13, 2024 · 0 comments

Comments

@aolesky
Copy link

aolesky commented May 13, 2024

Description

I am decoding a message schema that sends an empty string to represent an unset value. As far as I can tell, there is no way to handle this case without creating a custom type and processing in a decode hook. It could also probably be handled with internal fields that are mapped to duplicate external fields in the post_init method. Either way this complicates struct definition and loses some of the performance benefits of the library.

An optional flag to the decoder could make sense for this feature:

class Message(msgspec.Struct, kw_only=True, frozen=True):
    error_msg: str | msgspec.UnsetType = msgspec.UNSET
    value: Decimal | msgspec.UnsetType = msgspec.UNSET


success = b'{"error_msg": "", value: "2.5"}'
failure = b'{"error_msg": "Failed", value: ""}'


print(msgspec.json.decode(success, type=Message, empty_string_as_unset=True))
#> Message(value=Decimal('2.5'))


print(msgspec.json.decode(failure, type=Message, empty_string_as_unset=True))
#> Message(error_msg="Failed")
@aolesky aolesky changed the title Support decoding empty strings as null for Decimal types Support decoding empty strings as unset for all field types May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant