-
Notifications
You must be signed in to change notification settings - Fork 20
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
Build array filter #15
Conversation
…r_field is an array type and reduces to where(^val in field)
…nyone was using ecto shorts to do a nil, not nil, or not equal on an array field
end | ||
|
||
def build_array(query, filter_field, filters) when is_map(filters) do | ||
build(query, filter_field, filters) |
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.
What's the behaviour here? If it's a map what happens?
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.
This is to ensure standard filter behaviour. If a map is passed we send the call to build where the is_map guard will pick it up and do the standard build_subfield_filter reduce.
This is to ensure we can still make calls to check if the field is {:==, nil}, {:!=, nil}, {:!=, val}
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.
How does the current code handle this? And how would it need to change since the code that previously did this will no longer be doing this from what I gather? Should we change the method name to reflect that it's not just buliding and array?
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.
So this is preserving the behaviour of the current code
CommonFilters.convert_params_to_filter(User, %{array_field: %{!=: nil}})
in the current code this would go to the build function and get picked up by the is_map guard
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.
Okay cool! Looks good!
Changes to query builder to handle an array field in a schema.
Currently EctoShorts handles conversions the following way when dealing with an array field
CommonFilters.convert_params_to_filter(User, %{array_field: :value})
=>from u0 in Schemas.User, where: u0.array_field == ^:value, select: u0
CommonFilters.convert_params_to_filter(User, %{array_field: [:value]})
=>from u0 in Schemas.User, where: u0.array_field in ^[:value], select: u0
Both of which error out
Now EctoShorts will do the following conversions
CommonFilters.convert_params_to_filter(User, %{array_field: :value})
=>from u0 in Schemas.User, where: ^:value in u0.array_field, select: u0
CommonFilters.convert_params_to_filter(User, %{array_field: [:value]})
=>from u0 in Schemas.User, where: u0.array_field == ^[:value], select: u0