dynomite icon indicating copy to clipboard operation
dynomite copied to clipboard

Support fat enums

Open Veetaha opened this issue 5 years ago • 1 comments

💡 Feature description

It would be cool if we could use fat enum (type unions) as a first-class citizen with dynomite. For example, serde_dynamodb supports this via serde, but I am not sure how we should go about this in dynomite

💻 Basic example

#[derive(Item, Clone)]
struct Shape {
    #[dynomite(partition_key)]
    id: Uuid,
    display_name: String,
    #[dynomite(flatten)]
    kind: ShapeKind,
}

#[derive(Attributes)]
#[dynomite(tag = "shape_kind")] // tag for internally-tagged enum
enum ShapeKind {
    Square {
        width: u32,
        height: u32,
    }
    Circle {
        radius: u32
    },
}

I would expect it to be represented as:

{
     "id": "8e4a34e5-dcab-4462-9e81-b8181195b69f",
     "display_name": "bruh",
     "shape_kind": "Square",
     "width": 42,
     "height": 65,
}
{
     "id": "8e4a34e5-dcab-4462-9e81-b8181195b69f",
     "display_name": "bruh",
     "shape_kind": "Circle",
     "radius": 43,
}

With a flat structure (so that it is possible to use enum properties as partition/sort keys.

Veetaha avatar Sep 07 '20 21:09 Veetaha

I'm into this

Since there's already support for deriving Attribute for enums here there's a starting point. What's left is capturing the the tag name in the syntax and expanding support for structlike variants.

We only support bare variants atm

softprops avatar Sep 07 '20 22:09 softprops