ApplyQuery interface
This commit is contained in:
21
app/main.go
21
app/main.go
@ -29,7 +29,18 @@ type CertFilter struct {
|
|||||||
IdsNot *[]string `filterfield:"field=id;operator=NOT_IN"`
|
IdsNot *[]string `filterfield:"field=id;operator=NOT_IN"`
|
||||||
CreatedAt_Lt *time.Time `filterfield:"field=created_at;operator=LT"`
|
CreatedAt_Lt *time.Time `filterfield:"field=created_at;operator=LT"`
|
||||||
Timestamps *[]time.Time `filterfield:"field=created_at;operator=IN"`
|
Timestamps *[]time.Time `filterfield:"field=created_at;operator=IN"`
|
||||||
// CompanyIsActive *bool `filterfield:"joins=companies;field=is_active;operator=EQ"`
|
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 {
|
type CompanyFilter struct {
|
||||||
@ -99,7 +110,7 @@ func doListWithJoins(db *gorm.DB) {
|
|||||||
repo.Init(db, nil)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
filter := CertFilter{
|
filter := CertFilter{
|
||||||
Alive: &TRUE,
|
CompanyIsActive: &FALSE,
|
||||||
}
|
}
|
||||||
|
|
||||||
certs, err := repo.List(filter, nil)
|
certs, err := repo.List(filter, nil)
|
||||||
@ -203,11 +214,11 @@ func main() {
|
|||||||
db := db.InitDB()
|
db := db.InitDB()
|
||||||
|
|
||||||
// doMagic(db)
|
// doMagic(db)
|
||||||
doList(db)
|
// doList(db)
|
||||||
// doListWithJoins(db)
|
doListWithJoins(db)
|
||||||
// doCount(db)
|
// doCount(db)
|
||||||
// doSave(db)
|
// doSave(db)
|
||||||
doDelete(db)
|
// doDelete(db)
|
||||||
// doGet(db)
|
// doGet(db)
|
||||||
// doExists(db)
|
// doExists(db)
|
||||||
// inheritance.DoInheritanceTest()
|
// inheritance.DoInheritanceTest()
|
||||||
|
|||||||
@ -11,7 +11,6 @@ import (
|
|||||||
type FilterField struct {
|
type FilterField struct {
|
||||||
Name string
|
Name string
|
||||||
Operator Operator
|
Operator Operator
|
||||||
Joins []string
|
|
||||||
|
|
||||||
valueKind reflect.Kind
|
valueKind reflect.Kind
|
||||||
boolValue *bool
|
boolValue *bool
|
||||||
|
|||||||
@ -12,10 +12,15 @@ import (
|
|||||||
|
|
||||||
const TAG_NAME = "filterfield"
|
const TAG_NAME = "filterfield"
|
||||||
const TAG_PAIRS_SEPARATOR = ";"
|
const TAG_PAIRS_SEPARATOR = ";"
|
||||||
|
const TAG_LIST_SEPARATOR = ","
|
||||||
const TAG_KEYVALUE_SEPARATOR = "="
|
const TAG_KEYVALUE_SEPARATOR = "="
|
||||||
|
|
||||||
type handlerFunc func(query *gorm.DB, tableName string, filterField *FilterField) *gorm.DB
|
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{
|
var operatorHandlers = map[Operator]handlerFunc{
|
||||||
OperatorEQ: handleOperatorEQ,
|
OperatorEQ: handleOperatorEQ,
|
||||||
OperatorNE: handleOperatorNE,
|
OperatorNE: handleOperatorNE,
|
||||||
@ -67,15 +72,20 @@ func getFilterFields(filter interface{}) []ReflectedStructField {
|
|||||||
return res
|
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) {
|
func ToQuery(model schema.Tabler, filter interface{}, query *gorm.DB) (*gorm.DB, error) {
|
||||||
st := reflect.TypeOf(filter)
|
st := reflect.TypeOf(filter)
|
||||||
|
|
||||||
tableName := model.TableName()
|
tableName := model.TableName()
|
||||||
modelName := st.Name()
|
modelName := st.Name()
|
||||||
|
|
||||||
fmt.Printf("Table name: %s\n", tableName)
|
|
||||||
fmt.Printf("Model name: %s\n", modelName)
|
|
||||||
|
|
||||||
fields := getFilterFields(filter)
|
fields := getFilterFields(filter)
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
filterField, err := newFilterField(field.tagValue)
|
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
|
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) {
|
func newFilterField(tagValue string) (*FilterField, error) {
|
||||||
filterField := FilterField{}
|
filterField := FilterField{}
|
||||||
|
|
||||||
tagValue = strings.TrimSpace(tagValue)
|
for _, pair := range splitTrim(tagValue, TAG_PAIRS_SEPARATOR) {
|
||||||
pairs := strings.Split(tagValue, TAG_PAIRS_SEPARATOR)
|
kvs := splitTrim(pair, TAG_KEYVALUE_SEPARATOR)
|
||||||
|
|
||||||
for _, pair := range pairs {
|
|
||||||
kvs := strings.Split(pair, TAG_KEYVALUE_SEPARATOR)
|
|
||||||
if len(kvs) != 2 {
|
if len(kvs) != 2 {
|
||||||
return nil, fmt.Errorf("invalid tag value: %s", strings.TrimSpace(pair))
|
return nil, fmt.Errorf("invalid tag value: %s", strings.TrimSpace(pair))
|
||||||
}
|
}
|
||||||
key := strings.TrimSpace(kvs[0])
|
key := kvs[0]
|
||||||
value := strings.TrimSpace(kvs[1])
|
value := kvs[1]
|
||||||
|
|
||||||
switch key {
|
switch key {
|
||||||
case "field":
|
case "field":
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetFilterFields(t *testing.T) {
|
func TestGetFilterFields(t *testing.T) {
|
||||||
@ -203,3 +204,32 @@ func TestFilterField(t *testing.T) {
|
|||||||
assert.EqualError(t, err, "missing operator in tag: field=field_1")
|
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)
|
||||||
|
})
|
||||||
|
|
||||||
|
// t.Run("Get query applier interface - call interface function", func(t *testing.T) {
|
||||||
|
// f := filterWithQueryApplier{}
|
||||||
|
// queryApplier := getQueryApplierInterface(f)
|
||||||
|
// assert.NotNil(t, queryApplier)
|
||||||
|
// })
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user