Skip to content

Commit

Permalink
feat(filter):Add ILIKE filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoann committed Feb 1, 2021
1 parent 721e9e1 commit 0f3a526
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 0 deletions.
16 changes: 16 additions & 0 deletions filterapply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,19 @@ func TestFilterApply_Like(t *testing.T) {
assert.Nil(t, err)
assert.Len(t, models, 2)
}

func TestFilterApply_ILike(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()
assert.Nil(t, err)
dbp, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.Nil(t, err)
category := &testdata.TwoI{Name: "category"}
saveModel(t, dbp, category)
category2 := &testdata.TwoI{Name: "categoryY"}
saveModel(t, dbp, category2)

models, err := yaorm.GenericSelectAll(dbp, testdata.NewTwoIFilter().Name(yaormfilter.ILike("categor%")))
assert.Nil(t, err)
assert.Len(t, models, 2)
}
5 changes: 5 additions & 0 deletions yaormfilter/boolfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func (f *BoolFilter) Like(v interface{}) ValueFilter {
return f
}

// ILike is not applicable on bool
func (f *BoolFilter) ILike(v interface{}) ValueFilter {
return f
}

// Nil adds a nil filter
func (f *BoolFilter) Nil(v bool) ValueFilter {
f.nil(v)
Expand Down
5 changes: 5 additions & 0 deletions yaormfilter/boolfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func TestBoolFilter_Like(t *testing.T) {
assert.Equal(t, filter, filter.Like(true))
}

func TestBoolFilter_ILike(t *testing.T) {
filter := yaormfilter.NewBoolFilter()
assert.Equal(t, filter, filter.ILike(true))
}

func TestBoolFilter_Nil(t *testing.T) {
filter := yaormfilter.NewBoolFilter()
assert.Equal(t, filter, filter.Nil(true))
Expand Down
5 changes: 5 additions & 0 deletions yaormfilter/datefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func (f *DateFilter) Like(v interface{}) ValueFilter {
return f
}

// ILike is not applicable on Date
func (f *DateFilter) ILike(v interface{}) ValueFilter {
return f
}

// Nil applies a nil filter on Date
func (f *DateFilter) Nil(v bool) ValueFilter {
f.nil(v)
Expand Down
5 changes: 5 additions & 0 deletions yaormfilter/datefilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func TestDateFilter_Like(t *testing.T) {
assert.Equal(t, filter, filter.Like(time.Now()))
}

func TestDateFilter_ILike(t *testing.T) {
filter := yaormfilter.NewDateFilter()
assert.Equal(t, filter, filter.ILike(time.Now()))
}

func TestDateFilter_Nil(t *testing.T) {
filter := yaormfilter.NewDateFilter()
assert.Equal(t, filter, filter.Nil(true))
Expand Down
5 changes: 5 additions & 0 deletions yaormfilter/int64filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func (f *Int64Filter) Like(v interface{}) ValueFilter {
return f
}

// ILike is not applicable on int64
func (f *Int64Filter) ILike(v interface{}) ValueFilter {
return f
}

// Nil adds a nil filter
func (f *Int64Filter) Nil(v bool) ValueFilter {
f.nil(v)
Expand Down
5 changes: 5 additions & 0 deletions yaormfilter/int64filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func TestInt64Filter_Like(t *testing.T) {
assert.Equal(t, filter, filter.Like(int64(12)))
}

func TestInt64Filter_ILike(t *testing.T) {
filter := yaormfilter.NewInt64Filter()
assert.Equal(t, filter, filter.ILike(int64(12)))
}

func TestInt64Filter_Nil(t *testing.T) {
filter := yaormfilter.NewInt64Filter()
assert.Equal(t, filter, filter.Nil(true))
Expand Down
5 changes: 5 additions & 0 deletions yaormfilter/nilfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func (f *NilFilter) Like(v interface{}) ValueFilter {
return f
}

// ILike is not applicable on Date
func (f *NilFilter) ILike(v interface{}) ValueFilter {
return f
}

// Nil applies a nil filter on Date
func (f *NilFilter) Nil(v bool) ValueFilter {
f.nil(v)
Expand Down
11 changes: 11 additions & 0 deletions yaormfilter/stringfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ func (f *StringFilter) Like(v interface{}) ValueFilter {
return f
}

// ILike adds a Like filter
func (f *StringFilter) ILike(v interface{}) ValueFilter {
underlyingValue := tools.GetNonPtrValue(v)
// make sure we have a string
if underlyingValue.Kind() != reflect.String {
panic("Value in StringFilter is not a string")
}
f.ilike(underlyingValue.Interface())
return f
}

// Nil adds a nil filter
func (f *StringFilter) Nil(v bool) ValueFilter {
f.nil(v)
Expand Down
6 changes: 6 additions & 0 deletions yaormfilter/stringfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func TestStringFilter_Like(t *testing.T) {
assert.Equal(t, filter, filter.Like(str))
}

func TestStringFilter_ILike(t *testing.T) {
filter := yaormfilter.NewStringFilter()
str := "bla"
assert.Equal(t, filter, filter.ILike(str))
}

func TestStringFilter_Nil(t *testing.T) {
filter := yaormfilter.NewStringFilter()
assert.Equal(t, filter, filter.Nil(true))
Expand Down
10 changes: 10 additions & 0 deletions yaormfilter/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ func Like(v interface{}) ValueFilter {
panic(fmt.Errorf("Unknown type: %+v for value %+v in Like filter", underlyingValue.Kind(), v))
}

// ILike returns the correct filter according to the value sent
func ILike(v interface{}) ValueFilter {
underlyingValue := tools.GetNonPtrValue(v)
switch underlyingValue.Kind() {
case reflect.String:
return NewStringFilter().ILike(v)
}
panic(fmt.Errorf("Unknown type: %+v for value %+v in ILike filter", underlyingValue.Kind(), v))
}

// Lt returns the correct filter according the value sent
func Lt(v interface{}) ValueFilter {
underlyingValue := tools.GetNonPtrValue(v)
Expand Down
4 changes: 4 additions & 0 deletions yaormfilter/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func TestLike(t *testing.T) {
assert.IsType(t, &yaormfilter.StringFilter{}, yaormfilter.Like("abcdef%"))
}

func TestILike(t *testing.T) {
assert.IsType(t, &yaormfilter.StringFilter{}, yaormfilter.ILike("abcdef%"))
}

func TestLt(t *testing.T) {
assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.Lt(int64(12)))
assert.IsType(t, &yaormfilter.DateFilter{}, yaormfilter.Lt(time.Now()))
Expand Down
7 changes: 7 additions & 0 deletions yaormfilter/valuefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ValueFilter interface {
Equals(v interface{}) ValueFilter
NotEquals(v interface{}) ValueFilter
Like(v interface{}) ValueFilter
ILike(v interface{}) ValueFilter
Lt(v interface{}) ValueFilter
Lte(v interface{}) ValueFilter
Gt(v interface{}) ValueFilter
Expand Down Expand Up @@ -69,6 +70,12 @@ func (f *valuefilterimpl) like(e interface{}) *valuefilterimpl {
})
}

func (f *valuefilterimpl) ilike(e interface{}) *valuefilterimpl {
return f.raw(func(field string) interface{} {
return squirrel.Expr(fmt.Sprintf("%s ILIKE ?", field), e)
})
}

func (f *valuefilterimpl) in(e []interface{}) *valuefilterimpl {
return f.raw(func(field string) interface{} {
return squirrel.Eq{field: e}
Expand Down

0 comments on commit 0f3a526

Please sign in to comment.