Skip to content

Commit

Permalink
Add gormlike:false to options
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarten van der Heijden committed Aug 24, 2023
1 parent ecf25c2 commit d5bbd83
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ you have 2 options:
- `TaggedOnly()`: Will only change queries on fields that have the `gormlike:"true"` tag
- `SettingOnly()`: Will only change queries on `*gorm.DB` objects that have `.Set("gormlike", true)` set.

If you want a particular query to not be like-able, use `.Set("gormlike", false)`. This works
If you want a particular query or field to not be like-able, use `.Set("gormlike", false)` or `gormlike:"false"` respectively. These work
regardless of configuration.

## 💡 Related Libraries
Expand Down
52 changes: 30 additions & 22 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,26 @@ func (d *gormLike) queryCallback(db *gorm.DB) {
for index, cond := range exp.Exprs {
switch cond := cond.(type) {
case clause.Eq:
if d.conditionalTag {
columnName, ok := cond.Column.(string)
if !ok {
continue
}
columnName, columnOk := cond.Column.(string)
if !columnOk {
continue
}

value := db.Statement.Schema.FieldsByDBName[columnName].Tag.Get(tagName)
// Get the `gormlike` value
tagValue := db.Statement.Schema.FieldsByDBName[columnName].Tag.Get(tagName)

// Ignore if there's no valid tag settingValue
if value != "true" {
continue
}
// If the user has explicitly set this to false, ignore this field
if tagValue == "false" {
continue
}

// If tags are required and the tag is not true, ignore this field
if d.conditionalTag && tagValue != "true" {
continue
}

value, ok := cond.Value.(string)
if !ok {
value, columnOk := cond.Value.(string)
if !columnOk {
continue
}

Expand All @@ -62,18 +66,22 @@ func (d *gormLike) queryCallback(db *gorm.DB) {

exp.Exprs[index] = db.Session(&gorm.Session{NewDB: true}).Where(condition, value).Statement.Clauses["WHERE"].Expression
case clause.IN:
if d.conditionalTag {
columnName, ok := cond.Column.(string)
if !ok {
continue
}
columnName, columnOk := cond.Column.(string)
if !columnOk {
continue
}

value := db.Statement.Schema.FieldsByDBName[columnName].Tag.Get(tagName)
// Get the `gormlike` value
tagValue := db.Statement.Schema.FieldsByDBName[columnName].Tag.Get(tagName)

// Ignore if there's no valid tag settingValue
if value != "true" {
continue
}
// If the user has explicitly set this to false, ignore this field
if tagValue == "false" {
continue
}

// If tags are required and the tag is not true, ignore this field
if d.conditionalTag && tagValue != "true" {
continue
}

var likeCounter int
Expand Down
65 changes: 65 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,71 @@ func TestGormLike_Initialize_TriggersLikingCorrectlyWithConditionalTag(t *testin
}
}

func TestGormLike_Initialize_AlwaysIgnoresFieldsWithGormLikeFalse(t *testing.T) {
t.Parallel()

type ObjectB struct {
Name string
Other string `gormlike:"false"`
}

tests := map[string]struct {
filter map[string]any
existing []ObjectB
expected []ObjectB
}{
"Normal filter works on never field": {
filter: map[string]any{
"other": "abc",
},
existing: []ObjectB{{Name: "jessica", Other: "abc"}, {Name: "jessica", Other: "abc"}},
expected: []ObjectB{{Name: "jessica", Other: "abc"}, {Name: "jessica", Other: "abc"}},
},
"simple filter on disallowed fields": {
filter: map[string]any{
"other": "%b%",
},
existing: []ObjectB{{Name: "jessica", Other: "abc"}, {Name: "jessica", Other: "abc"}},
expected: []ObjectB{},
},
"multi-filter on disallowed fields": {
filter: map[string]any{
"other": []string{"%b%", "%c%"},
},
existing: []ObjectB{{Name: "jessica", Other: "abc"}, {Name: "jessica", Other: "abc"}},
expected: []ObjectB{},
},
}

for name, testData := range tests {
testData := testData
t.Run(name, func(t *testing.T) {
t.Parallel()
// Arrange
db := gormtestutil.NewMemoryDatabase(t, gormtestutil.WithName(t.Name()))
_ = db.AutoMigrate(&ObjectB{})
plugin := New(TaggedOnly())

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

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

// Assert
assert.NoError(t, err)

var actual []ObjectB
err = db.Where(testData.filter).Find(&actual).Error
assert.NoError(t, err)

assert.Equal(t, testData.expected, actual)
})
}
}

func TestGormLike_Initialize_TriggersLikingCorrectlyWithSetting(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit d5bbd83

Please sign in to comment.