ganigeorgiev / fexpr

Simple filter query language parser so that you can build SQL, Elasticsearch, etc. queries safely from user input.
BSD 3-Clause "New" or "Revised" License
101 stars 6 forks source link

How to pass values to an array operator? #1

Closed kolaente closed 10 months ago

kolaente commented 10 months ago

I'm trying to use the ?= operator with an expression like this: id ?= 1,2,34 - I want to filter everything which has an id of 1, 2 or 34. However, I get the error unexpected character ','. Using parenthesis or spaces does not work either.

How do I use this correctly? I was kind of expecting to be able to use it like sql's IN function. I could probably do something like id ?= '1,2,34' and then parsing the string out myself but that does not look like intended.

BTW, great library!

ganigeorgiev commented 10 months ago

fexpr doesn't have notation for an array or set of literals.

Depending on your use case a workaround could be to combine multiple expressions with ||, something like: id = 1 || id 2 || id = 34. The ? prefixed operators were added actually to indicate "any/at-least-one-of" for array-like fields (at least that is how it is used in PocketBase).

There are plans to make it modular to allow users to have more control over the allowed identifier characters and operators, but other tasks are higher priority at the moment.

kolaente commented 10 months ago

So the any/at-least-one-of part of the operator is meant for the field they're referring to and not the value?

ganigeorgiev commented 10 months ago

Yes. In the PocketBase context, if a field "users" is a multiple relation field, internally it is represented as serialized json array of ids and if we want to check if a value/literal matches a condition against a single entry from the arrayble field we use the ? prefixed operators. Here is one such pseudo-rule example:

users ?= 'a'

(where users in the above could be ["a", "b", "c"]; roughly it reads like: "any/at least one of the users matches with 'a'").

But again, the above operator prefix was added primarily for my use case in PocketBase and I could understand why it may not match your expectations. As mentioned earlier, I'll consider in the near future to make the package slightly more modular to allow users registering custom operators and identifier characters to match better with their use case.

kolaente commented 10 months ago

Thank you very much for the explanation! Makes more sense like that.

Would you accept a PR adding an in operator working like an sql one?

ganigeorgiev commented 10 months ago

No, as mentioned I think it will be better to make it modular/pluggable to allow users registering whatever operators they need instead of adding more to the hardcoded list. I'm currently busy with other tasks (see also https://github.com/pocketbase/pocketbase/discussions/3271), but will consider working on this in the near future because there were also requests in PocketBase for introducing has/in operator.

kolaente commented 10 months ago

Awesome, keep up the good work!