-
Notifications
You must be signed in to change notification settings - Fork 164
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
PHP discriminated unions proposal #5407
base: main
Are you sure you want to change the base?
Conversation
61b4b03
to
da8ce2a
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.
This looks great - mostly just thinking out loud on my comments, but I think this is shaping up really well.
$dateType = null; | ||
$arrayType = null; | ||
$unionType = null; | ||
$typeName = null; |
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.
nit/optional: I wonder if we could extract this into a helper function that we can reuse here and above. As long as this is well tested it's OK if we keep things as-is for now, just thinking about the long term structure of these core utilities.
if ($this->value !== 'fern') { | ||
throw new \Exception( | ||
"Expected value to be \"fern\"." | ||
); | ||
} |
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.
Depending on the phpstan literal support and whether or not we make this always be the literal value, we might not need this check.
With that said, we should definitely validate against literal values in the [de]serialization layer. For example, if this is a response type and the server sends the following, we should fail to deserialize the type:
{
"fern": "something"
}
All that to say - the more we can move this validation logic to the serialization layer the better. That way when users have a constructed instance of the type, it's guaranteed to be valid.
[ | ||
'base' => 'base', | ||
'type' => 'fern', | ||
'fern' => 'fern', |
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.
Depending on the result of the conversation above, would be great to include a test that throws when the literal doesn't match.
…ass of disc union doesn't have the appropriate arguments
Our PHP SDK generator currently does not support discriminated unions. This PR is a proposal detailing how they should look.