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

AdjacentTagging with no content #411

Open
jennydaman opened this issue Jul 21, 2023 · 1 comment
Open

AdjacentTagging with no content #411

jennydaman opened this issue Jul 21, 2023 · 1 comment

Comments

@jennydaman
Copy link

I am trying to use pyserde for interoperability between a Rust project using serde and a Python project.

In my Rust code, I have an enum which is similar to Option<String>.

https://github.com/FNNDSC/chrs/blob/841e3957232d199ba8662c2915d12744217fd3d1/chrs/src/login/tokenstore.rs#L18-L24

/// A secret which may be securely stored.
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
#[serde(tag = "store", content = "value")]
pub enum StoredToken {
    Text(String),
    Keyring,
}

In TOML, it looks like either

# matches!(x, StoredToken::Text { .. })
[cubes.store]
store = 'Text'
value = 'abcdefghijklmnop'

# matches!(x, StoredToken::Keyring)
[cubes.store]
store = 'Keyring'

Since the StoredToken::Keyring variant doesn't contain data, I am not sure how to use pyserde. I naively tried

Text = NewType('Text', str)
Keyring = NewType('Keyring', None)


@serde.deserialize(tagging=serde.AdjacentTagging('store', 'value'))
@dataclasses.dataclass(frozen=True)
class Login:
    address: ChrisURL
    username: Username
    store: Text | Keyring

It doesn't work:


SerdeError: Can not deserialize {'store': 'Keyring'} of type Dict into Union[str, Keyring].
Reasons:
 Failed to deserialize into str: Not a type of str
 Failed to deserialize into Keyring: Unsupported type: Dict
@jennydaman
Copy link
Author

Suggestion: should we support this via Union[Text, typing.Never]?

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