Biarity/Sieve

How to work with the array

rdvitka opened this issue · 12 comments

Hello,
How you can work with a array type filters.

&filters=LikeCount>10,Author==[Brad Pitt,Mel Gibson]

I am asking for a hint.

You'll have to write a custom filter for that. Here's an example for v>=1.3.7:

public class SieveCustomFilterMethods : ISieveCustomFilterMethods
{
    public IQueryable<Post> AuthorIsAnyOf(IQueryable<Post> source, string op, string value)
    {
        var result = source.Where(p => value.Split('|').Contains(p.Author));
        return result;
    }
}

Then inject the class: services.AddScoped<ISieveCustomFilterMethods, SieveCustomFilterMethods>();.

Now you can send requests in the form:

& AuthorIsAnyOf == Bad Pitt|Mel Gibson

Note I'm using the pipe character (|) instead of a comma because Sieve uses commas to separate filter terms.

I will be closing this issue. If you think this does not fit your use case please let me know - I could put in the time for creating native enumerable search if required.

Sorry for comment in closed issue but what about this feature - native enumerable search?
Custom filters are not really good idea for this case, because it's not a clear use case for clients.
I want to share my OpenAPI schema and describe how to work with filters and that's all.

Yeah I've ran into this issue. Will look more into this once I get some free time.

This sound like a very important feature to me, did you had any time to work over this by chance ?

A bit inclined to look at this, any issues with me submitting a PR if I manage?
Thinking the most flexible of scenarios would be to allow each array entry to have it's own filter, to allow for stuff like &filters=LikeCount>10,Author==[_=Brad P|@=Mel|Angelina Jolie]. Possibly leaving the nested operators optional, like the "Angelina Jolie" entry, and in that case using the operator specified by Author, i.e. ==.

Any opinions?

@maxstralin Sounds good, feel free to submit a PR. Unfortunately I'll be very busy in the coming days so unlikely to implement this myself in the near future.

@maxstralin Any progress on this?

@hasanmanzak Gave up on it back in the days of 2019 but got an urge now to get back to it when you pinged me. I have a dirty proof of concept in https://github.com/maxstralin/Sieve/tree/array-filtering, which would only allow for basic "contains" (case-sensitive) in an IEnumerable<string> property. It needs refactoring and improvement but thought I'd post the update at least.

See CanFilterArray test in General.cs tests for the unit test.
ConvertFilterValueToExpression in SieveProcessor.cs for the method containing the logic.

I think we should discuss an API structure then start to work on it :) I just didn't want to dictate any structure I please.

Feel free, if you want to take it then you can take it too haha ;)
Depending on the scope you want, it needs more or less refactoring. E.g. having operators per value, like I suggested in 2019 (Author==[_=Brad P|@=Mel|Angelina Jolie]) doesn't work with the current code as it picks up the Brad P operator instead of ==. However, individual operators per value is probably secondary as the use case is probably not the most common one.

Anyhow, either you do it do a larger refactoring immediately, or you get a working variant up and running, then improve it.