-
-
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
Handle array format for dateHandler #1108
Conversation
VincentLanglet
commented
Jul 14, 2019
Q | A |
---|---|
Bug fix? | no |
New feature? | yes |
Doc updated | no |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | |
License | MIT |
Hi! Can you add some tests for this? I'm really not able to understand how this works... |
I made a mistake. All is good now and I added tests.
|
I think here is missing the update on the type parser. How to provide this new type definition via |
@goetas I wanted to be used this way
Are you saying |
Exactly. I think this has to be implemented. |
Do you know how it works and what is needed to be added/changed. I see in both public function visitArray($data, array $type): array So it seems like it's already implemented. |
In https://github.com/schmittjoh/serializer/tree/master/src/Type you have the classes responsible for the type parsing. They convert the type definition syntax in the array of parameters passed to the visitors. The type parsing is implemented using the hoa lexer and parser (see https://github.com/hoaproject/Compiler) |
Yes, the type visitor call
When you look at the Can you check again ? |
@goetas You maybe missed my previous message |
Most probably is not. You can see here the tests for it https://github.com/schmittjoh/serializer/blob/master/tests/Serializer/Type/ParserTest.php#L36 You can try to add here strings following your syntax and check if they pass. |
It seems good to me this way 0ff06b7 It's the same syntax than
|
Yeah, but the foundation of your proposal is based on this syntax: Can you test this? |
Damned ! I forgot what I wanted to test... Still working |
@goetas I'm sorry, how should I understand your emoji ? |
I'm confused because the grammar file has no instructions on how to handle |
I understand. Indeed,
|
Hi @goetas, do you have time to check it on your own ? |
I had a look, and as said in #1108 (comment)
@Majkl578 Could you help us to have the parser supporting |
Ideally the tests case to pass should be yield [
'DateTime<null, null, [\'Y-m-d\TH:i:s\', \'Y-m-d\TH:i:sP\']>',
$type('DateTime', [null, null, ['Y-m-d\TH:i:s', 'Y-m-d\TH:i:sP']]),
]; |
What are the semantic meaning differences between |
|
@VincentLanglet If you look at #1125 , @Majkl578 did exactly what you need! |
Great ! Thanks @Majkl578 |
HI @goetas What are the step now ?
Am I missing something ? |
tests/Serializer/Type/ParserTest.php
Outdated
@@ -79,6 +79,10 @@ public function validTypesProvider(): iterable | |||
'array<Foo\Bar, Baz\Boo>', | |||
$type('array', [['name' => 'Foo\Bar', 'params' => []], ['name' => 'Baz\Boo', 'params' => []]]), | |||
]; | |||
yield [ | |||
'DateTime<null, null, array<\'Y-m-d\TH:i:s\', \'Y-m-d\TH:i:sP\'>>', | |||
$type('DateTime', [null, null, ['name' => 'array', 'params' => ['Y-m-d\TH:i:s', 'Y-m-d\TH:i:sP']]]), |
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.
They should be tested together to make sure it works as expected.
Your current branch is not compatible with #1125.
If you checkout #1125 and and test it with your code, it will fail.
DateTime<null, null, ['Y-m-d\TH:i:s', 'Y-m-d\TH:i:sP'],
will produce an array format that your getDeserializationFormatsAndTimeZones
will not be able to understand.
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.
Wouldn't be easier if the #1125 branch was merge into master ?
Hi @goetas , happy new year. |
doc/reference/annotations.rst
Outdated
@@ -463,6 +466,11 @@ Examples: | |||
*/ | |||
private $endAt; | |||
|
|||
/** | |||
* @Type("DateTime<['Y-m-d', 'Y/m/d']>") |
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 is not correct. Serialization does not accept more than one format. The feature you are building makes sense only for de-serialization.
I think we should throw some error if this is used for serialization.
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.
DateTime<'formats'>
is a shortcut for
DateTime<'formats', null, 'formats'>
that's why I allowed the syntax.
But for serialization I used the first valid format.
If I only allow the third parameter to be an array, it means that someone who only care bout de-serialization, will write
@Type("DateTime<'Y-m-d'>")
To deserialize only one format, and
@Type("DateTime<'Foo', 'Bar', ['Y-m-d', 'Y/m/d']>")
To handle multiple syntax. I found this a little sad...
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 can understand that, but DateTime<['Y-m-d', 'Y/m/d']>
is wrong since the serialization does not accept multiple formats. it will always pick the first.
But to be hones, thinking it twice, I start to like this feature less and less.
I had a similar problem in the past and i solved it with a custom date handler (see https://github.com/goetas-webservices/xsd2php-runtime/blob/master/src/Jms/Handler/XmlSchemaDateHandler.php#L86)
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 can only allow the syntax DateTime<'Foo', 'Bar', ['Y-m-d', 'Y/m/d']> if preferred.
But to be hones, thinking it twice, I start to like this feature less and less.
It would be nice to make your choice, I took time working on it and I prefer to know now if I taking more time on it will be useless.
I had a similar problem in the past and i solved it with a custom date handler
I feel like, it's really hard to write a custom date handler for someone starting with the library.
When only using the annotation (like we do in our company), it's so much easier to use an array that writing a whole custom dateHandler.
See #1108 (comment) and I think an integration test would be a good thing to do, something like
Providing two date formats, the first should fail, and the second should succeed. |
Is now not supported And there is an integration test |
thank you @VincentLanglet for your work and your patience! |
…ormats are supported here as well