-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Binding: add filter builder. (#463)
Add support for building filter queries. Example: ``` filter = binding.Filter{} filter.And("name").Eq("Elmer") filter.And("age").Gt(10) filter.And("height").Lt(44) filter.And("weight").LtEq(150) filter.And("hair").NotEq("blond") filter.And("pet").Like("Rov*") filter.And("friend").Eq(Any{"Sam","Ed"}) p := filter.Param() client.Get(path, object, p) ``` Adds binding.RuleSet.Find() needed by analyzer addon to get rulesets for labels. Example: ``` filter.And("labels").Eq(labels) ``` --------- Signed-off-by: Jeff Ortel <jortel@redhat.com>
- Loading branch information
Showing
5 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package filter | ||
|
||
/* | ||
filter = Filter{} | ||
filter.And("name").Eq("Elmer") | ||
filter.And("age").Gt(10) | ||
filter.And("height").Lt(44) | ||
filter.And("weight").LtEq(150) | ||
filter.And("hair").NotEq("blond") | ||
filter.And("pet").Like("Rov*") | ||
filter.And("friend").Eq(Any{"Sam","Ed"}) | ||
*/ | ||
|
||
import ( | ||
qf "github.com/konveyor/tackle2-hub/api/filter" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
EQ = string(qf.EQ) | ||
NOT = string(qf.NOT) | ||
GT = string(qf.GT) | ||
LT = string(qf.LT) | ||
LIKE = string(qf.LIKE) | ||
AND = string(qf.AND) | ||
OR = string(qf.OR) | ||
) | ||
|
||
// | ||
// Any match any. | ||
type Any []interface{} | ||
|
||
// | ||
// All match all. | ||
type All []interface{} | ||
|
||
// | ||
// Filter builder. | ||
type Filter struct { | ||
predicates []*Predicate | ||
} | ||
|
||
// | ||
// And adds a predicate. | ||
// Example: filter.And("name").Equals("Elmer") | ||
func (f *Filter) And(field string) (p *Predicate) { | ||
p = &Predicate{ | ||
field: field, | ||
operator: EQ, | ||
} | ||
f.predicates = append(f.predicates, p) | ||
return p | ||
} | ||
|
||
// | ||
// String returns string representation. | ||
func (f *Filter) String() (s string) { | ||
var preds []string | ||
for _, p := range f.predicates { | ||
preds = append(preds, p.String()) | ||
} | ||
s = strings.Join(preds, string(qf.COMMA)) | ||
return | ||
} | ||
|
||
// | ||
// Predicate is a filter query predicate. | ||
type Predicate struct { | ||
field string | ||
operator string | ||
value string | ||
} | ||
|
||
// | ||
// String returns a string representation of the predicate. | ||
func (p *Predicate) String() (s string) { | ||
s = p.field + p.operator + p.value | ||
return | ||
} | ||
|
||
// | ||
// Eq returns a (=) predicate. | ||
func (p *Predicate) Eq(object interface{}) *Predicate { | ||
p.operator = EQ | ||
p.value = p.valueOf(object) | ||
return p | ||
} | ||
|
||
// | ||
// NotEq returns a (!=) predicate. | ||
func (p *Predicate) NotEq(object interface{}) *Predicate { | ||
p.operator = NOT + EQ | ||
p.value = p.valueOf(object) | ||
return p | ||
} | ||
|
||
// | ||
// Like returns a (~) predicate. | ||
func (p *Predicate) Like(object interface{}) *Predicate { | ||
p.operator = LIKE | ||
p.value = p.valueOf(object) | ||
return p | ||
} | ||
|
||
// | ||
// Gt returns a (>) predicate. | ||
func (p *Predicate) Gt(object interface{}) *Predicate { | ||
p.operator = GT | ||
p.value = p.valueOf(object) | ||
return p | ||
} | ||
|
||
// | ||
// GtEq returns a (>=) predicate. | ||
func (p *Predicate) GtEq(object interface{}) *Predicate { | ||
p.operator = GT + EQ | ||
p.value = p.valueOf(object) | ||
return p | ||
} | ||
|
||
// | ||
// Lt returns a (<) predicate. | ||
func (p *Predicate) Lt(object interface{}) *Predicate { | ||
p.operator = LT | ||
p.value = p.valueOf(object) | ||
return p | ||
} | ||
|
||
// | ||
// LtEq returns a (<) predicate. | ||
func (p *Predicate) LtEq(object interface{}) *Predicate { | ||
p.operator = LT + EQ | ||
p.value = p.valueOf(object) | ||
return p | ||
} | ||
|
||
func (p *Predicate) valueOf(object interface{}) (result string) { | ||
kind := reflect.TypeOf(object).Kind() | ||
value := reflect.ValueOf(object) | ||
switch kind { | ||
case reflect.String: | ||
result = "'" + value.String() + "'" | ||
case reflect.Int, | ||
reflect.Int8, | ||
reflect.Int16, | ||
reflect.Int32, | ||
reflect.Int64: | ||
n := value.Int() | ||
result = strconv.Itoa(int(n)) | ||
case reflect.Bool: | ||
result = strconv.FormatBool(value.Bool()) | ||
case reflect.Slice: | ||
var items []string | ||
for i := 0; i < value.Len(); i++ { | ||
item := p.valueOf(value.Index(i).Interface()) | ||
items = append(items, item) | ||
} | ||
var operator string | ||
switch object.(type) { | ||
case Any: | ||
operator = OR | ||
case All: | ||
operator = AND | ||
default: | ||
operator = OR | ||
} | ||
result = strings.Join(items, operator) | ||
result = "(" + result + ")" | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package filter | ||
|
||
import ( | ||
"github.com/onsi/gomega" | ||
"testing" | ||
) | ||
|
||
func TestFilter(t *testing.T) { | ||
g := gomega.NewGomegaWithT(t) | ||
filter := Filter{} | ||
list := Any{"One", "Two", "Three"} | ||
filter.And("name").Eq(list) | ||
p := filter.String() | ||
g.Expect("name=('One'|'Two'|'Three')").To(gomega.Equal(p)) | ||
|
||
filter = Filter{} | ||
filter.And("name").Eq(All{"One", "Two", "Three"}) | ||
p = filter.String() | ||
g.Expect("name=('One','Two','Three')").To(gomega.Equal(p)) | ||
|
||
filter = Filter{} | ||
filter.And("name").Eq("Elmer") | ||
filter.And("age").Gt(10) | ||
filter.And("height").Lt(44) | ||
filter.And("weight").LtEq(150) | ||
filter.And("hair").NotEq("blond") | ||
p = filter.String() | ||
g.Expect("name='Elmer',age>10,height<44,weight<=150,hair!='blond'").To(gomega.Equal(p)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters