alfred

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2022 License: MIT Imports: 10 Imported by: 0

README

alfred

workflow Go Report Go Version Go Reference

Alfred is a simple golang package to apply filtering and sorting options on slice of structs.

logo

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddToPSQLQuery

func AddToPSQLQuery(query string, opt Option) string

AddToPSQLQuery add filter to a pq query. SELECT *, count(*) OVER() FROM (<query>) AS query WHERE <filter1> ILIKE <value1> AND ... ORDER BY <sortBy> <order> LIMIT 1 OFFSET 1

Types

type Contain

type Contain struct {
	Param string
	Value string
}

func (Contain) Keep

func (f Contain) Keep(v any) (bool, error)

Keep return true if the struct v has valid fields value according to the filter.

func (Contain) ToSQL

func (f Contain) ToSQL() string

ToSQL return a SQL condition to be used in a SQL query : "Param <= Value".

type EQ

type EQ struct {
	Param string
	Value string
}

EQ for ==

func (EQ) Keep

func (f EQ) Keep(v any) (bool, error)

Keep return true if the struct v has valid fields value according to the filter.

func (EQ) ToSQL

func (f EQ) ToSQL() string

ToSQL return a SQL condition to be used in a SQL query : "Param = Value".

type ErrParamType

type ErrParamType struct {
	Param string
	Err   error
}

An ErrParamType is returned when failed to convert to a certain type a value of a filter.

func (ErrParamType) Error

func (e ErrParamType) Error() string

type Filter

type Filter interface {
	Keep(v any) (bool, error)
	ToSQL() string
}

Filter is the interface for a simple filter for filtering result values.

type Filters

type Filters []Filter

Filters for filtering result values.

func (Filters) Keep

func (fs Filters) Keep(v any) (bool, error)

Keep return true if the struct v has valid fields value according to applayable filters.

type GT

type GT struct {
	Param string
	Value string
}

GT for >

func (GT) Keep

func (f GT) Keep(v any) (bool, error)

Keep return true if the struct v has valid fields value according to the filter.

func (GT) ToSQL

func (f GT) ToSQL() string

ToSQL return a SQL condition to be used in a SQL query : "Param > Value".

type GTE

type GTE struct {
	Param string
	Value string
}

GTE for >=

func (GTE) Keep

func (f GTE) Keep(v any) (bool, error)

Keep return true if the struct v has valid fields value according to the filter.

func (GTE) ToSQL

func (f GTE) ToSQL() string

ToSQL return a SQL condition to be used in a SQL query : "Param >= Value".

type LT

type LT struct {
	Param string
	Value string
}

LT for <

func (LT) Keep

func (f LT) Keep(v any) (bool, error)

Keep return true if the struct v has valid fields value according to the filter.

func (LT) ToSQL

func (f LT) ToSQL() string

ToSQL return a SQL condition to be used in a SQL query : "Param < Value".

type LTE

type LTE struct {
	Param string
	Value string
}

LTE for <=

func (LTE) Keep

func (f LTE) Keep(v any) (bool, error)

Keep return true if the struct v has valid fields value according to the filter.

func (LTE) ToSQL

func (f LTE) ToSQL() string

ToSQL return a SQL condition to be used in a SQL query : "Param <= Value".

type Like

type Like struct {
	Param string
	Value string
}

Like filter to test if a string contains a substring.

func (Like) Keep

func (f Like) Keep(v any) (bool, error)

Keep return true if the struct v has valid fields value according to the filter.

func (Like) ToSQL

func (f Like) ToSQL() string

ToSQL return a SQL condition to be used in a SQL query : "Param ILIKE %Value%".

type Option

type Option struct {
	// pagination
	Limit  int
	Offset int

	// sorting
	Order  string
	SortBy string

	// filters
	Filters Filters
}

Option are filtering, sorting and pagination parameters.

func ParseURLValues

func ParseURLValues(values url.Values) Option

ParseURLValues parse filters from url values. The format is '?filter[<>][<type>]=<value>'. Possible type are : like, eq, gt, gte, lt, lte.

func (Option) Sort

func (flt Option) Sort(slice any)

It panics if x is not a slice of structs.

Jump to

Keyboard shortcuts

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