From 7452dd4809fbbb8af5dc8413bb1eca5f23f1fc69 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Wed, 29 Nov 2023 01:32:26 -0500 Subject: [PATCH] refactor: move path parse to memdb --- pkg/database/memdb/filter.go | 32 ++++++++++++++++++++++---------- pkg/database/memdb/sort.go | 4 ++-- pkg/primitive/pick.go | 15 +-------------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/pkg/database/memdb/filter.go b/pkg/database/memdb/filter.go index e94e3466..ea3f70c4 100644 --- a/pkg/database/memdb/filter.go +++ b/pkg/database/memdb/filter.go @@ -1,10 +1,17 @@ package memdb import ( + "regexp" + "strings" + "github.com/siyul-park/uniflow/pkg/database" "github.com/siyul-park/uniflow/pkg/primitive" ) +var ( + numberSubPath = regexp.MustCompile(`\[([0-9]+)\]`) +) + func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { if filter == nil { return func(_ *primitive.Map) bool { @@ -15,7 +22,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { switch filter.OP { case database.EQ: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) == 0 @@ -23,7 +30,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.NE: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) != 0 @@ -31,7 +38,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.LT: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) < 0 @@ -39,7 +46,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.LTE: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) <= 0 @@ -47,7 +54,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.GT: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) > 0 @@ -55,7 +62,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.GTE: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) >= 0 @@ -63,7 +70,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.IN: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return false } else if o == nil { return false @@ -80,7 +87,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.NIN: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return true } else if o == nil { return true @@ -97,7 +104,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.NULL: return func(m *primitive.Map) bool { - if v, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if v, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return false } else { return v == nil @@ -105,7 +112,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.NNULL: return func(m *primitive.Map) bool { - if v, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok { + if v, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { return false } else { return v != nil @@ -212,3 +219,8 @@ func FilterToExample(filter *database.Filter) ([]*primitive.Map, bool) { return nil, false } + +func parsePath(key string) []string { + key = numberSubPath.ReplaceAllString(key, ".$1") + return strings.Split(key, ".") +} diff --git a/pkg/database/memdb/sort.go b/pkg/database/memdb/sort.go index fbafa7f0..ccabc69f 100644 --- a/pkg/database/memdb/sort.go +++ b/pkg/database/memdb/sort.go @@ -8,8 +8,8 @@ import ( func ParseSorts(sorts []database.Sort) func(i, j *primitive.Map) bool { return func(i, j *primitive.Map) bool { for _, s := range sorts { - x, _ := primitive.Pick[primitive.Object](i, s.Key) - y, _ := primitive.Pick[primitive.Object](j, s.Key) + x, _ := primitive.Pick[primitive.Object](i, parsePath(s.Key)...) + y, _ := primitive.Pick[primitive.Object](j, parsePath(s.Key)...) if x == y { continue diff --git a/pkg/primitive/pick.go b/pkg/primitive/pick.go index dea96aa8..e9d942df 100644 --- a/pkg/primitive/pick.go +++ b/pkg/primitive/pick.go @@ -1,18 +1,10 @@ package primitive import ( - "regexp" "strconv" - "strings" ) -var ( - numberSubPath = regexp.MustCompile(`\[([0-9]+)\]`) -) - -func Pick[T any](v Object, path string) (T, bool) { - paths := parsePath(path) - +func Pick[T any](v Object, paths ...string) (T, bool) { var zero T cur := v @@ -46,8 +38,3 @@ func Pick[T any](v Object, path string) (T, bool) { return zero, false } } - -func parsePath(key string) []string { - key = numberSubPath.ReplaceAllString(key, ".$1") - return strings.Split(key, ".") -}