Skip to content

Add union type design overview. #3119

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

Merged
merged 2 commits into from
Mar 28, 2022
Merged

Add union type design overview. #3119

merged 2 commits into from
Mar 28, 2022

Conversation

millems
Copy link
Contributor

@millems millems commented Mar 23, 2022

No description provided.

@millems millems requested a review from a team as a code owner March 23, 2022 20:32
static AttributeValue.Builder builder();
static fromS(String s);
static fromN(String s);
static fromB(String s);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops: SdkBytes b

Comment on lines 66 to 67
void accept(AttributeValue.VoidVisitor visitor);
<T> T accept(AttributeValue.Visitor<T> visitor);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering, should we have these? The customer can do a switch based on the type() for something equivalent.

Copy link
Contributor Author

@millems millems Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conclusion from review: They might be nice, but the void visitor is basically just a switch on the type() and the visitor is just a switch expression on the type() (added in Java 17). Admittedly, not a lot of customers are on 17 right now, but it would be good not to add it if we can avoid it. We'll end up waiting for customer feedback on this one before we add it.


## Union Type Definition

We will follow the patterns set by `Document` and `JsonNode`, because the pattern used by `AudioStream` would not be backwards-compatible with existing types and does not function particularly well for union type members that are not structures.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what the "...does not function particularly well for union type members that are not structures." means?


We currently implement friendly tagged unions in at least two external places within the SDK already:
1. The `software.amazon.awssdk.core.document.Document` type.
2. Event stream types, like TranscribeStreaming's `software.amazon.awssdk.services.transcribestreaming.model.AudioStream` type.
Copy link
Contributor

@dagnir dagnir Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better example (or in addition to) might be one of newer eventstreams like in Lex, which use the newer style of eventstream generation that supports 1:M of shapes to events

Comment on lines 125 to 124
**After Changes**
```java
String result = attributeValue.visit(new AttributeValue.Visitor<String>(){
String visitS(String s) { return s; }
String visitN(String n) { return n; }
String visitB(SdkBytes b) { return b.asUtf8String(); }
});
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I like this

Comment on lines +144 to +140
```java
attributeValue.visit(new AttributeValue.VoidVisitor() {
void visitS(String s) { System.out.println(s); }
void visitN(String n) { System.out.println(n); }
void visitB(SdkBytes b) { System.out.println(b.asUtf8String()); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or using pattern matching for switch!

@millems millems force-pushed the millem/union-types branch from ea31722 to dded97b Compare March 28, 2022 17:59
@millems millems enabled auto-merge (squash) March 28, 2022 17:59
millems added a commit that referenced this pull request Mar 28, 2022
Service models marked as a union type have additional helper methods to take advantage of the knowledge that just one member is expected to be set.

More information: #3119
@millems millems disabled auto-merge March 28, 2022 19:25
@millems millems merged commit 064d697 into master Mar 28, 2022
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

millems added a commit that referenced this pull request Mar 28, 2022
* Added support for modeling union types.

Service models marked as a union type have additional helper methods to take advantage of the knowledge that just one member is expected to be set.

More information: #3119

* Addressed comments: two values set is now type() == null instead of type() == UNKNOWN_TO_SDK_VERSION.
@millems millems deleted the millem/union-types branch October 19, 2022 19:35
aws-sdk-java-automation added a commit that referenced this pull request Jul 23, 2024
…f39ca924d

Pull request: release <- staging/661c5916-0027-4e4d-9671-d58f39ca924d
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