Skip to content

Commit

Permalink
refactor: change primitive helper arg to object
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 24, 2023
1 parent 777e57b commit f30a0a0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion pkg/database/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/database/memdb/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/memdb/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions pkg/primitive/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit f30a0a0

Please sign in to comment.