Skip to content

Commit

Permalink
Issue#24 - Support for bool filter (#25)
Browse files Browse the repository at this point in the history
Added boolfitler and update tools.go
Added testcases
Fixed formatting issues in tools/reflect.go
Fixed grammar ( an for vowels, a for consonants ;) )
Fixed import grouping
Dummy push for build trigger
  • Loading branch information
Shailesh Pant authored and geoffreybauduin committed Apr 24, 2018
1 parent c50e9fe commit 656fd35
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tools/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ func IsNil(v reflect.Value) bool {
return v.IsNil()
}
return false
}
}
73 changes: 73 additions & 0 deletions yaormfilter/boolfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package yaormfilter

import (
"reflect"

"github.com/geoffreybauduin/yaorm/tools"
)

// BoolFilter is the filter used to filter on bool fields. Implements ValueFilter
type BoolFilter struct {
valuefilterimpl
}

// NewBoolFilter returns a new bool filter
func NewBoolFilter() ValueFilter {
return &BoolFilter{}
}

func (f *BoolFilter) getValue(v interface{}) interface{} {
underlyingValue := tools.GetNonPtrValue(v)
// make sure we have a bool
if underlyingValue.Kind() != reflect.Bool {
panic("Value in BoolFilter is not a bool")
}
return underlyingValue.Interface()
}

// Equals adds an equal filter
func (f *BoolFilter) Equals(v interface{}) ValueFilter {
f.equals(f.getValue(v))
return f
}

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

// Nil adds a nil filter
func (f *BoolFilter) Nil(v bool) ValueFilter {
f.nil(v)
return f
}

// In adds a IN filter
func (f *BoolFilter) In(values ...interface{}) ValueFilter {
interfaceValues := []interface{}{}
for _, v := range values {
interfaceValues = append(interfaceValues, f.getValue(v))
}
f.in(interfaceValues)
return f
}

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

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

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

// Gte is not applicable on bool
func (f *BoolFilter) Gte(v interface{}) ValueFilter {
return f
}
36 changes: 36 additions & 0 deletions yaormfilter/boolfilter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package yaormfilter_test

import (
"testing"

"github.com/geoffreybauduin/yaorm/yaormfilter"
"github.com/stretchr/testify/assert"
)

func TestNewBoolFilter(t *testing.T) {
filter := yaormfilter.NewBoolFilter()
assert.IsType(t, &yaormfilter.BoolFilter{}, filter)
}

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

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

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

func TestBoolFilter_In(t *testing.T) {
filter := yaormfilter.NewBoolFilter()
assert.Equal(t, filter, filter.In(true, false))
}
4 changes: 4 additions & 0 deletions yaormfilter/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func Equals(v interface{}) ValueFilter {
if _, ok := underlyingValue.Interface().(time.Time); ok {
return NewDateFilter().Equals(v)
}
case reflect.Bool:
return NewBoolFilter().Equals(v)
}
if v == nil {
return NewNilFilter().Nil(true)
Expand Down Expand Up @@ -48,6 +50,8 @@ func In(values ...interface{}) ValueFilter {
return NewInt64Filter().In(values...)
case reflect.String:
return NewStringFilter().In(values...)
case reflect.Bool:
return NewBoolFilter().In(values...)
case reflect.Slice:
// if we receive a slice, we want to go through all the slices received an concat them inside one
data := []interface{}{}
Expand Down
2 changes: 2 additions & 0 deletions yaormfilter/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ func TestEquals(t *testing.T) {
assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.Equals(int64(12)))
assert.IsType(t, &yaormfilter.NilFilter{}, yaormfilter.Equals(nil))
assert.IsType(t, &yaormfilter.DateFilter{}, yaormfilter.Equals(time.Now()))
assert.IsType(t, &yaormfilter.BoolFilter{}, yaormfilter.Equals(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)))
assert.IsType(t, &yaormfilter.StringFilter{}, yaormfilter.In([]string{"abcdef", "bcderzzer"}))
assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.In([]int64{int64(12), int64(15)}))
assert.IsType(t, &yaormfilter.BoolFilter{}, yaormfilter.In([]bool{true, false}))
}

func TestLike(t *testing.T) {
Expand Down

0 comments on commit 656fd35

Please sign in to comment.