From f30a0a022b180a1e8e98216006701416e34f8b64 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Fri, 24 Nov 2023 00:53:00 -0500 Subject: [PATCH] refactor: change primitive helper arg to object --- pkg/database/filter.go | 3 ++- pkg/database/memdb/filter.go | 16 ++++++++++++---- pkg/database/memdb/index.go | 2 +- pkg/primitive/object.go | 5 +---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/database/filter.go b/pkg/database/filter.go index 5fb2c18f..90c7dab1 100644 --- a/pkg/database/filter.go +++ b/pkg/database/filter.go @@ -168,7 +168,8 @@ func (ft *Filter) String() (string, error) { return ft.Key + " " + string(ft.OP), nil } - b, err := json.Marshal(primitive.Interface(ft.Value)) + v, _ := ft.Value.(primitive.Object) + b, err := json.Marshal(primitive.Interface(v)) if err != nil { return "", err } diff --git a/pkg/database/memdb/filter.go b/pkg/database/memdb/filter.go index 36b61ab9..df7b4cef 100644 --- a/pkg/database/memdb/filter.go +++ b/pkg/database/memdb/filter.go @@ -38,32 +38,40 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { return func(m *primitive.Map) bool { if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok { return false + } else if v, ok := filter.Value.(primitive.Object); !ok { + return false } else { - return util.Compare(primitive.Interface(o), primitive.Interface(filter.Value)) < 0 + return util.Compare(primitive.Interface(o), primitive.Interface(v)) < 0 } } case database.LTE: return func(m *primitive.Map) bool { if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok { return false + } else if v, ok := filter.Value.(primitive.Object); !ok { + return false } else { - return util.Compare(primitive.Interface(o), primitive.Interface(filter.Value)) <= 0 + return util.Compare(primitive.Interface(o), primitive.Interface(v)) <= 0 } } case database.GT: return func(m *primitive.Map) bool { if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok { return false + } else if v, ok := filter.Value.(primitive.Object); !ok { + return false } else { - return util.Compare(primitive.Interface(o), primitive.Interface(filter.Value)) > 0 + return util.Compare(primitive.Interface(o), primitive.Interface(v)) > 0 } } case database.GTE: return func(m *primitive.Map) bool { if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok { return false + } else if v, ok := filter.Value.(primitive.Object); !ok { + return false } else { - return util.Compare(primitive.Interface(o), primitive.Interface(filter.Value)) >= 0 + return util.Compare(primitive.Interface(o), primitive.Interface(v)) >= 0 } } case database.IN: diff --git a/pkg/database/memdb/index.go b/pkg/database/memdb/index.go index bdf68602..c0a01abe 100644 --- a/pkg/database/memdb/index.go +++ b/pkg/database/memdb/index.go @@ -156,7 +156,7 @@ func (iv *IndexView) findMany(_ context.Context, examples []*primitive.Map) ([]p var i int var k string for i, k = range model.Keys { - if obj, ok := primitive.Get[any](example, k); ok { + if obj, ok := primitive.Get[primitive.Object](example, k); ok { v := primitive.Interface(obj) hash, err := util.Hash(v) diff --git a/pkg/primitive/object.go b/pkg/primitive/object.go index 6caedf9d..2a9890e5 100644 --- a/pkg/primitive/object.go +++ b/pkg/primitive/object.go @@ -45,12 +45,9 @@ func Equal(x, y Object) bool { } } - -func Interface(v any) any { +func Interface(v Object) any { if v == nil { return nil - } else if v, ok := v.(Object); !ok { - return nil } else { return v.Interface() }