-
Notifications
You must be signed in to change notification settings - Fork 586
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
feat(smithy-client): support strict union parsing #2746
Conversation
Codecov Report
@@ Coverage Diff @@
## main #2746 +/- ##
=======================================
Coverage ? 61.41%
=======================================
Files ? 539
Lines ? 27525
Branches ? 6722
=======================================
Hits ? 16905
Misses ? 10620
Partials ? 0 Continue to review full report at Codecov.
|
const setKeys = new Set<string>(); | ||
for (const [key, value] of Object.entries(asObject)) { | ||
if (value !== null && value !== undefined) { | ||
setKeys.add(key); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the Set really needed? Since we are validating the object has exactly 1 key, and JS object doesn't allow multiple entries under same key, why not just Object.keys(asObject)?.length !== 1
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can have multiple keys where only one is not null - { a: 1, b: null, c: null }
is a valid union (and a check of the # of keys would fail in this case, I think)
Set
isn't being used here as a way to deduplicate. I just used it because order doesn't matter and object keys are expected to be unique anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Object.entries
already returns a collection, it's looks unnecessary to create a Set explicitly. Will this do?
const isDefined = (value: unknow): boolean => value !== null && value !== undefined;
if (Object.values(asObject).filter(isDefined).length !== 1) {
throw new TypeError(`Unions must have exactly one non-null member`);
}
return asObject;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to include which keys were non-null in the error message when more than one key is set, which I can't do if I just filter the values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The suggested way is more idiomatic. But we don't feel strong about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of Set
seems unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ship it!
Unions must be JSON objects that only have one key set.
93f6c03
to
be99b31
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-approve
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread. |
Description
Unions must be JSON objects that only have one key set.
Testing
Unit tests, and protocol tests against regenerated clients.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.