Compare commits
9 Commits
3dc8d0d79f
...
filter-joi
| Author | SHA1 | Date | |
|---|---|---|---|
| 86f4550b08 | |||
| f50aaca44d | |||
| f7cf63c2bf | |||
| 30714bb3da | |||
| 8a81173fc8 | |||
| 43824af97c | |||
| 326866dc49 | |||
| 450345ba6b | |||
| bdc978aec1 |
91
app/main.go
91
app/main.go
@ -20,15 +20,31 @@ var (
|
||||
)
|
||||
|
||||
type CertFilter struct {
|
||||
Alive *bool `filterfield:"field=alive,operator=EQ"`
|
||||
SerialNumber *string `filterfield:"field=serial_number,operator=NE"`
|
||||
SerialNumberContains *string `filterfield:"field=serial_number,operator=LIKE"`
|
||||
IssuerContains *string `filterfield:"field=issuer,operator=ILIKE"`
|
||||
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
||||
Ids *[]uuid.UUID `filterfield:"field=id,operator=IN"`
|
||||
IdsNot *[]string `filterfield:"field=id,operator=NOT_IN"`
|
||||
CreatedAt_Lt *time.Time `filterfield:"field=created_at,operator=LT"`
|
||||
Timestamps *[]time.Time `filterfield:"field=created_at,operator=IN"`
|
||||
Alive *bool `filterfield:"field=alive;operator=EQ"`
|
||||
SerialNumber *string `filterfield:"field=serial_number;operator=NE"`
|
||||
SerialNumberContains *string `filterfield:"field=serial_number;operator=LIKE"`
|
||||
IssuerContains *string `filterfield:"field=issuer;operator=ILIKE"`
|
||||
Id *uuid.UUID `filterfield:"field=id;operator=EQ"`
|
||||
Ids *[]uuid.UUID `filterfield:"field=id;operator=IN"`
|
||||
IdsNot *[]string `filterfield:"field=id;operator=NOT_IN"`
|
||||
CreatedAt_Lt *time.Time `filterfield:"field=created_at;operator=LT"`
|
||||
Timestamps *[]time.Time `filterfield:"field=created_at;operator=IN"`
|
||||
CompanyIsActive *bool
|
||||
}
|
||||
|
||||
func (f CertFilter) ApplyQuery(query *gorm.DB) *gorm.DB {
|
||||
if f.CompanyIsActive != nil {
|
||||
query = query.Joins(
|
||||
fmt.Sprintf(
|
||||
"JOIN companies ON certificates.company_id = companies.id WHERE companies.is_active = %t",
|
||||
*f.CompanyIsActive,
|
||||
))
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
type CompanyFilter struct {
|
||||
IsActive *bool `filterfield:"field=is_active;operator=EQ"`
|
||||
}
|
||||
|
||||
func doMagic(db *gorm.DB) {
|
||||
@ -89,6 +105,24 @@ func doList(db *gorm.DB) {
|
||||
}
|
||||
}
|
||||
|
||||
func doListWithJoins(db *gorm.DB) {
|
||||
repo := repository.RepoBase[models.Cert]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
filter := CertFilter{
|
||||
CompanyIsActive: &FALSE,
|
||||
}
|
||||
|
||||
certs, err := repo.List(filter, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for n, cert := range *certs {
|
||||
fmt.Printf(">> [%d] %+v %s (alive %t)\n", n, cert.Id, cert.CreatedAt, cert.Alive)
|
||||
}
|
||||
}
|
||||
|
||||
func doCount(db *gorm.DB) {
|
||||
repo := repository.RepoBase[models.Cert]{}
|
||||
repo.Init(db, nil)
|
||||
@ -121,6 +155,40 @@ func doGet(db *gorm.DB) {
|
||||
fmt.Printf(">> %+v %s (alive %t)\n", cert.Id, cert.CreatedAt, cert.Alive)
|
||||
}
|
||||
|
||||
func doSave(db *gorm.DB) {
|
||||
repo := repository.RepoBase[models.Company]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
company := models.Company{
|
||||
Name: "Test company",
|
||||
Address: "Some address",
|
||||
City: "Some city",
|
||||
Email: "email@example.org",
|
||||
Oib: "123456",
|
||||
}
|
||||
|
||||
_, err := repo.Save(&company)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("New company id: %s\n", company.Id.String())
|
||||
}
|
||||
|
||||
func doDelete(db *gorm.DB) {
|
||||
repo := repository.RepoBase[models.Company]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
filter := CompanyFilter{
|
||||
IsActive: &FALSE,
|
||||
}
|
||||
|
||||
deletedCount, err := repo.Delete(filter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf(">> DELETED: %d\n", deletedCount)
|
||||
}
|
||||
|
||||
func doExists(db *gorm.DB) {
|
||||
repo := repository.RepoBase[models.Cert]{}
|
||||
repo.Init(db, nil)
|
||||
@ -147,7 +215,10 @@ func main() {
|
||||
|
||||
// doMagic(db)
|
||||
// doList(db)
|
||||
doCount(db)
|
||||
doListWithJoins(db)
|
||||
// doCount(db)
|
||||
// doSave(db)
|
||||
// doDelete(db)
|
||||
// doGet(db)
|
||||
// doExists(db)
|
||||
// inheritance.DoInheritanceTest()
|
||||
|
||||
@ -21,7 +21,7 @@ type Company struct {
|
||||
UpdatedAt time.Time `faker:"-"`
|
||||
}
|
||||
|
||||
func (m *Company) TableName() string {
|
||||
func (m Company) TableName() string {
|
||||
return "companies"
|
||||
}
|
||||
|
||||
|
||||
31
app/repository/method_delete.go
Normal file
31
app/repository/method_delete.go
Normal file
@ -0,0 +1,31 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"repo-pattern/app/repository/smartfilter"
|
||||
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type DeleteMethod[T schema.Tabler] struct {
|
||||
repo *RepoBase[T]
|
||||
}
|
||||
|
||||
func (m *DeleteMethod[T]) Init(repo *RepoBase[T]) {
|
||||
m.repo = repo
|
||||
}
|
||||
|
||||
func (m DeleteMethod[T]) Delete(filter interface{}) (int64, error) {
|
||||
var (
|
||||
model T
|
||||
)
|
||||
|
||||
query, err := smartfilter.ToQuery(model, filter, m.repo.dbConn)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
result := query.Delete(&model)
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
return result.RowsAffected, nil
|
||||
}
|
||||
73
app/repository/method_delete_test.go
Normal file
73
app/repository/method_delete_test.go
Normal file
@ -0,0 +1,73 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDeleteMethod(t *testing.T) {
|
||||
t.Run("With filter", func(t *testing.T) {
|
||||
sqldb, db, mock := NewMockDB()
|
||||
defer sqldb.Close()
|
||||
|
||||
repo := RepoBase[MyModel]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
id := uuid.New()
|
||||
filter := MyModelFilter{
|
||||
Id: &id,
|
||||
}
|
||||
|
||||
sql := "DELETE FROM my_models WHERE my_models.id = $1"
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||
WithArgs(id).
|
||||
WillReturnResult(sqlmock.NewResult(1, 111))
|
||||
mock.ExpectCommit()
|
||||
|
||||
deleted, err := repo.Delete(filter)
|
||||
assert.Equal(t, int64(111), deleted)
|
||||
assert.Nil(t, err)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("With multiple filters", func(t *testing.T) {
|
||||
sqldb, db, mock := NewMockDB()
|
||||
defer sqldb.Close()
|
||||
|
||||
repo := RepoBase[MyModel]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
id1 := uuid.New()
|
||||
id2 := uuid.New()
|
||||
id3 := uuid.New()
|
||||
cnt := 3456
|
||||
filter := MyModelFilter{
|
||||
Ids: &[]uuid.UUID{id1, id2, id3},
|
||||
CntGT: &cnt,
|
||||
}
|
||||
|
||||
sql := "DELETE FROM my_models WHERE my_models.id IN ($1,$2,$3) AND my_models.cnt > $4"
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||
WithArgs(id1, id2, id3, cnt).
|
||||
WillReturnResult(sqlmock.NewResult(1, 123))
|
||||
mock.ExpectCommit()
|
||||
|
||||
deleted, err := repo.Delete(filter)
|
||||
assert.Equal(t, int64(123), deleted)
|
||||
assert.Nil(t, err)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -33,9 +33,9 @@ func NewMockDB() (*sql.DB, *gorm.DB, sqlmock.Sqlmock) {
|
||||
}
|
||||
|
||||
type MyModel struct {
|
||||
Id uuid.UUID
|
||||
Id *uuid.UUID `gorm:"type(uuid);unique"`
|
||||
Value string
|
||||
Count int
|
||||
Cnt int
|
||||
}
|
||||
|
||||
func (m MyModel) TableName() string {
|
||||
@ -43,10 +43,10 @@ func (m MyModel) TableName() string {
|
||||
}
|
||||
|
||||
type MyModelFilter struct {
|
||||
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
||||
Ids *[]uuid.UUID `filterfield:"field=id,operator=IN"`
|
||||
Value *string `filterfield:"field=value,operator=EQ"`
|
||||
Count *int `filterfield:"field=count,operator=GT"`
|
||||
Id *uuid.UUID `filterfield:"field=id;operator=EQ"`
|
||||
Ids *[]uuid.UUID `filterfield:"field=id;operator=IN"`
|
||||
Value *string `filterfield:"field=value;operator=EQ"`
|
||||
CntGT *int `filterfield:"field=cnt;operator=GT"`
|
||||
}
|
||||
|
||||
func TestListMethod(t *testing.T) {
|
||||
@ -65,13 +65,13 @@ func TestListMethod(t *testing.T) {
|
||||
Direction: OrderASC,
|
||||
},
|
||||
{
|
||||
Field: "count",
|
||||
Field: "cnt",
|
||||
Direction: OrderDESC,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
sql := "SELECT * FROM my_models ORDER BY id,count DESC"
|
||||
sql := "SELECT * FROM my_models ORDER BY id,cnt DESC"
|
||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
|
||||
|
||||
_, err := repo.List(filter, &options)
|
||||
@ -198,10 +198,10 @@ func TestListMethod(t *testing.T) {
|
||||
filter := MyModelFilter{
|
||||
Id: &id,
|
||||
Value: &value,
|
||||
Count: &count,
|
||||
CntGT: &count,
|
||||
}
|
||||
|
||||
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.count > $3"
|
||||
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.cnt > $3"
|
||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||
WithArgs(id, value, count)
|
||||
|
||||
@ -226,7 +226,7 @@ func TestListMethod(t *testing.T) {
|
||||
filter := MyModelFilter{
|
||||
Id: &id,
|
||||
Value: &value,
|
||||
Count: &count,
|
||||
CntGT: &count,
|
||||
}
|
||||
options := ListOptions{
|
||||
Pagination: &Pagination{
|
||||
@ -235,7 +235,7 @@ func TestListMethod(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.count > $3 LIMIT $4 OFFSET $5"
|
||||
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.cnt > $3 LIMIT $4 OFFSET $5"
|
||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||
WithArgs(id, value, count, options.Pagination.Limit, options.Pagination.Offset)
|
||||
|
||||
@ -258,11 +258,11 @@ func TestListMethod(t *testing.T) {
|
||||
options := ListOptions{
|
||||
Only: &[]string{
|
||||
"id",
|
||||
"count",
|
||||
"cnt",
|
||||
},
|
||||
}
|
||||
|
||||
sql := "SELECT id,count FROM my_models"
|
||||
sql := "SELECT id,cnt FROM my_models"
|
||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
|
||||
|
||||
_, err := repo.List(filter, &options)
|
||||
|
||||
18
app/repository/method_save.go
Normal file
18
app/repository/method_save.go
Normal file
@ -0,0 +1,18 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type SaveMethod[T schema.Tabler] struct {
|
||||
repo *RepoBase[T]
|
||||
}
|
||||
|
||||
func (m *SaveMethod[T]) Init(repo *RepoBase[T]) {
|
||||
m.repo = repo
|
||||
}
|
||||
|
||||
func (m SaveMethod[T]) Save(model *T) (*T, error) {
|
||||
result := m.repo.dbConn.Save(model)
|
||||
return model, result.Error
|
||||
}
|
||||
69
app/repository/method_save_test.go
Normal file
69
app/repository/method_save_test.go
Normal file
@ -0,0 +1,69 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSaveMethod(t *testing.T) {
|
||||
t.Run("Save new model", func(t *testing.T) {
|
||||
sqldb, db, mock := NewMockDB()
|
||||
defer sqldb.Close()
|
||||
|
||||
repo := RepoBase[MyModel]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
model := MyModel{
|
||||
Value: "some value",
|
||||
Cnt: 123,
|
||||
}
|
||||
|
||||
sql := "INSERT INTO my_models (id,value,cnt) VALUES ($1,$2,$3)"
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||
WithArgs(model.Id, model.Value, model.Cnt).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
_, err := repo.Save(&model)
|
||||
assert.Nil(t, err)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Update existing model", func(t *testing.T) {
|
||||
sqldb, db, mock := NewMockDB()
|
||||
defer sqldb.Close()
|
||||
|
||||
repo := RepoBase[MyModel]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
id := uuid.New()
|
||||
model := MyModel{
|
||||
Id: &id,
|
||||
Value: "some value",
|
||||
Cnt: 123,
|
||||
}
|
||||
|
||||
sql := "UPDATE my_models SET value=$1,cnt=$2 WHERE id = $3"
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||
WithArgs(model.Value, model.Cnt, model.Id).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
_, err := repo.Save(&model)
|
||||
assert.Nil(t, err)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
31
app/repository/method_update.go
Normal file
31
app/repository/method_update.go
Normal file
@ -0,0 +1,31 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"repo-pattern/app/repository/smartfilter"
|
||||
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type UpdateMethod[T schema.Tabler] struct {
|
||||
repo *RepoBase[T]
|
||||
}
|
||||
|
||||
func (m *UpdateMethod[T]) Init(repo *RepoBase[T]) {
|
||||
m.repo = repo
|
||||
}
|
||||
|
||||
func (m UpdateMethod[T]) Update(filter interface{}, values map[string]any) (int64, error) {
|
||||
var (
|
||||
model T
|
||||
)
|
||||
|
||||
query, err := smartfilter.ToQuery(model, filter, m.repo.dbConn)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
result := query.Model(&model).Updates(values)
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
return result.RowsAffected, nil
|
||||
}
|
||||
43
app/repository/method_update_test.go
Normal file
43
app/repository/method_update_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUpdateMethod(t *testing.T) {
|
||||
t.Run("Update", func(t *testing.T) {
|
||||
sqldb, db, mock := NewMockDB()
|
||||
defer sqldb.Close()
|
||||
|
||||
repo := RepoBase[MyModel]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
cnt := 10
|
||||
filter := MyModelFilter{
|
||||
CntGT: &cnt,
|
||||
}
|
||||
values := map[string]any{
|
||||
"cnt": 111,
|
||||
"value": 222,
|
||||
}
|
||||
|
||||
sql := "UPDATE my_models SET cnt=$1,value=$2 WHERE my_models.cnt > $3"
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||
WithArgs(values["cnt"], values["value"], cnt).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
_, err := repo.Update(filter, values)
|
||||
assert.Nil(t, err)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -5,6 +5,8 @@ import (
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
const DEFAULT_ID_FIELD = "id"
|
||||
|
||||
type MethodInitInterface[T schema.Tabler] interface {
|
||||
Init(repo *RepoBase[T])
|
||||
}
|
||||
@ -21,11 +23,13 @@ type RepoBase[T schema.Tabler] struct {
|
||||
GetMethod[T]
|
||||
ExistsMethod[T]
|
||||
CountMethod[T]
|
||||
methods []MethodInitInterface[T]
|
||||
SaveMethod[T]
|
||||
UpdateMethod[T]
|
||||
DeleteMethod[T]
|
||||
}
|
||||
|
||||
func (repo *RepoBase[T]) InitMethods() {
|
||||
for _, method := range repo.methods {
|
||||
func (repo *RepoBase[T]) InitMethods(methods []MethodInitInterface[T]) {
|
||||
for _, method := range methods {
|
||||
method.Init(repo)
|
||||
}
|
||||
}
|
||||
@ -35,18 +39,21 @@ func (m *RepoBase[T]) Init(dbConn *gorm.DB, options *RepoOptions) {
|
||||
|
||||
if options == nil {
|
||||
// no options provided? set defaults
|
||||
m.IdField = "id"
|
||||
m.IdField = DEFAULT_ID_FIELD
|
||||
} else {
|
||||
if len(options.IdField) > 0 {
|
||||
m.IdField = options.IdField
|
||||
}
|
||||
}
|
||||
|
||||
m.methods = []MethodInitInterface[T]{
|
||||
methods := []MethodInitInterface[T]{
|
||||
&m.ListMethod,
|
||||
&m.GetMethod,
|
||||
&m.ExistsMethod,
|
||||
&m.CountMethod,
|
||||
&m.SaveMethod,
|
||||
&m.UpdateMethod,
|
||||
&m.DeleteMethod,
|
||||
}
|
||||
m.InitMethods()
|
||||
m.InitMethods(methods)
|
||||
}
|
||||
|
||||
@ -11,11 +11,16 @@ import (
|
||||
)
|
||||
|
||||
const TAG_NAME = "filterfield"
|
||||
const TAG_PAIRS_SEPARATOR = ","
|
||||
const TAG_PAIRS_SEPARATOR = ";"
|
||||
const TAG_LIST_SEPARATOR = ","
|
||||
const TAG_KEYVALUE_SEPARATOR = "="
|
||||
|
||||
type handlerFunc func(query *gorm.DB, tableName string, filterField *FilterField) *gorm.DB
|
||||
|
||||
type QueryApplier interface {
|
||||
ApplyQuery(query *gorm.DB) *gorm.DB
|
||||
}
|
||||
|
||||
var operatorHandlers = map[Operator]handlerFunc{
|
||||
OperatorEQ: handleOperatorEQ,
|
||||
OperatorNE: handleOperatorNE,
|
||||
@ -67,15 +72,20 @@ func getFilterFields(filter interface{}) []ReflectedStructField {
|
||||
return res
|
||||
}
|
||||
|
||||
func getQueryApplierInterface(filter interface{}) QueryApplier {
|
||||
queryApplier, ok := filter.(QueryApplier)
|
||||
if ok {
|
||||
return queryApplier
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ToQuery(model schema.Tabler, filter interface{}, query *gorm.DB) (*gorm.DB, error) {
|
||||
st := reflect.TypeOf(filter)
|
||||
|
||||
tableName := model.TableName()
|
||||
modelName := st.Name()
|
||||
|
||||
fmt.Printf("Table name: %s\n", tableName)
|
||||
fmt.Printf("Model name: %s\n", modelName)
|
||||
|
||||
fields := getFilterFields(filter)
|
||||
for _, field := range fields {
|
||||
filterField, err := newFilterField(field.tagValue)
|
||||
@ -97,22 +107,36 @@ func ToQuery(model schema.Tabler, filter interface{}, query *gorm.DB) (*gorm.DB,
|
||||
}
|
||||
}
|
||||
|
||||
// apply custom filters, if interface exists
|
||||
queryApplier := getQueryApplierInterface(filter)
|
||||
if queryApplier != nil {
|
||||
query = queryApplier.ApplyQuery(query)
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
func splitTrim(value string, separator string) []string {
|
||||
var out []string = []string{}
|
||||
for _, s := range strings.Split(value, separator) {
|
||||
if len(s) == 0 {
|
||||
continue
|
||||
}
|
||||
out = append(out, strings.TrimSpace(s))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func newFilterField(tagValue string) (*FilterField, error) {
|
||||
filterField := FilterField{}
|
||||
|
||||
tagValue = strings.TrimSpace(tagValue)
|
||||
pairs := strings.Split(tagValue, TAG_PAIRS_SEPARATOR)
|
||||
|
||||
for _, pair := range pairs {
|
||||
kvs := strings.Split(pair, TAG_KEYVALUE_SEPARATOR)
|
||||
for _, pair := range splitTrim(tagValue, TAG_PAIRS_SEPARATOR) {
|
||||
kvs := splitTrim(pair, TAG_KEYVALUE_SEPARATOR)
|
||||
if len(kvs) != 2 {
|
||||
return nil, fmt.Errorf("invalid tag value: %s", strings.TrimSpace(pair))
|
||||
}
|
||||
key := strings.TrimSpace(kvs[0])
|
||||
value := strings.TrimSpace(kvs[1])
|
||||
key := kvs[0]
|
||||
value := kvs[1]
|
||||
|
||||
switch key {
|
||||
case "field":
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestGetFilterFields(t *testing.T) {
|
||||
@ -101,11 +102,11 @@ func TestGetFilterFields(t *testing.T) {
|
||||
|
||||
t.Run("Skip nil fields", func(t *testing.T) {
|
||||
type TestFilter struct {
|
||||
Alive *bool `filterfield:"alive,EQ"`
|
||||
Id *int64 `filterfield:"id,EQ"`
|
||||
Ids *[]uint `filterfield:"id,IN"`
|
||||
IdsNot *[]uint `filterfield:"id,NOT_IN"`
|
||||
FirstName *string `filterfield:"first_name,EQ"`
|
||||
Alive *bool `filterfield:"alive;EQ"`
|
||||
Id *int64 `filterfield:"id;EQ"`
|
||||
Ids *[]uint `filterfield:"id;IN"`
|
||||
IdsNot *[]uint `filterfield:"id;NOT_IN"`
|
||||
FirstName *string `filterfield:"first_name;EQ"`
|
||||
}
|
||||
filter := TestFilter{}
|
||||
result := getFilterFields(filter)
|
||||
@ -119,7 +120,7 @@ func TestGetFilterFields(t *testing.T) {
|
||||
)
|
||||
type TestFilter struct {
|
||||
Alive *bool
|
||||
Id *int64 `funnytag:"created_at,LT"`
|
||||
Id *int64 `funnytag:"created_at;LT"`
|
||||
}
|
||||
filter := TestFilter{
|
||||
Alive: &alive,
|
||||
@ -140,7 +141,7 @@ func TestFilterField(t *testing.T) {
|
||||
testCases := []TagParseTestCase{
|
||||
{
|
||||
name: "Parse without spaces",
|
||||
tagValue: "field=field_1,operator=EQ",
|
||||
tagValue: "field=field_1;operator=EQ",
|
||||
expected: FilterField{
|
||||
Name: "field_1",
|
||||
Operator: OperatorEQ,
|
||||
@ -148,7 +149,7 @@ func TestFilterField(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Parse spaces between pairs",
|
||||
tagValue: " field=field_2 , operator=LT ",
|
||||
tagValue: " field=field_2 ; operator=LT ",
|
||||
expected: FilterField{
|
||||
Name: "field_2",
|
||||
Operator: OperatorLT,
|
||||
@ -156,7 +157,7 @@ func TestFilterField(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Parse spaces between around keys and values",
|
||||
tagValue: "operator = LIKE , field = field_3",
|
||||
tagValue: "operator = LIKE ; field = field_3",
|
||||
expected: FilterField{
|
||||
Name: "field_3",
|
||||
Operator: OperatorLIKE,
|
||||
@ -174,19 +175,19 @@ func TestFilterField(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run("Fail on invalid tag value", func(t *testing.T) {
|
||||
filterField, err := newFilterField("field=field_1=fail, operator=EQ")
|
||||
filterField, err := newFilterField("field=field_1=fail; operator=EQ")
|
||||
assert.Nil(t, filterField)
|
||||
assert.EqualError(t, err, "invalid tag value: field=field_1=fail")
|
||||
})
|
||||
|
||||
t.Run("Fail on invalid operator", func(t *testing.T) {
|
||||
filterField, err := newFilterField("field=field_1, operator=FAIL")
|
||||
filterField, err := newFilterField("field=field_1; operator=FAIL")
|
||||
assert.Nil(t, filterField)
|
||||
assert.EqualError(t, err, "unknown operator: FAIL")
|
||||
})
|
||||
|
||||
t.Run("Fail on invalid value key", func(t *testing.T) {
|
||||
filterField, err := newFilterField("failkey=field_1, operator=FAIL")
|
||||
filterField, err := newFilterField("failkey=field_1; operator=FAIL")
|
||||
assert.Nil(t, filterField)
|
||||
assert.EqualError(t, err, "invalid value key: failkey")
|
||||
})
|
||||
@ -203,3 +204,26 @@ func TestFilterField(t *testing.T) {
|
||||
assert.EqualError(t, err, "missing operator in tag: field=field_1")
|
||||
})
|
||||
}
|
||||
|
||||
type filterWithoutQueryApplier struct{}
|
||||
|
||||
type filterWithQueryApplier struct{}
|
||||
|
||||
func (f filterWithQueryApplier) ApplyQuery(query *gorm.DB) *gorm.DB {
|
||||
return query
|
||||
}
|
||||
|
||||
func TestSmartfilterApplyQuery(t *testing.T) {
|
||||
|
||||
t.Run("Get query applier interface - without interface", func(t *testing.T) {
|
||||
f := filterWithoutQueryApplier{}
|
||||
queryApplier := getQueryApplierInterface(f)
|
||||
assert.Nil(t, queryApplier)
|
||||
})
|
||||
|
||||
t.Run("Get query applier interface - with interface", func(t *testing.T) {
|
||||
f := filterWithQueryApplier{}
|
||||
queryApplier := getQueryApplierInterface(f)
|
||||
assert.NotNil(t, queryApplier)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user