-
Notifications
You must be signed in to change notification settings - Fork 35
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
Aliases for custom method #66
Comments
Hi @xchi ! Looks like what you need is support for casing, so you can write something like this: class MetricPeriodSerializer < Panko::Serializer
attributes :period, :metric_values, :first_name
def metric_values
[{
'name' => 'metric1',
'value' => object.metric_1
},
{
'name' => 'metric2',
'value' => object.metric_2
}]
end
end and the output of the keys will be Now the only question is how we want to support it, currently, Panko don't have global configuration and I want to delay it. So we can have: specify it on the serializer level like attributes or when serializing. I prefer on the serializer level. If this makes sense to you, let me know and I'll implement it. |
Hey @yosiat , It will be very helpful if you can support casing for our project, as we basically need to create aliases for almost all fields we want to parse. On the serializer level as one attribute is fine for us as well. Vivian |
@xchi let's make sure the api makes sense to you and I'll implement it. class MetricPeriodSerializer < Panko::Serializer
keys_format :camel_case
attributes :period, :metric_values, :first_name
def metric_values
[{
'name' => 'metric1',
'value' => object.metric_1
},
{
'name' => 'metric2',
'value' => object.metric_2
}]
end
end We will start by defining the serializer class and maybe in the future we will create a global configuration. |
In my mind, the simplest and most obvious way to achieve this is to just not use snake_case in your serializers. No other code should be referencing the attribute methods in your serializer, and I think the clearest possible way of doing it, is just using the attribute names as you expect them to come out after serialization. Also I think global configuration should not be a thing. It's just another name for a global variable that affects the result of something that was pure before. Using a base serializer should be all that is needed to support that. Side note @yosiat: I'd like to pick this feature up, if you disagree with my first statement and think it has value. |
@mikebaldry about global configuration, I totally agree with you. About specifying the names of the attributes as snake case - this can be really problematic for a developer - database column names are not snake case and linters will yell that methods are not snake case. In addition to that, think about serializer that is used for external API and some specific integration needs to be output as snake case with a subset of fields. In today's case, you need another serializer for this, in ideal place - you will re-use the same serializers and pass options for filters and key format. I am ok with you taking this feature up (and really happy with that)! but let's make sure we are aligned on the public API for panko and let's get @xchi on board with us. |
Sure, I see your points :) Happy to implement like the example above and we can go from there? |
@mikebaldry cool, let's go for it! as how I see the implementation, there is no need to change something in the C-extension part (except aliases I think, need to test it). since each attribute have Please make sure that your PR includes testing for - attributes (both methods and simple attributes), aliases and associations (both has_one and has_many) and docs. |
I've had a little first attempt working.. 9d352a4 I have a few things:
Many thanks |
Hey @mikebaldry , I had look the implementation and it seems can fix our problem. The only question I had is whether you need to provide all support for |
@mikebaldry hi! sorry, another long work-week :) If you can make a PR and it will be simpler to discuss on the code. Some notes:
|
@mikebaldry hi! any update on this? can I help you? |
@yosiat Just adding +1 to the camelize support, that would be super helpful! |
Just want to check any update to this one? |
We want to implement a custom method with payload key name as
metricValues
, but the proper ruby method name should be snake_case.We have tried the following way, but having no luck. Just wondering does panko support alias for custom method by any chance?
The text was updated successfully, but these errors were encountered: