github.com/tobgu/qframe@v0.4.0/filter/filter.go (about) 1 package filter 2 3 import "fmt" 4 5 const ( 6 // Gt = Greater than. 7 Gt = ">" 8 9 // Gte = Greater than equals. 10 Gte = ">=" 11 12 // Eq = Equals. 13 Eq = "=" 14 15 // Neq = Not equals. 16 Neq = "!=" 17 18 // Lt = Less than. 19 Lt = "<" 20 21 // Lte = Less than equals. 22 Lte = "<=" 23 24 // In = In given set. 25 In = "in" 26 27 // Nin = Not in given set. 28 Nin = "not in" 29 30 // IsNull = Is null. 31 IsNull = "isnull" 32 33 // IsNotNull = IsNotNull. 34 IsNotNull = "isnotnull" 35 ) 36 37 // Inverse is a mapping from one comparator to its inverse. 38 var Inverse = map[string]string{ 39 Gt: Lte, 40 Gte: Lt, 41 Eq: Neq, 42 Lt: Gte, 43 Lte: Gt, 44 In: Nin, 45 Nin: In, 46 IsNotNull: IsNull, 47 IsNull: IsNotNull, 48 } 49 50 // Filter represents a filter to apply to a QFrame. 51 // 52 // Example using a built in comparator on a float column: 53 // Filter{Comparator: ">", Column: "COL1", Arg: 1.2} 54 // 55 // Same example as above but with a custom function: 56 // Filter{Comparator: func(f float64) bool { return f > 1.2 }, Column: "COL1"} 57 type Filter struct { 58 // Comparator may be a string referring to a built in or a function taking an argument matching the 59 // column type and returning a bool bool. 60 // 61 // IMPORTANT: For pointer and reference types you must not assume that the data passed argument 62 // to this function is valid after the function returns. If you plan to keep it around you need 63 // to take a copy of the data. 64 Comparator interface{} 65 66 // Column is the name to filter by 67 Column string 68 69 // Arg is passed as argument to built in functions. 70 Arg interface{} 71 72 // Inverse can be set to true to negate the filter. 73 Inverse bool 74 } 75 76 // String returns a string representation of the filter. 77 func (f Filter) String() string { 78 arg := f.Arg 79 if s, ok := f.Arg.(string); ok { 80 arg = fmt.Sprintf(`"%s"`, s) 81 } 82 83 s := fmt.Sprintf(`["%v", "%s", %v]`, f.Comparator, f.Column, arg) 84 if f.Inverse { 85 return fmt.Sprintf(`["!", %s]`, s) 86 } 87 return s 88 }