Skip to content

Commit

Permalink
fix(filter): Fixed for filtering booleans (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamess-Lucass committed May 22, 2024
1 parent d06930b commit f1c1000
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ func Apply(db *gorm.DB, query Query, maxTop *int, searchFunc func(db *gorm.DB, s
operand := opts[1]
value := opts[2]

field, ok := modelType.FieldByNameFunc(func(s string) bool {
return strings.EqualFold(s, property)
})

property = GetGormColumnNameByJsonTag(namer, tableName, modelType, property)

if strings.EqualFold(operand, "contains") {
if ok && field.Type.Kind() == reflect.Bool {
where.WriteString(fmt.Sprintf("%s %s %s", property, filterOperations[operand], value))
} else if strings.EqualFold(operand, "contains") {
valueWithoutQuotes := getValueBetweenQuotes(value)
where.WriteString(fmt.Sprintf("%s %s '%%%s%%'", property, filterOperations[operand], valueWithoutQuotes))
} else {
Expand Down
4 changes: 2 additions & 2 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,15 +461,15 @@ func Test_QueryWithFilterPropertyDoesntMatchJsonProperty(t *testing.T) {
}

func Test_QueryWithFilterAndOrInColumnName(t *testing.T) {
query := Query{Filter: "contributor eq 'true'"}
query := Query{Filter: "contributor eq true"}

sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
res, _, _ := Apply(tx.Model(&User{}), query, nil, nil, &[]User{})
return res.Find(&[]User{})
})

expectedSql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Model(&User{}).Where("LOWER(contributor) = LOWER('true')").Find(&[]User{})
return tx.Model(&User{}).Where("contributor = true").Find(&[]User{})
})

assert.Equal(t, expectedSql, sql)
Expand Down

0 comments on commit f1c1000

Please sign in to comment.