Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶
Filter filters the input slice using the filter, storing the result in output. Input must be a slice of filterable elements (a struct) and output must be a pointer to a slice of identical type. If the filter contains fields not present in the struct or values that aren't compatible with corresponding field, an error is returned. If a filter is encountered that is not from the squirrel package, it is only used if it implements ValueFilterer
Example ¶
package main import ( "fmt" "github.com/Masterminds/squirrel" "github.com/pixelrazor/sqlice" ) func main() { type FooBar struct { A int B string `db:"bar"` } input := []FooBar{ {A: 2, B: "b"}, {A: 4, B: "d"}, {A: 1, B: "a"}, {A: 5, B: "e"}, {A: 3, B: "c"}, } var output []FooBar err := sqlice.Filter(input, &output, squirrel.And{ squirrel.Gt{"A": 1}, squirrel.Lt{"bar": "e"}, }) if err != nil { panic(err) } fmt.Println(output) }
Output: [{2 b} {4 d} {3 c}]
Types ¶
type ValueFilterFunc ¶
type ValueFilterFunc func(interface{}) bool
ValueFilterFunc is an adapter that allows a function to be used as a custom filter to Filter. It implements the squirrel.Sqlizer interface to satisfy requirements, but the implementation of ToSql will always return an error
Example ¶
package main import ( "fmt" "sort" "github.com/pixelrazor/sqlice" ) func main() { type FooBar struct { Things []int } filterFunc := func(i interface{}) bool { return sort.IntsAreSorted((i.(FooBar).Things)) } input := []FooBar{ {Things: []int{3, 8, 1}}, {Things: []int{1, 2, 3}}, {Things: []int{5, 3, 1}}, {Things: []int{5, 8, 9}}, } var output []FooBar err := sqlice.Filter(input, &output, sqlice.ValueFilterFunc(filterFunc)) if err != nil { panic(err) } fmt.Println(output) }
Output: [{[1 2 3]} {[5 8 9]}]
func (ValueFilterFunc) FilterValue ¶
func (vff ValueFilterFunc) FilterValue(i interface{}) bool
FilterValue calls vff(i)
func (ValueFilterFunc) ToSql ¶
func (vff ValueFilterFunc) ToSql() (string, []interface{}, error)
ToSql always returns an error when called
type ValueFilterer ¶
type ValueFilterer interface {
FilterValue(interface{}) bool
}
ValueFilterer is the interface that wraps the FilterValue method. FilterValue is given a value from slice of elements, and should return true if it is to be included.