Add graph.Search for substr

This commit is contained in:
Bel LaPointe
2020-07-22 22:42:21 -06:00
parent 660cd64262
commit db1c36fc0f
3 changed files with 46 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ package storage
import (
"context"
"fmt"
"local/dndex/storage/entity"
"local/dndex/storage/operator"
@@ -19,12 +20,25 @@ func NewGraph() Graph {
}
}
func (g Graph) Search(ctx context.Context, namespace string, nameContains string) ([]entity.One, error) {
filter := operator.Regex{Key: entity.Name, Value: fmt.Sprintf(".*%s.*", nameContains)}
return g.find(ctx, namespace, filter)
}
func (g Graph) List(ctx context.Context, namespace string, from ...string) ([]entity.One, error) {
filter := operator.NewFilterIn("_id", from)
filter := operator.NewFilterIn(entity.Name, from)
return g.find(ctx, namespace, filter)
}
func (g Graph) find(ctx context.Context, namespace string, filter interface{}) ([]entity.One, error) {
ch, err := g.mongo.Find(ctx, namespace, filter)
if err != nil {
return nil, err
}
return g.gatherOnes(ctx, ch)
}
func (g Graph) gatherOnes(ctx context.Context, ch <-chan bson.Raw) ([]entity.One, error) {
var ones []entity.One
for one := range ch {
var o entity.One