Skip to content

Commit

Permalink
fix(server): bson array support (#58)
Browse files Browse the repository at this point in the history
bson array support

Co-authored-by: tomokazu tantaka <t.tantaka@eukarya.io>
  • Loading branch information
hexaforce and tomokazu tantaka authored Nov 28, 2024
1 parent 4fcdd23 commit 9b8bc8a
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 11 deletions.
40 changes: 31 additions & 9 deletions mongox/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package mongox

import "go.mongodb.org/mongo-driver/bson"
import (
"encoding/json"
"fmt"

"go.mongodb.org/mongo-driver/bson"
)

func AppendE(f interface{}, elements ...bson.E) interface{} {
switch f2 := f.(type) {
Expand Down Expand Up @@ -199,16 +204,33 @@ func AddCondition(filter interface{}, key string, condition interface{}) interfa
newCondition = condition
}

if existingAnd, ok := filterMap["$and"].(bson.A); ok {
filterMap["$and"] = append(existingAnd, newCondition)
} else {
existingConditions := make(bson.A, 0)
for k, v := range filterMap {
if k != "$and" {
existingConditions = append(existingConditions, bson.M{k: v})
if newConditionMap, ok := newCondition.(bson.M); ok {
if existingAnd, ok := filterMap["$and"].(bson.A); ok {
filterMap["$and"] = append(existingAnd, newConditionMap)
} else if existingAnd, ok := filterMap["$and"].([]bson.M); ok {
filterMap["$and"] = append(existingAnd, newConditionMap)
} else {
existingConditions := make(bson.A, 0)
for k, v := range filterMap {
if k != "$and" {
existingConditions = append(existingConditions, bson.M{k: v})
}
}
filterMap["$and"] = append(existingConditions, newConditionMap)
}
filterMap["$and"] = append(existingConditions, newCondition)
} else {
return filter
}

return filterMap
}

func IndentPrint(title string, data interface{}) {
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Printf("===== %s =====\n", title)
fmt.Println(string(jsonData))
}
}
150 changes: 148 additions & 2 deletions mongox/util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mongox

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -375,10 +377,10 @@ func TestAddConditionEmptySliceCondition(t *testing.T) {
assert.Equal(t, expected, actual)
}

func TestAddConditionProjectRefetchFilter(t *testing.T) {
func TestAddConditionProjectRefetchFilter_bsonM(t *testing.T) {
team := "team_id_example"
last := "last_project_id"
updatedat := 1654849072592
updatedat := 1111111111111
filter := bson.M{
"$and": bson.A{
bson.M{
Expand Down Expand Up @@ -429,5 +431,149 @@ func TestAddConditionProjectRefetchFilter(t *testing.T) {
}

actual := AddCondition(filter, "", condition)
JSONEqAny(t, expected, actual)
assert.Equal(t, expected, actual)
}

func TestAddConditionProjectRefetchFilter_bsonA(t *testing.T) {
team := "team_id_example"
last := "last_project_id"
updatedat := 1111111111111
filter := bson.M{
"$and": bson.A{
bson.M{
"$or": bson.A{
bson.M{"deleted": false},
bson.M{"deleted": bson.M{"$exists": false}},
},
},
bson.M{
"$or": bson.A{
bson.M{"coresupport": true},
bson.M{"coresupport": bson.M{"$exists": false}},
},
},
},
"team": team,
}

condition := bson.M{
"$or": []bson.M{
{"updatedat": bson.M{"$lt": updatedat}},
{"id": bson.M{"$lt": last}, "updatedat": updatedat},
},
}
expected := bson.M{
"$and": bson.A{
bson.M{
"$or": bson.A{
bson.M{"deleted": false},
bson.M{"deleted": bson.M{"$exists": false}},
},
},
bson.M{
"$or": bson.A{
bson.M{"coresupport": true},
bson.M{"coresupport": bson.M{"$exists": false}},
},
},
bson.M{
"$or": []bson.M{
{"updatedat": bson.M{"$lt": updatedat}},
{"id": bson.M{"$lt": last}, "updatedat": updatedat},
},
},
},
"team": team,
}

actual := AddCondition(filter, "", condition)
JSONEqAny(t, expected, actual)
assert.Equal(t, expected, actual)
}

func TestAddConditionProjectRefetchFilterWithKeyword(t *testing.T) {
team := "team_id_example"
last := "last_project_id"
updatedat := 1654849072592

filter := bson.M{
"$and": []bson.M{
{
"$and": []bson.M{
{
"$or": []bson.M{
{"deleted": false},
{"deleted": bson.M{"$exists": false}},
},
},
{
"$or": []bson.M{
{"coresupport": true},
{"coresupport": bson.M{"$exists": false}},
},
},
},
},
{"team": team},
{"name": bson.M{"$regex": bson.M{"pattern": ".*test.*", "options": "i"}}},
},
}

condition := bson.M{
"$or": []bson.M{
{"updatedat": bson.M{"$lt": updatedat}},
{"id": bson.M{"$lt": last}, "updatedat": updatedat},
},
}

expected := bson.M{
"$and": []bson.M{
{
"$and": []bson.M{
{
"$or": []bson.M{
{"deleted": false}, {"deleted": bson.M{"$exists": false}},
},
},
{
"$or": []bson.M{
{"coresupport": true}, {"coresupport": bson.M{"$exists": false}},
},
},
},
},
{"team": team},
{
"name": bson.M{
"$regex": bson.M{
"pattern": ".*test.*",
"options": "i",
},
},
},
{
"$or": []bson.M{
{"updatedat": bson.M{"$lt": updatedat}},
{"id": bson.M{"$lt": last}, "updatedat": updatedat},
},
},
},
}

actual := AddCondition(filter, "", condition)
JSONEqAny(t, expected, actual)
assert.Equal(t, expected, actual)
}

func JSONEqAny(t *testing.T, expected interface{}, actual interface{}) {
e, err := json.Marshal(expected)
if err != nil {
fmt.Println(err.Error())
}
a, err := json.Marshal(actual)
if err != nil {
fmt.Println(err.Error())
}
assert.JSONEq(t, string(e), string(a))
}

0 comments on commit 9b8bc8a

Please sign in to comment.