Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter replacement for OR conditions #6

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: go test -json > TestResults-${{ matrix.go-version }}.json

- name: Upload Go test results for ${{ matrix.go-version }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Go-results-${{ matrix.go-version }}
path: TestResults-${{ matrix.go-version }}.json
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/stretchr/testify v1.8.1
github.com/survivorbat/go-tsyncmap v0.0.0
gorm.io/driver/sqlite v1.5.2
gorm.io/gorm v1.25.6
gorm.io/gorm v1.25.12
)

require (
Expand All @@ -18,5 +18,6 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/text v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/survivorbat/go-tsyncmap v0.0.0 h1:XTc1+uXyuw//1Hhpg4IxW6tEe3Tvd2d5vM/6IPqmkeg=
github.com/survivorbat/go-tsyncmap v0.0.0/go.mod h1:zKe2CuXEo+c1d9DVT5L7AG2jPTdWi7QQN/Gk+26Vecg=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.2 h1:TpQ+/dqCY4uCigCFyrfnrJnrW9zjpelWVoEVNy5qJkc=
gorm.io/driver/sqlite v1.5.2/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/gorm v1.25.6 h1:V92+vVda1wEISSOMtodHVRcUIOPYa2tgQtyF+DfFx+A=
gorm.io/gorm v1.25.6/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
3 changes: 2 additions & 1 deletion plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func createDeepFilterRecursively(exprs []clause.Expression, db *gorm.DB) {
switch cond := cond.(type) {
case clause.AndConditions:
createDeepFilterRecursively(exprs[index].(clause.AndConditions).Exprs, db)

case clause.OrConditions:
createDeepFilterRecursively(exprs[index].(clause.OrConditions).Exprs, db)
case clause.Eq:
switch value := cond.Value.(type) {
case map[string]any:
Expand Down
94 changes: 93 additions & 1 deletion plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package deepgorm

import (
"testing"

"github.com/google/uuid"
"github.com/ing-bank/gormtestutil"
"github.com/stretchr/testify/assert"
"gorm.io/gorm/clause"
"testing"
)

func TestDeepGorm_Name_ReturnsExpectedName(t *testing.T) {
Expand Down Expand Up @@ -227,3 +228,94 @@ func TestDeepGorm_Initialize_TriggersFilteringCorrectly(t *testing.T) {
})
}
}

func TestDeepGorm_Initialize_TriggersFilteringCorrectlyWithOrQuery(t *testing.T) {
t.Parallel()
filter1 := map[string]any{
"object_bs": map[string]any{
"name": "def",
},
}
filter2 := map[string]any{
"object_bs": map[string]any{
"name": "abc",
},
}
existing := []ObjectA{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
Name: "ghi",
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
Name: "def",
},
},
},
{
ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
Name: "nope",
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("83aaf47d-a167-4a49-8b7c-3516ced56e8a"),
Name: "abc",
},
},
},
{
ID: uuid.MustParse("383e9a9b-ef95-421d-a89e-60f0344ee29d"),
Name: "Maybe",
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("3b35e207-c544-424e-b029-be31d5fe8bad"),
Name: "cba",
},
},
},
}
expected := []ObjectA{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
Name: "ghi",
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
Name: "def",
ObjectAID: uuid.MustParse("59aa5a8f-c5de-44fa-9355-080650481688"),
},
},
},
{
ID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
Name: "nope",
ObjectBs: []ObjectB{
{
ID: uuid.MustParse("83aaf47d-a167-4a49-8b7c-3516ced56e8a"),
Name: "abc",
ObjectAID: uuid.MustParse("3415d786-bc03-4543-aa3c-5ec9e55aa460"),
},
},
},
}

db := gormtestutil.NewMemoryDatabase(t, gormtestutil.WithName(t.Name()))
_ = db.AutoMigrate(&ObjectA{}, &ObjectB{})
plugin := New()

if err := db.CreateInBatches(existing, 10).Error; err != nil {
t.Error(err)
t.FailNow()
}

// Act
err := db.Use(plugin)

// Assert
assert.Nil(t, err)

var actual []ObjectA
err = db.Where(filter1).Or(filter2).Preload(clause.Associations).Find(&actual).Error
assert.Nil(t, err)

assert.Equal(t, expected, actual)
}