Skip to content
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

Create dump/1 for saving filters. #57

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

gen1321
Copy link

@gen1321 gen1321 commented Nov 11, 2018

Closes #42. The issue also mentions implementing Ecto Type. If you can provide an example, I will do my best to implement this too.

Copy link
Owner

@rcdilorenzo rcdilorenzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a great start @gen1321. Here are my comments.

I think we need to more clearly represent the terminology here. While we have parse for understanding data (which makes sense due to the human-like terms in there parameters), dump doesn't quite give enough. I realized I originally used that word in my issue, but I think that encode_map_value will work better since it gives the idea of what form we are encoding to. Additionally, when we implement the custom type, we don't want to overload the term dump since that's used by Ecto.

Because I'm suggesting the addition of a Filtrex.Encoder.Map (see individual comments), I think we should also be consistent and rename Filtrex.Encoder to Filtrex.Encoder.Fragment since it maps to Ecto fragment structures. If you need guidance on creating a protocol, checkout that encoder to get an idea.

As far as a custom type. I think that you would add Filtrex.Ecto.Type as the module name. For implementation details, checkout my article on Medium on how to do this: https://medium.com/@rcdilorenzo/creating-a-custom-ecto-duration-type-994e32ad4613.

README.md Outdated Show resolved Hide resolved
lib/filtrex/condition.ex Outdated Show resolved Hide resolved
lib/filtrex/condition.ex Outdated Show resolved Hide resolved
lib/filtrex/condition.ex Outdated Show resolved Hide resolved
lib/filtrex/conditions/date.ex Outdated Show resolved Hide resolved
@gen1321
Copy link
Author

gen1321 commented Nov 12, 2018

First of all thanks for a great review! It's a bit unclear to me how exactly you want to name/place encoders, my guess is:

  1. Create encoders folders.
  2. Move lib/encoder.ex to lib/encoders/fragment_encoder.ex folder with Filtrex.Encoders.FragmentEncoder
  3. Add llb/encoders/map_encoder with Filtrex.Encoders.MapEncoder inside
  4. Rename Filtrex.Utils.Encoder to Filtrex.Utils.FragmentEncoderDSL

@rcdilorenzo
Copy link
Owner

Yes. That sounds good with one minor tweak on the names. Let's use Filtrex.Encoders.Fragment and Filtrex.Encoders.Map to reduce some verbosity. That means they would go in lib/encoders/fragment.ex and lib/encoders/map.ex respectively.

@gen1321
Copy link
Author

gen1321 commented Nov 18, 2018

About ecto type, how can we pass config to cast function of ecto type? As I can understand we probably need to implement custom casting function like cast_filter(changeset, key, config). Anyway, maybe it's a good idea to separate this into another PR?

@rcdilorenzo
Copy link
Owner

@gen1321 Ah, I hadn't thought about that. 🤔 I think we'll probably have to have a module that allows you to define your own type that uses a hardcoded configuration (or calls a module). For example, the helper module might be Filtrex.Ecto.Type but a custom module would be MyApp.Filtrex.UserType. Regardless, I agree that it should be a separate pull request.

@gen1321
Copy link
Author

gen1321 commented Jan 21, 2019

Is there something left? looks done to me

Copy link
Owner

@rcdilorenzo rcdilorenzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So sorry that I haven't circled back on this. Last semester of a graduate degree will do that to a person... 😄 These are the final changes I think are necessary. I'll try to be quicker to respond as you make changes. Thanks for sticking it out!

lib/filtrex/encoders/map.ex Outdated Show resolved Hide resolved
test/conditions/datetime_test.exs Outdated Show resolved Hide resolved
test/conditions/datetime_test.exs Outdated Show resolved Hide resolved
test/conditions/text_test.exs Outdated Show resolved Hide resolved
%{
"type" => type,
"conditions" => Enum.map(conditions, &Filtrex.Condition.encode_condition/1),
"sub_filters" => Enum.map(sub_filters, &encode_sub_filters/1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because filtrex elsewhere allows for an arbitrary level of recursion for sub_filters, let's make sure this is consistent with this encoding feature being added. Checkout schema.json for the definition.

README.md Outdated Show resolved Hide resolved
@gen1321
Copy link
Author

gen1321 commented Feb 10, 2019

@rcdilorenzo done

]
},
],
"sub_filters" => []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you actually seen the recursion working properly? It looks like the code is right, but it might be worth it to throw in an example where we have a sub-filter that has to be dumped. (I'll do a final review to make sure we didn't miss anything, but everything in the new commits look good.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants