-
Notifications
You must be signed in to change notification settings - Fork 356
Better coercion #336
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
Better coercion #336
Conversation
Please review! |
I did...
Max Loeb <[email protected]> schrieb am Mi., 30. Nov. 2016, 18:41:
… Please review!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#336 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAR614rXX-NeGNX5oSlaySFCeXiARzCaks5rDbVhgaJpZM4K9BDN>
.
|
@digitalkaoz Did you? I don't see any comments, stamps, or 'approved' events. Did I miss something? |
{ | ||
$this->_check($value, $schema, $path, $i, true); | ||
} | ||
|
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 should fix the indentation here. maybe we should create a PR with php-cs-fixer & style-ci
$validator->coerce($value, $schema, $path, $i); | ||
} else { | ||
$validator->check($value, $schema, $path, $i); | ||
} |
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.
indentation again
$this->_check($value, $schema, $path, $i, true); | ||
} | ||
|
||
protected function _check(&$value, $schema = null, JsonPointer $path = null, $i = null, $coerce = false) |
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.
mh maybe we can make it even more BC by just providing a CoerceConstraint (which wraps the underlying Constraint) and does all the type checking. so in my educated guess we only pass the coerce flag to the factory so it wraps the wanted constraint with the CoerceConstraint. The Facade-Pattern it is ;)
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.
so all other constraints are mostly untouched...
like this:
class CoerceConstraint
{
public function __construct(Constraint $constraint)
{
$this->constraint = $constraint;
}
public function check(...)
{
$result = $this->constraint->check(...);
//do the coercion here...
}
}
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.
Interesting idea, but unfortunately any kind of coercion that depends on check
is a non-starter because I would have to change the first argument to a reference, which would break bc.
ah now, forgot to submit the review |
Fixed the whitespace issues, thanks; one of these days I should quit being lazy and modify my IDE settings. |
@bighappyface How are your plans for the next release coming along? |
4.1.0 with this PR? |
Sounds good. Are we waiting on anything? I believe I'll need to rebase once you merge #331 |
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.
@shmax please rebase
@bighappyface Rebased. @SereneAnt I de-simplified your code a bit; I've brought in a Please review! |
@@ -125,10 +124,14 @@ protected function incrementPath(JsonPointer $path = null, $i) | |||
* @param JsonPointer|null $path | |||
* @param mixed $i | |||
*/ | |||
protected function checkArray($value, $schema = null, JsonPointer $path = null, $i = null) | |||
protected function checkArray(&$value, $schema = null, JsonPointer $path = null, $i = null, $coerce = false) |
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.
Missing PHPDoc for parameter$coerse
.
IMHO, $coerse
flag would look better as the Factory
field rather than additional method parameter.
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.
Ah, good eye, thank you. I've added phpdocs, following the patterns and style already in place.
As for adding $coerce as a Factory
flag, well, that was the approach I tried previously. It made sense in my previous approach because I was driving everything through check
, and it just became a mode
flag (stored in Factory
, as you suggest). The problem there was that I had no reference to work with in check
, so I couldn't properly coerce in all situations.
Now that I'm driving it through an explicit function call, coerce
, it's no longer really a configuration value. If one were to, say, call check
immediately after calling coerce
, I would just have to set any stored fields back to false, so it would be another moving part with no real value.
I was actually interested in your reaction to what I did in CollectionConstraint
. Any thoughts?
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.
Changes look reasonable. Type check is more solid with this approach.
The performance penalty is not too big, avg time for validating 100K string array:
- original implementation: 1700 ms
- optimized, explicit type check (string, number, int) improve validation performance of long string, number and integer arrays #331: 550 ms
- this branch: 720 ms
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.
Thanks for the review! If you're satisfied, please approve.
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 have no write permissions, can't approve.
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.
A LGTM will help
@bighappyface please merge, or let me know what I have to do to move forward. |
LGTM |
Sorry for the late reply, but works like a charm ;) |
@bighappyface What's the plan? |
Thank you! |
Nobody noticed the mixed spaces and tabs indentations? |
Nope, nobody did, and you're commenting on a PR that was merged almost two months ago. If you can find formatting issues in the current state of the code I'll be happy to clean 'em up. |
Yeah, sorry, I was going through the changes between 4.x and 5.x as I wanted to see if Composer can easily upgrade/be compatible with the latest major version (we currently are compatible with 3.x and 4.x). Noticed some inconsistencies in the change sets. Hence my code-style PR submission :-) |
Thanks for the help ensuring things stay smooth with Composer, @alcohol. Being a dependency for Composer is our crown jewel 👑 |
This is a second attempt at providing coercion support. My first attempt suffered from a few faults:
check()
--which did not take a reference as a first argument--so only objects passed in as variables were affected by coercion.allOf
This new approach has a number of advantages and improvements over the older one:
coerce
method to kick things off. It takes a reference, and everything after that point switches betweencoerce
andcheck
methods as needed.check
is unaffected.coercion now happens more deeply in the code, at the
TypeConstraint
level. This means that as long as the rightcoerce
flags are passed along and references are used where appropriate internally, coercion just sort of works properly without having to wedge additional code in all over the place.The
CHECK_MODE_COERCE
flag is no longer necessary, so you're no longer obliged to create aFactory
, which means simpler client code in most cases.Fixes #334, #329 and #322
API Note: while this does not break the
check()
API, you must now usecoerce()
to do coercion. TheCHECK_MODE_COERCE
flag has been removed.Touched a lot of files, apologies, but hopefully it's not too hard to follow.
@bighappyface @pcjacobse @digitalkaoz please review