-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue#24 - Support for bool filter (#25)
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
1 parent
c50e9fe
commit 656fd35
Showing
5 changed files
with
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,4 +51,4 @@ func IsNil(v reflect.Value) bool { | |
return v.IsNil() | ||
} | ||
return false | ||
} | ||
} |
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,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 | ||
} |
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,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)) | ||
} |
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