filter

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Constants

View Source
const (
	QueryKeyFilterExpression = "q"
	OperationAND             = "and"
	OperationOR              = "or"
	OperationNOT             = "not"
)

Variables

View Source
var (
	ErrEmptyQuery = errors.New("empty query")

	DefaultParser = Parser{
		Transformer: PassthroughTransformer,
	}
)

Functions

func Generate

func Generate(expr query.FilterExpression, q url.Values) error

func Parse

func Parse(query url.Values) (query.FilterExpression, error)

func ParseWithTransform

func ParseWithTransform(query url.Values, transformer Transformer) (query.FilterExpression, error)

Types

type Parser

type Parser struct {
	Transformer Transformer
}

func (Parser) ParseFilterQuery

func (Parser) ParseFilterQuery(r *http.Request) (query.FilterExpression, error)

type StrictTransformerMux

type StrictTransformerMux struct {
	// contains filtered or unexported fields
}

StrictTransformerMux is like TransformerMux but it returns an error if no transformer is found for a given property.

func (StrictTransformerMux) TransformCriteria

func (m StrictTransformerMux) TransformCriteria(c *query.Filter) (query.FilterExpression, error)

TransformCriteria implements the Transformer interface.

type Transformer

type Transformer interface {
	// TransformCriteria transforms the given criteria into another expression.
	// When the criteria should not be transformed, it returns the original criteria.
	// If an error occurs during transformation, it returns the error.
	TransformCriteria(*query.Filter) (query.FilterExpression, error)
}

Transformer can transform query.Filter criteria into another query.FilterExpression. This is useful when decoding input criteria into more complex filters.

var PassthroughTransformer Transformer = TransformerFunc(func(c *query.Filter) (query.FilterExpression, error) {
	return c, nil
})

PassthroughTransformer is a transformer that passes the criteria through without any transformation.

type TransformerFunc

type TransformerFunc func(*query.Filter) (query.FilterExpression, error)

TransformerFunc is a function that implements the Transformer interface.

Example:

var uppercase TransformerFunc = func(c *query.Filter) (query.FilterExpression, error) {
  c.Value = strings.ToUpper(c.Value.(string))
  return c, nil
}

func (TransformerFunc) TransformCriteria

func (f TransformerFunc) TransformCriteria(c *query.Filter) (query.FilterExpression, error)

TransformCriteria implements the Transformer interface.

type TransformerMux

type TransformerMux map[string]Transformer

TransformerMux combines several Transformer instances into a single one. Each transformer is mapped to a unique key, which will be invoked when the key matches the [Criteria] property value. If no transformer is found for the given property, the PassthroughTransformer is used.

Example:

mux := TransformerMux{
  "name": TransformerFunc(func(c *query.Filter) (query.FilterExpression, error) {
    c.Value = strings.ToUpper(c.Value.(string))
    return c, nil
  }),
}
mux.TransformCriteria(&Criteria{Property: "name", Value: "john"}) // returns the transformed criteria
mux.TransformCriteria(&Criteria{Property: "age", Value: 30}) // returns the original criteria

func (TransformerMux) Strict

Strict returns a StrictTransformerMux that returns an error if no transformer is found for a given property.

func (TransformerMux) TransformCriteria

func (m TransformerMux) TransformCriteria(c *query.Filter) (query.FilterExpression, error)

TransformCriteria implements the Transformer interface.

Jump to

Keyboard shortcuts

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