-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[ZF3] Data Transformer idea #5051
Comments
Initial response: fucking brilliant! |
This is just a piece of the puzzle though. I'm not saying anything new other than what we've already discussed on IRC, but basically, my idea is a bit more widespread. What we're currently doing can be represented via pipes:
Data Transformer is just one of the missing pieces, but what I see is a replacement for the huge (always hated) Form component. Huge +1 for data transformer at first, but I'm just not sure about how to deal with non-array data in this "bigger" plan. |
Is a deserializer missing in the incoming? If so, that could make whatever it is an array? Sent from my iPhone On Aug 30, 2013, at 8:51 PM, Marco Pivetta notifications@github.com wrote:
|
Initial feedback: @devosc DataTransformer is to manipulate data either incoming out outgoing from one representation to another. In terms of deserialization, likely you're thinking along the lines of what the current |
At the time I was thinking more along the lines of php unserialize for data coming from session storage (as an example)... Actually I wondered if decode and encode were better names... But I may be missing the point here :) Sent from my iPhone On Sep 3, 2013, at 10:26 AM, weierophinney notifications@github.com wrote:
|
Yep, it's not really the same. In my mind DataTransformer are not bi-directional. They just convert any format to anything. This anything can be… well… anything. Changing keys, changing how values are ordered… It would really be a really lightweight component, in fact. |
So what's the purpose of the serializer after the data transformer in the output? Sent from my iPhone On Sep 3, 2013, at 10:59 AM, Michaël Gallego notifications@github.com wrote:
|
@devosc Serializer is primarily for working with binary formats; DataTransformer is targetted at PHP native types. |
Hi, it's a good idea. I'd like to take the opportunity of this brainstorm phase to widen the discussion before narrowing it down and implementation. So i looked around and wrote some stuff down that might need to be considered. Lists come first, scroll down for questions and some code. Below is a list of possible steps you might want to do with the data.
What should happen if a steps fails, or completes with errors?
Each step can apply at different levels
Data formats
Datamodels
Metadata formatsAnnotations, XML, YML A list of metadata already out there.Some will be the same as the steps defined above. But others like group do not apply to one particular step, but to all the steps (group is like a profile of the entire pipe). A second problem is that traditionally this meta data does exist on a property level or object level, but so far i have not seen metadata that described an object graph. (a lot of annotations are described here: http://jmsyst.com/libs/serializer/master/reference/annotations)
Metadata for Doctrine2Additional metadata for Doctrine 2 that could be combined with metadata described above. Possible to use one piece of metadata for multiple purposes:
QuestionsLooking at all these possibilities a few questions come to mind:
CodeSome code of how some of this stuff could work:
I hope any of this is useful to get some good ideas ! :) |
Hi, I'm not sure to have fully understood everything you're saying. Sorry about that ;-). I think the main question is to draw clear boundaries about what component should do or not doing (hydrators, serializers, data transformers).
Data transformer.
Data transformer.
Data transformer.
Data transformer.
Input filter.
Input filter. Globally, it seems that you confuse several things. A lot of what you are saying in your post seems to belong to the Input filter component. I'm not sure to understand your part about various JMSSerializer annotations. It will be even more confuse because we already have a Serializer component. I mean the terminology is getting really confused. We really should make a clear document about : which components do we have, how do we call them, what are they used for, what kind of data they operate on... Anyway, thanks for your comment, it is really helpful :). |
Ooh ok i misunderstood the concept, i was under the assumption that one component just does 1 thing (just like in linux). But as i understand now it's like this:
I certainly agree that boundaries about what what should do are very important! |
Could this component solve the issue worked out in https://github.com/SamsonIT/DataViewBundle as well? I just learned about this pierce while I was thinking about this for quite some time. Basically, just like a .phtml view script transforms an entity into HTML, a DataView transforms an entity into a configurable array. This array can be used to return json from a RESTfull server. The point there, you don't want to have a 1:1 mapping between entities and json. Either because naming between entities and json can differ (camel case/underscores etc) or not all properties should be converted (hashed passwords from users). A data view is able to make this conversion per entity, if desired. Not sure if this is relevant for this proposal, but it just sounded similar and hopefully we can get something like this for zf2 too. |
Yes, this seems like a sane use case of such a feature. The question is : what should be included by default in data transformer component ? Only some interfaces and a plugin manager or something more complex ? I'm a bit in the dark :). Envoyé de mon iPhone Le 15 sept. 2013 à 21:50, Jurian Sluiman notifications@github.com a écrit :
|
Best regards, Andreas |
I've got mixed feelings about that - it's something that sounds like a job for validators.
In case a transformer doesn't know what to do with the data (i.e. double-deserialization for some reason) or some other case when transformation is not required |
@Thinkscape invalid data should simply halt everything. You don't need to process invalid stuff except if you want to log malicious attacks. |
Please mark it for the 3.0.0 milestone! |
This issue has been closed as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html |
ZF3 - Data Transformer component
EDIT : This can of course be integrated in future version of ZF2 if there is interest for it.
Hi everyone. I'd like to share some thoughts about a new component that Ocramius and I thought about on IRC that would solve some issues more cleanly, in Zend Framework 3.
This component would be called "DataTransformer" (I kinda like the name) and should be responsible to… well… transform data to one format to another. The whole point of this component would be to have something "higher level" than introducing similar concepts in various components (like normalizers in hydrators).
It looks a bit like hydrators, but in fact this component could transform one array to another array. Hydrators could be thought as specialized data transformers (they could even implement a DataTransformer interface, in fact).
Problems
In order to highlight the problems I want to solve, here are two examples:
ZF2 ClassMethods hydrator
The current ClassMethods hydrator has a "underscoreSeparated" option that is used by the hydrator to transform keys from underscore_separated keys to camelCased (or the opposite.)
This is used among other things so that people can add form elements using the standard ZF2 "underscore_separated" convention for input names, and when hydrating, convert back those keys to "camelCased" so that the key "first_name" calls the "getFirstName" method. And vice-versa.
In my hydrator refactor I've removed those conversions because: it makes the hydrator harder to read and maintains and seems like the wrong place to do it (what about other components like Input Filters?).
REST API
I'm an Ember-Data user (which is a ORM for JavaScript), and this framework has very strong conventions (made by Ruby on Rails developers). It's better not changed those conventions and adapt the backend code to use their convention for communicate.
For instance, let's stay we have the following User entity:
When Ember-Data will make a POST request to the server, it will send data using the following form (according to the JSON API format):
It automatically wrapped the payload around a word that is the pluralized word of the entity ("users"), and converted everything to underscore_separated. Obviously, the server does not understand this format and will fail. For solving this issue we must write a lot of boilerplate code in input filter, hydrator…
On the other hand, for Ember-Data to understand a GET response, the server must send the data according to this format.
Some may argue that the client ORM should adapt to the backend, but in this case, it's just pain in the ass.
Anyway, this example is just to show you one use case of data-transformers.
Solutions
One first and naive solution is to do what we did in the ClassMethods hydrator: embedding the transformer directly in the hydrator. However, this does not work: if we use a REST API, a POST request will first validate and filter data using an input filter. At this point, hydrator was not executed yet. So for this to work, we should duplicate also the normalizing code to the input filter. Obviously not good.
Furthermore, embedding the transformation in the hydrator make it hard to extend to custom use-cases (for instance Ember or any other JS framework).
DataTransformer
The DataTransformer would be a very simple interface that would define only one method:
Transformations could be chained using a TransformationPipeline:
How to use it?
REST API exemple
The idea is that this thing could be use everywhere, for any data transformation. For instance, I may decide to attach one transformer very early in the process, maybe on "route" event, and convert the POST data from one format to another. On the other hand, I could have another transformer on "render" event that would convert back to another format.
Here is a workflow, using my previous Ember-Data exemple:
Problems
Here are a few problems:
Thoughts
?
The text was updated successfully, but these errors were encountered: