-
Notifications
You must be signed in to change notification settings - Fork 88
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
Add json options #120
Add json options #120
Conversation
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.
Nice addition. The change from type
to options
is very clean and opens up many new possibilities.
I am a little concerned that the model
object has more than one responsibility (breaking the Single Responsibility principle). It's only responsibility should be to handle the Object to Relational Mapping. By adding to_json
we have added the Serialization concern as well.
However, Rails model objects have to_json
that supports only:, except:, and methods:. Adding the as:, filter: and hidden: field options gives us the same flexibility and I find it to be very useful for quick prototyping.
I think the desire by the community is to simplify moving from Rails to Amber, and to that end, I think this change makes sense.
I share the same concern about keeping the functionality of ORM::Base simple, but wanting to provide a simple way for basic JSON configuration. A lot of the work here (and in Granite in general) is done in macro-land and I'm not sure it has to be. Muddling this concern so deeply with the The same functionality provided here could be given by splitting up json rendering into two methods: Here is a simplified example, some of which is a little psuedocode-ish: # generated as part of __process_fields
def rendered_fields : JSON::Any
{
field_1_name => field_1,
field_2_name => field_2
}
end
def to_json(json : JSON::Builder)
rendered_fields.to_json
end Then, when a presentation of that model is desired: # handwritten by dev
def rendered_fields : JSON::Any
{
"other_name" => field_1,
field_2_name => field_2,
related_object_name => related_object.rendered_fields
}
end Dividing the problem like this allows developers to do whatever they want in their override of |
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.
@c910335 I would like to keep the |
I started this PR because It is very useful and convenient for Web API developing if Amber has such kind of feature and Granite reaches this nearly. But I agree that ORM should not do too much work besides database. As for the design of |
@c910335 I like the presenter or serializer pattern. We use grape-entity at work and I find it provides a pretty clean approach for exposing an API. To your point, if you are only looking to change a couple fields, I can see overriding However, I can see this being abused and the mapping could look pretty ugly if you add logic to every field. There are two conflicting goals here. We want to provide an easy prototyping platform like rails provides but we also want to promote good coding practices and design patterns. @amberframework/core-team WDYT? |
Chipping in here. I agree with all above. We should build a presenter layer that will handle ViewModel and Serialization in Amber. |
I’m 👍 for a serializers library. However, you needn’t rewrite the entire |
Is there anything apart for the conflicting files, that keeps this PR from being merged? |
@Jens0512 unfortunately, yes. Per the discussion on this pull I think we've decided to go a different direction. |
as
: the name to replacefilter
: a lambda for filtering the valuehidden
: hiding this field if true