pop icon indicating copy to clipboard operation
pop copied to clipboard

Filter the result from params

Open isgj opened this issue 6 years ago • 5 comments

New feature. The idea is to create a FilterStruct that can be used to filter the result of a query using the request's query params. The dev can give the rules to the structure and use it on the action he/she wants.

type FilterStruct struct {
    OnlyParams   [] FilterRuleParams
    WithDefaults [] FilterRuleWithDefault
    MustFilter   [] FilterMust
}
  • OnlyParams will hold the rules that take the model field, the param field to look up and a FieldRule
  • WithDefaults will hold the rules that take the model field, the param field, the FildRule and a default value that will be used if no param field was found in the request
  • MustFilter will hold the rules that take the model field, the FieldRule and the value (so these rules won't look to the request query params)

The FieldRule can be some constants like MinInt, Bool, MaxLength... and translate after to SQL

Paginator can be used as a base to wire this with pop/{Connection, Query} Example: django-filters

isgj avatar Jun 19 '18 10:06 isgj

This can kinda be done with Scoping...this just seems a tad heavy to me...unless I am missing something...

robbyoconnor avatar Jun 19 '18 18:06 robbyoconnor

I feel identified with this solution, and actually, we're doing something similar in our apps, I feel Scoping provides the base to build something like this but something like this FilterStruct could provide an upper level (actions level) and a generalization for filtering lists across the app.

paganotoni avatar Jun 19 '18 18:06 paganotoni

@robbyoconnor now that I noticed this is in the pop repository, I'm not so positive about the idea, maybe we could build a filtering package that translates the filtering struct into a pop scope.

I somehow thought this was in the gobuffalo/buffalo repo

paganotoni avatar Jun 19 '18 18:06 paganotoni

The idea of Scoping is a little bit different from Filtering. Usually when you scope, you filter with a know value, in filtering you get the values from the request params. Of course you can use Scope for Filtering

maybe we could build a filtering package that translates the filtering struct into a pop scope

Since what the Scope method does is execute a function that will use Where why not using Where directly.

now that I noticed this is in the pop repository, I'm not so positive about the idea

I think too that it should be on a different package

isgj avatar Jun 19 '18 21:06 isgj

Hey there. I already wrote a filter package. You can find it here. The README file has a how to but it's pretty simple. Any thoughts and/or suggestions are welcome. As for the pop package maybe the *Query can add a method Filter like

// Same as PaginationParams interface
type ParamProvider interface {
        Get(string) string
}

type Filterer interface {
        FilterQuery(*Query,  ParamProvider) *Query
}

func (q *Qurery) Filter(f Filterer, p ParamProvider) *Query {
        return f.FilterQuey(q, p)
}

Like this there are no external imports and the *Query can use the filter of your choice.

isgj avatar Jun 22 '18 12:06 isgj