Sieve icon indicating copy to clipboard operation
Sieve copied to clipboard

How to work with the array

Open rdvitka opened this issue 7 years ago • 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.

rdvitka avatar Feb 07 '18 19:02 rdvitka

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.

Biarity avatar Feb 07 '18 23:02 Biarity

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.

Biarity avatar Feb 14 '18 04:02 Biarity

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.

elmankross avatar Aug 31 '18 21:08 elmankross

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

Biarity avatar Oct 01 '18 05:10 Biarity

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

Rakiah avatar May 23 '19 12:05 Rakiah

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 avatar Sep 12 '19 10:09 maxstralin

Ping @Biarity

maxstralin avatar Sep 23 '19 08:09 maxstralin

@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.

Biarity avatar Sep 24 '19 02:09 Biarity

@maxstralin Any progress on this?

hasanmanzak avatar Jan 07 '21 18:01 hasanmanzak

@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.

maxstralin avatar Jan 11 '21 19:01 maxstralin

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.

hasanmanzak avatar Jan 12 '21 17:01 hasanmanzak

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.

maxstralin avatar Jan 12 '21 18:01 maxstralin