Skip to content

Add initial support for fat enums (internally-tagged representation) #136

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

Closed
wants to merge 1 commit into from

Conversation

Veetaha
Copy link
Contributor

@Veetaha Veetaha commented Sep 12, 2020

Blocked by (these PRS have to reviewed and merged first before this one):

Internally tagged enums

The docs are pretty self-explanatory. This PR adds support for simple fat enums that have either one-element-tuple variants of the nested value of type T such that T: Attributes, or unit variants.

use dynomite::Attributes;

#[derive(Attributes)]
#[dynomite(tag = "kind")] // required to specify for fat enums
enum Shape {
    Rectangle(Rectangle),
    // Use `rename` to change the **value** of the tag for a particular variant
    // by default the tag for a particular variant is the name of the variant verbatim
    #[dynomite(rename = "my_circle")]
    Circle(Circle),
    Unknown,
}

#[derive(Attributes)]
struct Circle {
    radius: u32,
}

#[derive(Attributes)]
struct Rectangle {
    width: u32,
    height: u32,
}

These Rust definition correspond to the following representation in DynamoDB for each enum variant:

  • Rectangle:
    {
        "kind": "Rectangle",
        "width": 42,
        "height": 64
    }
  • Circle:
    {
        "kind": "my_circle",
        "radius": 54
    }
  • Unknown:
    {
        "kind": "Unknown"
    }

Closes: #131

How did you verify your change:

Added a unit test, trybuild tests and doc tests

What (if anything) would need to be called out in the CHANGELOG for the next release:

  • Add initial support for fat enums (only one-element tuple variants of T: Attributes values and unit variants)

@Veetaha Veetaha force-pushed the feat/fat-enums branch 6 times, most recently from 3ec117b to 86b63e3 Compare September 12, 2020 10:26
@Veetaha
Copy link
Contributor Author

Veetaha commented Sep 15, 2020

This should be ready for the review.
cc @softprops

@softprops
Copy link
Owner

@Veetaha I'll try to follow up within the next week. I'm getting caught up with some things at work but I'll try to chew through this iteratively

@Veetaha
Copy link
Contributor Author

Veetaha commented Oct 7, 2020

Friendly ping @softprops :D

@softprops
Copy link
Owner

Sorry about the delay. I've been caught up with a team transition at work. I just merged the last clean up pull and it seems there are now conflicts with these changes

@Veetaha
Copy link
Contributor Author

Veetaha commented Oct 18, 2020

Uh oh, that PR depended on this one. Now the changes from this PR are already in master ;d

@Veetaha
Copy link
Contributor Author

Veetaha commented Jan 20, 2021

We've been using our fork with this feature that we found lacking for us for quite some time successfully, I think it's fine to have this in master, so we are waiting for a new release on crates.io.

@Veetaha Veetaha closed this Jan 20, 2021
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

Successfully merging this pull request may close these issues.

Support fat enums
2 participants