[wip] impove filter interfaces

Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
Anton Nesterov 2024-08-08 19:44:12 +02:00
parent 4198ec4c75
commit 9fae95b906
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
5 changed files with 19 additions and 10 deletions

View file

@ -1,21 +1,19 @@
package dal package dal
import ( import (
"fmt"
"strings" "strings"
filters "l12.xyz/dal/filters" filters "l12.xyz/dal/filters"
) )
func CovertFind(find filters.Find, ctx filters.Context) string { func CovertFind(find Find, ctx Context) string {
expressions := []string{} expressions := []string{}
for key, value := range find { for key, value := range find {
values, err := filters.Convert(ctx.New(map[string]string{ context := ctx.New(CtxOpts{
"FieldName": key, "FieldName": key,
}), value) })
values, _ := filters.Convert(context, value)
expressions = append(expressions, values) expressions = append(expressions, values)
fmt.Println(err)
} }
return strings.Join(expressions, " AND ") return strings.Join(expressions, " AND ")
} }

View file

@ -7,10 +7,10 @@ import (
) )
func TestConvertFind(t *testing.T) { func TestConvertFind(t *testing.T) {
find := f.Find{ find := Find{
"test": "1", "test": "1",
"test2": "2", "test2": "2",
"test3": f.Filter{ "test3": Filter{
"$ne": 1, "$ne": 1,
}, },
} }

10
pkg/dal/types.go Normal file
View file

@ -0,0 +1,10 @@
package dal
import (
filters "l12.xyz/dal/filters"
)
type Find = filters.Find
type Filter = filters.Filter
type Context = filters.Context
type CtxOpts = filters.CtxOpts

View file

@ -14,7 +14,7 @@ type SQLiteContext struct {
FieldName string FieldName string
} }
func (c SQLiteContext) New(opts map[string]string) Context { func (c SQLiteContext) New(opts CtxOpts) Context {
ta := opts["TableAlias"] ta := opts["TableAlias"]
if ta == "" { if ta == "" {
ta = c.TableAlias ta = c.TableAlias

View file

@ -1,7 +1,8 @@
package filters package filters
type CtxOpts map[string]string
type Context interface { type Context interface {
New(opts map[string]string) Context New(opts CtxOpts) Context
GetFieldName() string GetFieldName() string
NormalizeValue(interface{}) interface{} NormalizeValue(interface{}) interface{}
} }