Skip to content

Commit

Permalink
Add NotEquals ValueFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-vasseur authored and geoffreybauduin committed Feb 28, 2019
1 parent cfed731 commit c18bb90
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 15 deletions.
11 changes: 11 additions & 0 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ func TestGenericCount(t *testing.T) {
modelFound, err := yaorm.GenericCount(dbp, testdata.NewCategoryFilter().Name(yaormfilter.Equals("category")))
assert.Nil(t, err)
assert.Equal(t, uint64(1), modelFound)
modelFound, err = yaorm.GenericCount(dbp, testdata.NewCategoryFilter().Name(yaormfilter.NotEquals("category")))
assert.Nil(t, err)
assert.Equal(t, uint64(0), modelFound)

m = &testdata.Category{Name: "other"}
m.SetDBP(dbp)
err = yaorm.GenericSave(m)
assert.Nil(t, err)
modelFound, err = yaorm.GenericCount(dbp, testdata.NewCategoryFilter().Name(yaormfilter.NotEquals("category")))
assert.Nil(t, err)
assert.Equal(t, uint64(1), modelFound) // catch "other"
}

func TestGenericCount_WithJoinFilters(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions yaormfilter/boolfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (f *BoolFilter) Equals(v interface{}) ValueFilter {
return f
}

// NotEquals adds an notEqual filter
func (f *BoolFilter) NotEquals(v interface{}) ValueFilter {
f.notEquals(f.getValue(v))
return f
}

// Like is not applicable on bool
func (f *BoolFilter) Like(v interface{}) ValueFilter {
return f
Expand Down
8 changes: 8 additions & 0 deletions yaormfilter/boolfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func TestBoolFilter_Equals(t *testing.T) {
assert.Panics(t, func() { filter.Equals("true") })
}

func TestBoolFilter_NotEquals(t *testing.T) {
filter := yaormfilter.NewBoolFilter()
v := true
assert.Equal(t, filter, filter.NotEquals(v))
assert.Equal(t, filter, filter.NotEquals(&v))
assert.Panics(t, func() { filter.NotEquals("true") })
}

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

// NotEquals applies an notEqual filter on Date
func (f *DateFilter) NotEquals(v interface{}) ValueFilter {
f.notEquals(f.getValue(v))
return f
}

// Like is not applicable on Date
func (f *DateFilter) Like(v interface{}) ValueFilter {
return f
Expand Down
8 changes: 8 additions & 0 deletions yaormfilter/datefilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ func TestDateFilter_Equals(t *testing.T) {
assert.Panics(t, func() { filter.Equals(0) })
}

func TestDateFilter_NotEquals(t *testing.T) {
filter := yaormfilter.NewDateFilter()
now := time.Now()
assert.Equal(t, filter, filter.NotEquals(now))
assert.Equal(t, filter, filter.NotEquals(&now))
assert.Panics(t, func() { filter.NotEquals(0) })
}

func TestDateFilter_Like(t *testing.T) {
filter := yaormfilter.NewDateFilter()
assert.Equal(t, filter, filter.Like(time.Now()))
Expand Down
6 changes: 6 additions & 0 deletions yaormfilter/int64filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (f *Int64Filter) Equals(v interface{}) ValueFilter {
return f
}

// NotEquals adds an notEqual filter
func (f *Int64Filter) NotEquals(v interface{}) ValueFilter {
f.notEquals(f.getValue(v))
return f
}

// Like is not applicable on int64
func (f *Int64Filter) Like(v interface{}) ValueFilter {
return f
Expand Down
8 changes: 8 additions & 0 deletions yaormfilter/int64filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func TestInt64Filter_Equals(t *testing.T) {
assert.Panics(t, func() { filter.Equals("aazeae") })
}

func TestInt64Filter_NotEquals(t *testing.T) {
filter := yaormfilter.NewInt64Filter()
v := int64(12)
assert.Equal(t, filter, filter.NotEquals(v))
assert.Equal(t, filter, filter.NotEquals(&v))
assert.Panics(t, func() { filter.NotEquals("aazeae") })
}

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

// NotEquals applies an notEqual filter
func (f *NilFilter) NotEquals(v interface{}) ValueFilter {
return f
}

// Like is not applicable on Date
func (f *NilFilter) Like(v interface{}) ValueFilter {
return f
Expand Down
11 changes: 11 additions & 0 deletions yaormfilter/stringfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func (f *StringFilter) Equals(v interface{}) ValueFilter {
return f
}

// NotEquals adds an notEqual filter
func (f *StringFilter) NotEquals(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.notEquals(underlyingValue.Interface())
return f
}

// Like adds a Like filter
func (f *StringFilter) Like(v interface{}) ValueFilter {
underlyingValue := tools.GetNonPtrValue(v)
Expand Down
8 changes: 8 additions & 0 deletions yaormfilter/stringfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func TestStringFilter_Equals(t *testing.T) {
assert.Panics(t, func() { filter.Equals(0) })
}

func TestStringFilter_NotEquals(t *testing.T) {
filter := yaormfilter.NewStringFilter()
str := "bla"
assert.Equal(t, filter, filter.NotEquals(str))
assert.Equal(t, filter, filter.NotEquals(&str))
assert.Panics(t, func() { filter.NotEquals(0) })
}

func TestStringFilter_Like(t *testing.T) {
filter := yaormfilter.NewStringFilter()
str := "bla"
Expand Down
21 changes: 21 additions & 0 deletions yaormfilter/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ func Equals(v interface{}) ValueFilter {
panic(fmt.Errorf("Unknown type: %+v for value %+v in Equals filter", underlyingValue.Kind(), v))
}

// NotEquals returns the correct filter according the value sent
func NotEquals(v interface{}) ValueFilter {
underlyingValue := tools.GetNonPtrValue(v)
switch underlyingValue.Kind() {
case reflect.Int64:
return NewInt64Filter().NotEquals(v)
case reflect.String:
return NewStringFilter().NotEquals(v)
case reflect.Struct:
if _, ok := underlyingValue.Interface().(time.Time); ok {
return NewDateFilter().NotEquals(v)
}
case reflect.Bool:
return NewBoolFilter().NotEquals(v)
}
if v == nil {
return NewNilFilter().Nil(true)
}
panic(fmt.Errorf("Unknown type: %+v for value %+v in NotEquals filter", underlyingValue.Kind(), v))
}

// In returns the correct filter according to the value sent
func In(values ...interface{}) ValueFilter {
var t reflect.Type
Expand Down
8 changes: 8 additions & 0 deletions yaormfilter/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ func TestEquals(t *testing.T) {
assert.IsType(t, &yaormfilter.BoolFilter{}, yaormfilter.Equals(false))
}

func TestNotEquals(t *testing.T) {
assert.IsType(t, &yaormfilter.StringFilter{}, yaormfilter.NotEquals("abcdef"))
assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.NotEquals(int64(12)))
assert.IsType(t, &yaormfilter.NilFilter{}, yaormfilter.NotEquals(nil))
assert.IsType(t, &yaormfilter.DateFilter{}, yaormfilter.NotEquals(time.Now()))
assert.IsType(t, &yaormfilter.BoolFilter{}, yaormfilter.NotEquals(false))
}

func TestIn(t *testing.T) {
assert.IsType(t, &yaormfilter.StringFilter{}, yaormfilter.In("abcdef", "bcderzzer"))
assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.In(int64(12), int64(15)))
Expand Down
52 changes: 37 additions & 15 deletions yaormfilter/valuefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type ValueFilter interface {
Apply(statement squirrel.SelectBuilder, tableName, fieldName string) squirrel.SelectBuilder
Equals(v interface{}) ValueFilter
NotEquals(v interface{}) ValueFilter
Like(v interface{}) ValueFilter
Lt(v interface{}) ValueFilter
Lte(v interface{}) ValueFilter
Expand All @@ -21,31 +22,41 @@ type ValueFilter interface {
}

type valuefilterimpl struct {
equals_ interface{}
nil_ *bool
shouldEqual bool
like_ interface{}
shouldLike bool
in_ []interface{}
shouldIn bool
lt_ interface{}
shouldLt bool
lte_ interface{}
shouldLte bool
gt_ interface{}
shouldGt bool
gte_ interface{}
shouldGte bool
equals_ interface{}
nil_ *bool
shouldEqual bool
like_ interface{}
shouldLike bool
in_ []interface{}
shouldIn bool
lt_ interface{}
shouldLt bool
lte_ interface{}
shouldLte bool
gt_ interface{}
shouldGt bool
gte_ interface{}
shouldGte bool
notEquals_ interface{}
shouldNotEqual bool
}

func (f valuefilterimpl) IsEquality() bool {
return f.shouldEqual
}

func (f valuefilterimpl) IsInequality() bool {
return f.shouldNotEqual
}

func (f valuefilterimpl) GetEquality() interface{} {
return f.equals_
}

func (f valuefilterimpl) GetInequality() interface{} {
return f.notEquals_
}

func (f *valuefilterimpl) nil(v bool) *valuefilterimpl {
f.nil_ = &v
return f
Expand All @@ -57,6 +68,12 @@ func (f *valuefilterimpl) equals(e interface{}) *valuefilterimpl {
return f
}

func (f *valuefilterimpl) notEquals(e interface{}) *valuefilterimpl {
f.notEquals_ = e
f.shouldNotEqual = true
return f
}

func (f *valuefilterimpl) like(e interface{}) *valuefilterimpl {
f.like_ = e
f.shouldLike = true
Expand Down Expand Up @@ -111,6 +128,11 @@ func (f *valuefilterimpl) Apply(statement squirrel.SelectBuilder, tableName, fie
squirrel.Eq{computedField: f.GetEquality()},
)
}
if f.IsInequality() {
statement = statement.Where(
squirrel.NotEq{computedField: f.GetInequality()},
)
}
if f.shouldLike {
statement = statement.Where(
fmt.Sprintf("%s LIKE ?", computedField), f.like_,
Expand Down

0 comments on commit c18bb90

Please sign in to comment.