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

Add as_xyz helper methods to Smithy unions / Rust enums #298

Closed
1 of 2 tasks
rcoh opened this issue Apr 12, 2021 · 5 comments
Closed
1 of 2 tasks

Add as_xyz helper methods to Smithy unions / Rust enums #298

rcoh opened this issue Apr 12, 2021 · 5 comments
Assignees
Labels

Comments

@rcoh
Copy link
Collaborator

rcoh commented Apr 12, 2021

Each union variant will define a as_xyz() -> Option<&Xyz> and is_xyz() -> bool method to enable easily extracting individual variants from the union without matching.

These should also be added to top level & combined errors.

  • Union Shapes
  • Error shapes
@rcoh rcoh added this to the 4/22 - Public Alpha milestone Apr 12, 2021
@rcoh rcoh added the good first issue Good for newcomers label Apr 12, 2021
@rcoh rcoh changed the title Add get_xyz helper methods to unions Add as_xyz helper methods to unions Apr 14, 2021
@rcoh rcoh changed the title Add as_xyz helper methods to unions Add as_xyz helper methods to Smithy unions / Rust enums Apr 14, 2021
@barbu110
Copy link
Contributor

Would like to jump on this. As a newbie with Rust and this project, this would be a good start to look through the code.

@rcoh rcoh removed this from the 4/22 - Public Alpha milestone Apr 15, 2021
@barbu110
Copy link
Contributor

barbu110 commented Apr 21, 2021

Hey, what would be the preferred approach between the following two:

  1. Implement a derive macro (e.g. MemberConvertible) in the Rust runtime, or
  2. Generate the impl and functions from the code generator?

To detail more about option 1, I was thinking of something like generating the following Rust code for unions:

#[derive(MemberConvertible)]
pub enum SomeUnion {
  Foo(Foo),
  Bar(Bar),
}

where this MemberConvertible macro would generate an impl SomeUnion with the is_xyz and as_xyz for every variant of the enum.

What do you think?

@rcoh
Copy link
Collaborator Author

rcoh commented Apr 21, 2021

In general, I would favor generating impl functions in Kotlin. Since we're code generating everything anyway, there isn't a compelling reason to build a derive macro. Adding a derive macro is what a "regular" Rust project would probably do but it introduces a bunch of extra complexity

@barbu110
Copy link
Contributor

Cool, then I will go with option 2. I'll submit a PR by tomorrow.

@barbu110 barbu110 self-assigned this Apr 21, 2021
rcoh pushed a commit that referenced this issue Apr 21, 2021
* Add as_xyz and is_xyz helper methods on Smithy Unions

Issue #298

* Fix Unions generation and add unit test

Co-authored-by: Victor Barbu <vicbarbu@amazon.com>
@rcoh rcoh reopened this Apr 21, 2021
@rcoh
Copy link
Collaborator Author

rcoh commented Apr 22, 2021

[reopened to also track adding the same thing to enums]

@rcoh rcoh closed this as completed Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants