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

chore(smithy): Improve union DX #3304

Merged
merged 3 commits into from
Jun 28, 2023
Merged

Conversation

dnys1
Copy link
Contributor

@dnys1 dnys1 commented Jun 22, 2023

Currently to construct a union member, you need to pass an instance of the represented object, for example:

sealed class AnalyticsFilter extends SmithyUnion<AnalyticsFilter> {
  const factory AnalyticsFilter.tag(Tag tag) = AnalyticsFilterTag;
}

void usage() {
  final filter = AnalyticsFilter.tag(Tag(key: key, value: value));
}

This was uncovered as a less-than-desirable DX when exploring the config object. Instead, we can inline the parameters needed to construct the object to save the extra construction:

sealed class AnalyticsFilter extends SmithyUnion<AnalyticsFilter> {
  factory AnalyticsFilter.tag({
    required String key,
    required String value,
  }) =>
      AnalyticsFilterTag(_i2.Tag(
        key: key,
        value: value,
      ));
}

void usage() {
  final filter = AnalyticsFilter.tag(key: key, value: value);
}

This also means you don't need to know the underlying object type which is often just a required abstraction for Smithy - more often than not they are just anonymous groups of properties.

The old behavior is still available by invoking the union subclass constructor directly, e.g.

void usage() {
  final filter = AnalyticsFilterTag(Tag(key: key, value: value));
}

@dnys1 dnys1 requested a review from a team as a code owner June 22, 2023 22:57
@dnys1 dnys1 force-pushed the chore/smithy/union-factory-improvements branch 2 times, most recently from 3d18751 to 2fedd4f Compare June 22, 2023 23:19
NikaHsn
NikaHsn previously approved these changes Jun 27, 2023
Jordan-Nelson
Jordan-Nelson previously approved these changes Jun 27, 2023
Base automatically changed from chore/smithy/cleanups to main June 28, 2023 20:33
@dnys1 dnys1 dismissed stale reviews from Jordan-Nelson and NikaHsn June 28, 2023 20:33

The base branch was changed.

@dnys1 dnys1 force-pushed the chore/smithy/union-factory-improvements branch from 2fedd4f to 3c5c27c Compare June 28, 2023 20:34
@dnys1 dnys1 merged commit b48c205 into main Jun 28, 2023
182 checks passed
@dnys1 dnys1 deleted the chore/smithy/union-factory-improvements branch June 28, 2023 21:01
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.

3 participants