From b4679a154f3c4e85e28a8cbc271c25d58a9152c5 Mon Sep 17 00:00:00 2001 From: George Suntres Date: Sat, 11 Apr 2026 14:10:31 -0400 Subject: [PATCH] Add filter conversion --- filter.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ find.go | 54 ++++------------------------ main_index.go | 2 +- 3 files changed, 104 insertions(+), 49 deletions(-) create mode 100644 filter.go diff --git a/filter.go b/filter.go new file mode 100644 index 0000000..fdcaff8 --- /dev/null +++ b/filter.go @@ -0,0 +1,97 @@ +package mongo + +// import "runtime" +import "log" + +type Query struct { + Filter map[string]any +} + +type Filter struct { + Name string + Op string + Value string +} + +func makeFilter(name string, value any) *Filter { + + log.Printf("type is %T", value) + + var op string + + var v string + + switch value.(type) { + case string: + op = "eq" + v = value.(string) + case map[string]any: + vMap := value.(map[string]any) + for kk, vv := range vMap { + op = kk + v = vv.(string) + + break + } + + default: + op = "eq" + } + + o := &Filter{ + Name: name, + Op: op, + Value: v, + } + + return o +} + +func Mongofy(q *Query) map[string]any { + + conditions := make([]map[string]interface{}, 0) + + logic := "and" + + // runtime.Breakpoint() + for k, v := range q.Filter { + if k == "_logic" { + logic = v.(string) + + continue + } + + f := makeFilter(k, v) + + log.Printf("=> filter: %#v", f) + + mongoOp := mapToOp(f.Op) + + conditions = append(conditions, map[string]any{ k: map[string]any{ mongoOp: f.Value }}) + } + + if logic == "or" { + return map[string]any{ + "$or": conditions, + } + } + // if filter == nil { + // return filter + // } + + return map[string]any{ + "$and": conditions, + } +} + +func mapToOp(v string) string { + switch v { + case "eq": + return "$eq" + case "includes": + return "$regex" + default: + log.Printf("WARN: no conversion") + return v + } +} \ No newline at end of file diff --git a/find.go b/find.go index ecc2ab4..40e03ee 100644 --- a/find.go +++ b/find.go @@ -1,15 +1,9 @@ package mongo import ( - // "log" "context" - // "fmt" - // "encoding/base64" - - // "go.mongodb.org/mongo-driver/v2/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" - - // "git.gsuntres.com/boxtep/boxtep/core" ) // Find is used to fetch the first page of data. @@ -20,16 +14,7 @@ func (c *MongoClient) Find(ctx context.Context, database, name string, filter bs pageSize := max(limit, c.Limit) - - // id := DecodeCursor(nextCursor) - - // filter["_id"] = bson.M{"_id": bson.M{"$gt": id}} - // opts := options.Find(). - // SetLimit(pageSize + 1). - // SetSort(bson.M{"_id": 1}) - // id := DecodeCursor(nextCursor) sort := bson.M{"_id": 1} - // filter["_id"] = bson.M{"_id": bson.M{"$gt": id}} pipeline := BuildPaginationPipeline(0, pageSize + 1, filter, sort) @@ -91,16 +76,6 @@ func (c *MongoClient) Find(ctx context.Context, database, name string, filter bs out["next_cursor"] = nextCursor } - // _data, err := bson.Marshal(out) - // if err != nil { - // return nil, err - // } - - // var r bson.M - // if err := bson.Unmarshal(_data, &r); err != nil { - // return nil, err - // } - return out, nil } @@ -109,8 +84,12 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil collection := c.GetCollection(database, name) finalLimit := max(limit, c.Limit) + + f := Mongofy(&Query{ + Filter: filter, + }) - pipeline := BuildPaginationPipeline(offset, finalLimit, filter, nil) + pipeline := BuildPaginationPipeline(offset, finalLimit, f, nil) // 2. Query cursor, err := collection.Aggregate(ctx, pipeline) @@ -163,24 +142,3 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil return out, nil } -// func (c *MongoClient) FindNext(ctx context.Context, database, name string, filter bson.M, nextCursor string, limit int64) ([]bson.M, error) { -// collection := c.GetCollection(database, name) - -// opts := options.Find(). -// SetLimit(max(limit, c.Limit)). -// SetSort() - -// cursor, err := collection.Find(ctx, filter) -// if err != nil { -// return nil, err -// } - -// var results []bson.M -// if err = cursor.All(ctx, &results); err != nil { -// return nil, err -// } - -// return results, err -// } - -// func EncodeCursor() diff --git a/main_index.go b/main_index.go index ded84c3..2eb9c9e 100644 --- a/main_index.go +++ b/main_index.go @@ -45,7 +45,7 @@ func (c *MongoClient) CreateIndexes(collection *mongo.Collection, cdef *Collecti var keysBson bson.D if err := bson.Unmarshal(keysVal.Value, &keysBson); err != nil { - log.Printf("failed to unmarshal keys value %v", err) + log.Printf("failed to unmarshal keys value %v, %v", keysVal, err) continue }