Skip to content

AdjacentTagging with no content #411

@jennydaman

Description

@jennydaman

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions