Skip to content

Commit

Permalink
Merge pull request #4 from 9spokes/32186
Browse files Browse the repository at this point in the history
updated to accept multiple connections
  • Loading branch information
Tyhun05 authored Sep 28, 2023
2 parents 5d28a56 + 6d05c0f commit 60e857d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion example/odata.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func example() {
var object []interface{}
collection := mainSession.Database("testdb").Collection("collectionName")

if _, err := odata.ODataQuery("", testURL.Query(), &object, collection); err != nil {
if _, err := odata.ODataQuery(nil, testURL.Query(), &object, collection); err != nil {
fmt.Errorf("Error: %s", err.Error())
}
}
31 changes: 16 additions & 15 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,24 @@ type Query struct {
Sort bson.D
}

func addConnectionToAndQuery(connectionID string, filterObj bson.M) []bson.M {
func addConnectionToAndQuery(connectionIDs []string, filterObj bson.M) []bson.M {
andFilter, _ := filterObj["$and"].([]bson.M)
return append(andFilter, bson.M{
"$or": []bson.M{
{
"connection_id": connectionID,
},
{
"connection": connectionID,
},
},
})

orQuery := bson.M{"$or": []bson.M{}}

for _, connection := range connectionIDs {
orQuery["$or"] = append(orQuery["$or"].([]bson.M), bson.M{
"connection_id": connection,
}, bson.M{
"connection": connection,
})
}
return append(andFilter, orQuery)
}

// ODataQuery creates a mgo query based on odata parameters
// nolint :gocyclo
func ODataQuery(connectionID string, query url.Values, object interface{}, collection *mongo.Collection) (int64, error) {
func ODataQuery(connectionIDs []string, query url.Values, object interface{}, collection *mongo.Collection) (int64, error) {

// Parse url values
queryMap, err := parser.ParseURLValues(query)
Expand All @@ -67,7 +68,7 @@ func ODataQuery(connectionID string, query url.Values, object interface{}, colle
return 0, errors.Wrap(ErrInvalidInput, err.Error())
}
}
filterObj["$and"] = addConnectionToAndQuery(connectionID, filterObj)
filterObj["$and"] = addConnectionToAndQuery(connectionIDs, filterObj)

// Prepare Select
selectMap := make(bson.M)
Expand Down Expand Up @@ -121,7 +122,7 @@ func ODataQuery(connectionID string, query url.Values, object interface{}, colle
return count, nil
}

func GetODataQuery(connectionID string, query url.Values) (Query, error) {
func GetODataQuery(connectionIDs []string, query url.Values) (Query, error) {

var odataQuery Query
// Parse url values
Expand All @@ -145,7 +146,7 @@ func GetODataQuery(connectionID string, query url.Values) (Query, error) {
return odataQuery, errors.Wrap(ErrInvalidInput, err.Error())
}
}
filterObj["$and"] = addConnectionToAndQuery(connectionID, filterObj)
filterObj["$and"] = addConnectionToAndQuery(connectionIDs, filterObj)

odataQuery.Filter = filterObj

Expand Down
10 changes: 5 additions & 5 deletions mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestODataQuery(t *testing.T) {
var object []interface{}
collection := mainSession.Database("test").Collection("tests")

if _, err := ODataQuery("", testURL.Query(), &object, collection); err != nil {
if _, err := ODataQuery(nil, testURL.Query(), &object, collection); err != nil {
t.Errorf("Error: %s", err.Error())
}
}
Expand All @@ -54,7 +54,7 @@ func TestODataFilter(t *testing.T) {
var object []interface{}
collection := mainSession.Database("test").Collection("tests")

if _, err := ODataQuery("", testURL.Query(), &object, collection); err != nil {
if _, err := ODataQuery(nil, testURL.Query(), &object, collection); err != nil {
t.Errorf("Error: %s", err.Error())
}
}
Expand All @@ -74,7 +74,7 @@ func TestODataFunctions(t *testing.T) {
var object []interface{}
collection := mainSession.Database("test").Collection("tests")

if _, err := ODataQuery("", testURL.Query(), &object, collection); err != nil {
if _, err := ODataQuery(nil, testURL.Query(), &object, collection); err != nil {
t.Errorf("Error: %s", err.Error())
}

Expand All @@ -95,7 +95,7 @@ func TestODataFilterGreaterLessThan(t *testing.T) {
var object []interface{}
collection := mainSession.Database("test").Collection("tests")

if _, err := ODataQuery("", testURL.Query(), &object, collection); err != nil {
if _, err := ODataQuery(nil, testURL.Query(), &object, collection); err != nil {
t.Errorf("Error: %s", err.Error())
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestODataWithFilter(t *testing.T) {
return
}

_, queryErr := ODataQuery("", testURL.Query(), &object, collection)
_, queryErr := ODataQuery(nil, testURL.Query(), &object, collection)

// an error happened, is it expected?
if (queryErr != nil) != (expectedVal.Err != nil) {
Expand Down

0 comments on commit 60e857d

Please sign in to comment.