Documentation
¶
Overview ¶
Apply a post processing filters to the Datastore/Firestore results mapped in struct with json tag or not.
The filter format is designed to be passed in API param (query or path). The filters can express compound operation.
During the processing the values to filter (an array) is passed to the library to apply the filters. A filter can be composed to several part:
- Several filter elements
- Each filter element have a tree path into the JSON, name the key, an operator and the value(s) to compare
Each filter element must return OK for keeping the entry value. For this, 4 operators are allowed:
- The equality, comparable to IN sql clause: at least one value must matches. Default operator is `=`
- The not equality, comparable to NOT IN sql clause: all values mustn't match. Default operator is `!=`
- The Greater Than: only one numeric can be compared. Default operator is `>`
- The Lower Than: only one numeric can be compared. Default operator is `<`
It's possible to combine operators on the same key, for example k1 < 10 && k1 != 2. The same operator on the same key will raise an error.
The filters are applicable on this list types and structures (and combination possibles):
- simple types
- string
- int
- float
- bool
- Complex type
- pointer (invisible in JSON result but your structure can include filters)
- struct
- array
- of simple types
- of map
- of array
- of pointer
- map
- of simple types
- of map
- of array
- of pointer
This library works with Go app and use reflection. It performs 3 things
- Check if the provided filter is valid.
- Compile the filter according with the data structure to filter -> Validate the filter against the structure to filter.
- Apply the filter to the array of structure.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Filter ¶
type Filter struct {
// contains filtered or unexported fields
}
Filter structure to use for filtering. Init the default value like this
filter := jsonFilter.Filter{}
func (*Filter) ApplyFilter ¶
Apply the initialized Filter to a list (array) of struct. The type of array elements is the same as this one provided in the Init method. The entries must be an array.
Return an array with only the matching entries, else an error is returned.
Cast the return in the array type like this:
ret, err := filter.ApplyFilter(results) if err != nil { // Perform error handling fmt.Println(err) return } // Cast here the return results = ret.([]structExample)
func (*Filter) Init ¶
Initialize the filter with the requested filter and the struct on which to apply later the filter
The filter parsing and compilation are saved in the Filter struct.
Errors are returned in case of:
- Duplicated entry in the filter key name for the same operator
- Violation of filter format:
- No values for a key
- No key for a filter
- More than 1 value for Greater Than and Lower than operator
- Not a numeric (float compliant) value for Greater Than and Lower than operator
- Filter key not exist in the provided interface
- Struct field name not match the filter key
- Struct json tag not match the filter key
func (*Filter) SetOptions ¶
Set the option to the filter.
If the option is nil, the default option will be used.
If there is some missing or incorrect value to the defined option, a warning message is displayed and the erroneous part is replace by the default ones.
To set option:
filter := jsonFilter.Filter{} o := &jsonFilter.Options{ MaxDepth: 4, EqualKeyValueSeparator: "=", GreaterThanKeyValueSeparator: ">", LowerThanKeyValueSeparator: "<", NotEqualKeyValueSeparator: "!=", ValueSeparator: ",", KeysSeparator: ":", ComposedKeySeparator: "->", } filter.SetOptions(o)
type Options ¶
type Options struct { // Limit the depth of the key search. In case of complex object, can limit the compute resources. 0 means infinite. Default is '0' MaxDepth int // Character(s) to separate key (filter name) from values (value to compare) for an equal comparison. Default is '=' EqualKeyValueSeparator string // Character(s) to separate key (filter name) from values (value to compare) for a greater than comparison. Default is '>' GreaterThanKeyValueSeparator string // Character(s) to separate key (filter name) from values (value to compare) for a lower than comparison. Default is '<' LowerThanKeyValueSeparator string // Character(s) to separate key (filter name) from values (value to compare) for a not equal comparison. Default is '!=' NotEqualKeyValueSeparator string // Character(s) to separate values (value to compare). Default is ',' ValueSeparator string // Character(s) to separate keys (filters name). Default is ':' KeysSeparator string // Character(s) to separate key part in case of composed key (filter.subfilter) . Default is '.' ComposedKeySeparator string }
Structure to define the option of the Filter.
You can customize it if you want. Else the default values are applied