yosiat / panko_serializer

High Performance JSON Serialization for ActiveRecord & Ruby Objects
https://panko.dev
MIT License
592 stars 36 forks source link

Filter on array serializer? #140

Closed mahdiar-naufal-shyftplan closed 1 year ago

mahdiar-naufal-shyftplan commented 1 year ago

Right now we are able to filter on singular serializer

class UserSerializer < Panko::Serializer
  attributes :id, :name, :email
end

# this line will return { 'name': '..' }
UserSerializer.new(only: [:name]).serialize(User.first)

# this line will return { 'id': '..', 'email': ... }
UserSerializer.new(except: [:name]).serialize(User.first)

But do you think we can apply that filter if we are using ArraySerializer?

Panko::ArraySerializer.new(users, each_serializer: UserSerializer)
yosiat commented 1 year ago

@mahdiar-naufal-shyftplan please see the docs regarding Nested Filters - https://panko.dev/docs/associations#nested-filters

mahdiar-naufal-shyftplan commented 1 year ago

I see, that's doable for same filter on each array element. Thank you @yosiat!

Do they also able to handle the different filter on each element?

For an example in array index 0 i want to keep only the id keys, but on array index 1 I want to keep only the id and name keys

yosiat commented 1 year ago

Do they also able to handle the different filter on each element?

It's not possible as of today and it's something a bit complex to implement, to support filtering based json-paths. It's not something I plan to add support for, but maybe in the future I'll come up with extension point that will allow choose custom filtering logic.

mahdiar-naufal-shyftplan commented 1 year ago

Thanks!