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

Python representation of enums is still not perfect. #459

Open
amorenoz opened this issue Dec 10, 2024 · 0 comments
Open

Python representation of enums is still not perfect. #459

amorenoz opened this issue Dec 10, 2024 · 0 comments
Milestone

Comments

@amorenoz
Copy link
Contributor

amorenoz commented Dec 10, 2024

e["skb"].ip.version
V4 { v4: SkbIpv4Event { tos: 0, id: 59502, flags: 2, offset: 0 } }

It's cool that we can now access e["skb"].ip.version.v4 but there is no clean generic way of knowing if v4 or v6 should be accessed. We need to use something like hasattr.

In OVS is even more confusing because we do add the event_type in the JSON representation but that information is not available in the python side.
Maybe we could have a macro automagically add a helper called type() that returns a string representation of the enum variant, with the option to rewrite it like what serde does. Something like:

/// IP version and specific fields.
#[event_type]
#[serde(rename_all = "snake_case")]
pub enum SkbIpVersion {
   #retis(rename = "ipv4")
    V4 {
        #[serde(flatten)]
        v4: SkbIpv4Event,
    },
    #retis(rename = "ipv6")
    V6 {
        #[serde(flatten)]
        v6: SkbIpv6Event,
    },
}

That yields:

>>> e["skb"].ip.version.type()
"ipv4"

Of course this is just an idea for a follow up improvement.

Originally posted by @amorenoz in #441 (comment)

@atenart atenart added this to the v1.6 milestone Dec 10, 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

2 participants