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

Build array filter #15

Merged
merged 3 commits into from
May 4, 2022
Merged

Build array filter #15

merged 3 commits into from
May 4, 2022

Conversation

hlista
Copy link
Contributor

@hlista hlista commented Apr 26, 2022

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

…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)
Copy link
Owner

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?

Copy link
Contributor Author

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}

Copy link
Owner

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?

Copy link
Contributor Author

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

Copy link
Owner

Choose a reason for hiding this comment

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

Okay cool! Looks good!

@MikaAK MikaAK merged commit 9fd48e1 into MikaAK:main May 4, 2022
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