popf

package
v0.0.0-...-98c8157 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2018 License: GPL-3.0 Imports: 2 Imported by: 0

README

POPF

An orm filter for gobuffalo/pop


// Filter struct holds all the filter rules that will be
// used in the *pop.Query or *pop.Connection.
type Filter struct {
	// FormParams holds the filter rules that will be used
	// only if ormf.Rule.CheckParam returns no error.
	FromParams []ormf.Rule
	// DefaultParams holds the filter rules that will be used every time.
	// If the ormf.Rule.CheckParam returns an error the filter will use the default value (ormf.Rule.Value).
	DefaultParams []ormf.Rule
	// MustFiler holds the rules used every time using ormf.Rule.Value (won't check the omrf.Rule.CheckParam).
	MustFilter []ormf.Rule
}

// FilterParams is the provider from which the filter will get the values passing ormf.Rule.Param
type FilterParams interface {
	Get(string) string
}
How to use

Say you have a model

type User struct {
    Name   string // ...
    Age    int
    Active bool
}

Somewhere in your action package you create a popf.Filter

userFilter := popf.Filter{
    FromParams: ormf.Rules{
        ormf.Rule{"age", "<", "max_age", 0, ormf.IntField},
        ormf.Rule{"lower(name)", "~", "name_like", "", ormf.StringField},
    },
    DefaultParams: ormf.Rules{
        ormf.Rule{"age", ">", "min_age", 17, ormf.IntField},
    },
    MustFilter: ormf.Rules{
        ormf.Rule{"active", "=", "", true, ormf.BoolField},
    },
}

Now inside an action where you want to filter the users

params := c.GetParams()
users := models.Users{}
tx := c.Value("tx").(*pop.Connection)
userFilter.FilterConnection(tx, params).All(&users) // or you can use userFilter.FilterQuery(tx, params) if tx is a *pop.Query
// Now you can do something with the filtered set of users

So depending on the query parameters you'll get differnt set of users.

query used filter
?max_age=50 all users have age < 50
?max_age=sd no filter used (it's IntField)
?name_like=is all users have lower(name) ~ is
?min_age=20 all users have age > 20
?min_age=sdf (or no min_age specified) all users have age > 17 (default value)
(for every query) all users have active = true (it's a must filter)

First version

Documentation

Overview

Package popf is an orm filter for gobuffalo/pop.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Filter

type Filter struct {
	// FormParams holds the filter rules that will be used
	// only if ormf.Rule.CheckParam returns no error.
	FromParams ormf.Rules
	// DefaultParams holds the filter rules that will be used every time.
	// If the ormf.Rule.CheckParam returns an error the filter will use the default value (ormf.Rule.Value).
	DefaultParams ormf.Rules
	// MustFiler holds the rules used every time using ormf.Rule.Value (won't check the omrf.Rule.CheckParam).
	MustFilter ormf.Rules
}

Filter struct holds all the filter rules that will be used in the *pop.Query or *pop.Connection.

func (Filter) FilterConnection

func (f Filter) FilterConnection(c *pop.Connection, fp FilterParams) *pop.Query

FilterConnection will filter the *pop.Connection passed.

func (Filter) FilterQuery

func (f Filter) FilterQuery(q *pop.Query, fp FilterParams) *pop.Query

FilterQuery will filter the *pop.Query passed.

type FilterParams

type FilterParams interface {
	Get(string) string
}

FilterParams is the provider from which the filter will get the values passing ormf.Rule.Param

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL