Documentation ¶
Index ¶
- Constants
- Variables
- func Generate(expr query.FilterExpression, q url.Values) error
- func Parse(query url.Values) (query.FilterExpression, error)
- func ParseWithTransform(query url.Values, transformer Transformer) (query.FilterExpression, error)
- type Parser
- type StrictTransformerMux
- type Transformer
- type TransformerFunc
- type TransformerMux
Constants ¶
const ( QueryKeyFilterExpression = "q" OperationAND = "and" OperationOR = "or" OperationNOT = "not" )
Variables ¶
var ( ErrEmptyQuery = errors.New("empty query") DefaultParser = Parser{ Transformer: PassthroughTransformer, } )
Functions ¶
func ParseWithTransform ¶
func ParseWithTransform(query url.Values, transformer Transformer) (query.FilterExpression, error)
Types ¶
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 ¶
func (m TransformerMux) Strict() StrictTransformerMux
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.