Add graph.Search for substr
parent
660cd64262
commit
db1c36fc0f
|
|
@ -2,6 +2,7 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"local/dndex/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"local/dndex/storage/operator"
|
"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) {
|
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)
|
ch, err := g.mongo.Find(ctx, namespace, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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
|
var ones []entity.One
|
||||||
for one := range ch {
|
for one := range ch {
|
||||||
var o entity.One
|
var o entity.One
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ func TestIntegration(t *testing.T) {
|
||||||
|
|
||||||
os.Args = os.Args[:1]
|
os.Args = os.Args[:1]
|
||||||
graph := NewGraph()
|
graph := NewGraph()
|
||||||
graph.mongo.db = "test-db"
|
graph.mongo.db = "db"
|
||||||
ctx, can := context.WithCancel(context.TODO())
|
ctx, can := context.WithCancel(context.TODO())
|
||||||
defer can()
|
defer can()
|
||||||
clean := func() {
|
clean := func() {
|
||||||
|
|
@ -67,6 +67,17 @@ func TestIntegration(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("graph.Search(foo => *)", func(t *testing.T) {
|
||||||
|
some, err := graph.Search(ctx, "col", ones[0].Name[:3])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Logf("searching for %s (%s)\nsom = %+v", ones[0].Name, ones[0].Name[:3], some)
|
||||||
|
if len(some) < 1 {
|
||||||
|
t.Fatalf("%+v: %+v", len(some), some)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("graph.Update(foo, --bar)", func(t *testing.T) {
|
t.Run("graph.Update(foo, --bar)", func(t *testing.T) {
|
||||||
err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{entity.Connections, map[string]interface{}{}})
|
err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{entity.Connections, map[string]interface{}{}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,15 @@ import (
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Regex struct {
|
||||||
|
Key string
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (re Regex) MarshalBSON() ([]byte, error) {
|
||||||
|
return filterMarshal("$regex", re.Key, re.Value)
|
||||||
|
}
|
||||||
|
|
||||||
type FilterIn struct {
|
type FilterIn struct {
|
||||||
Key string
|
Key string
|
||||||
Values []interface{}
|
Values []interface{}
|
||||||
|
|
@ -46,12 +55,17 @@ func NewFilterIn(key string, values interface{}) FilterIn {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fi FilterIn) MarshalBSON() ([]byte, error) {
|
func (fi FilterIn) MarshalBSON() ([]byte, error) {
|
||||||
if len(fi.Key) == 0 {
|
return filterMarshal("$in", fi.Key, fi.Values)
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterMarshal(op, key string, value interface{}) ([]byte, error) {
|
||||||
|
if len(key) == 0 {
|
||||||
return bson.Marshal(map[string]interface{}{})
|
return bson.Marshal(map[string]interface{}{})
|
||||||
}
|
}
|
||||||
return bson.Marshal(map[string]map[string][]interface{}{
|
m := map[string]map[string]interface{}{
|
||||||
fi.Key: map[string][]interface{}{
|
key: map[string]interface{}{
|
||||||
"$in": fi.Values,
|
op: value,
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
return bson.Marshal(m)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue