filter

package
v3.7.8 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2022 License: LGPL-2.1 Imports: 14 Imported by: 0

README

Filter Syntax

Pivot uses a simplified filter syntax that is URL-friendly and fairly straightforward to use. It consists of a sequence of field/value pairs that are themselves separated by a forward slash. Generally, the best way to learn this syntax is through examples:

Examples

# Where field "id" is exactly equal to 123
id/123

# Where "id" is exactly 3, 14, OR 27
id/3|14|27

# Where "id" is exactly 3, 14, OR 27 AND "enabled" is true
id/3|14|27/enabled/true

# Where "name" is "Bob" AND "age" is 42.
name/Bob/age/42

# Where "product" contains the string "usb" and "price" is less than 5.00
product/contains:usb/price/lt:5.00

# Where "product" contains the string "usb" and "price" is between 10.00 (inclusive) and 20.01 (exclusive)
product/contains:usb/price/range:10|20.01

General Form

The general form of this filter syntax is:

[[type:]field/[operator:]value[|orvalue ..] ..]

Operators

Supported operators are as follows:

Operator Description
is Values must exactly match (this is the default if no operator is provided)
not Values may be anything except an exact match
contains String value must contain the given substring (case sensitive)
like String value must contain the given substring (case insensitive)
unlike String value may not contain the given substring (case insensitive)
prefix String value must start with the given string
suffix String value must end with the given string
gt Numeric or date value must be strictly greater than
gte Numeric or date value must be strictly greater than or equal to
lt Numeric or date value must be strictly less than
lte Numeric or date value must be strictly less than or equal to
range Numeric or date value must be between two values (separated by `

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllValue = `all`
View Source
var CriteriaSeparator = `/`
View Source
var DefaultIdentityField = `id`
View Source
var DefaultNormalizerFunc = func(in string) string {
	in = strings.ToLower(in)
	return rxCharFilter.ReplaceAllString(in, ``)
}
View Source
var FieldLengthDelimiter = `#`
View Source
var FieldTermSeparator = `/`
View Source
var ModifierDelimiter = `:`
View Source
var QueryUnescapeValues = false
View Source
var SortAscending = `+`
View Source
var SortDescending = `-`
View Source
var ValueSeparator = `|`

Functions

func IsExactMatchOperator

func IsExactMatchOperator(operator string) bool

func IsInvertingOperator

func IsInvertingOperator(operator string) bool

func Render

func Render(generator IGenerator, collectionName string, filter *Filter) ([]byte, error)

func SplitModifierToken

func SplitModifierToken(in string) (string, string)

Types

type Aggregate

type Aggregate struct {
	Aggregation Aggregation
	Field       string
}

type Aggregation

type Aggregation int
const (
	First Aggregation = iota
	Last
	Minimum
	Maximum
	Sum
	Average
	Count
)

type ConjunctionType

type ConjunctionType string
const (
	AndConjunction ConjunctionType = ``
	OrConjunction                  = `or`
)

type Criterion

type Criterion struct {
	Type        dal.Type      `json:"type,omitempty"`
	Length      int           `json:"length,omitempty"`
	Field       string        `json:"field"`
	Operator    string        `json:"operator,omitempty"`
	Values      []interface{} `json:"values"`
	Aggregation Aggregation   `json:"aggregation,omitempty"`
}

func (*Criterion) IsExactMatch

func (self *Criterion) IsExactMatch() bool

func (*Criterion) String

func (self *Criterion) String() string

type Filter

type Filter struct {
	Spec          string
	MatchAll      bool
	Offset        int
	Limit         int
	Criteria      []Criterion
	Sort          []string
	Fields        []string
	Options       map[string]interface{}
	Paginate      bool
	IdentityField string
	Normalizer    NormalizerFunc `json:"-" bson:"-" pivot:"-"`
	Conjunction   ConjunctionType
}

func All

func All() *Filter

func Copy

func Copy(other *Filter) Filter

func FromMap

func FromMap(in map[string]interface{}) (*Filter, error)

func MakeFilter

func MakeFilter(specs ...string) Filter

func MustParse

func MustParse(in interface{}) *Filter

func New

func New() *Filter

func Null

func Null() *Filter

func Parse

func Parse(in interface{}) (*Filter, error)

func ParseSpec

func ParseSpec(spec string) (*Filter, error)

Filter syntax definition

func (*Filter) AddCriteria

func (self *Filter) AddCriteria(criteria ...Criterion) *Filter

func (*Filter) ApplyOptions

func (self *Filter) ApplyOptions(in interface{}) error

func (*Filter) BoundedBy

func (self *Filter) BoundedBy(limit int, offset int) *Filter

func (*Filter) CriteriaFields

func (self *Filter) CriteriaFields() []string

func (*Filter) GetFirstValue

func (self *Filter) GetFirstValue() (interface{}, bool)

func (*Filter) GetIdentityValue

func (self *Filter) GetIdentityValue() (interface{}, bool)

func (*Filter) GetSort

func (self *Filter) GetSort() []SortBy

func (*Filter) GetValues

func (self *Filter) GetValues(field string) ([]interface{}, bool)

func (*Filter) IdOnly

func (self *Filter) IdOnly() bool

func (*Filter) IsMatchAll

func (self *Filter) IsMatchAll() bool

func (*Filter) MatchesRecord

func (self *Filter) MatchesRecord(record *dal.Record) bool

func (*Filter) NewFromMap

func (self *Filter) NewFromMap(in map[string]interface{}) (*Filter, error)

func (*Filter) NewFromSpec

func (self *Filter) NewFromSpec(specs ...string) (*Filter, error)

func (*Filter) SortBy

func (self *Filter) SortBy(fields ...string) *Filter

func (*Filter) String

func (self *Filter) String() string

func (*Filter) WithFields

func (self *Filter) WithFields(fields ...string) *Filter

type Generator

type Generator struct {
	IGenerator
	// contains filtered or unexported fields
}

func (*Generator) Finalize

func (self *Generator) Finalize(_ *Filter) error

func (*Generator) Payload

func (self *Generator) Payload() []byte

func (*Generator) Push

func (self *Generator) Push(data []byte)

func (*Generator) Reset

func (self *Generator) Reset()

func (*Generator) Set

func (self *Generator) Set(data []byte)

type IGenerator

type IGenerator interface {
	Initialize(string) error
	Finalize(*Filter) error
	Push([]byte)
	Set([]byte)
	Payload() []byte
	WithCriterion(Criterion) error
	OrCriterion(Criterion) error
	WithField(string) error
	GroupByField(string) error
	AggregateByField(Aggregation, string) error
	SetOption(string, interface{}) error
	GetValues() []interface{}
	Reset()
}

type NormalizerFunc

type NormalizerFunc func(in string) string // {}

type SortBy

type SortBy struct {
	Field      string
	Descending bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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