Skip to content

Commit

Permalink
[TT-9704] fix a bug where only a single key from nested query is cons…
Browse files Browse the repository at this point in the history
…idered. (#81)

* fix a bug where only a single key from nested query is considered.

* fix sonarcloud reported bugs
  • Loading branch information
jeffy-mathew authored Aug 8, 2023
1 parent 0871150 commit 3e6ea91
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
9 changes: 8 additions & 1 deletion persistent/internal/driver/mgo/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ func handleNestedQuery(search bson.M, key string, value interface{}) {
search[key] = bson.M{"$regex": bson.RegEx{Pattern: regexp.QuoteMeta(stringValue), Options: "i"}}
}
default:
search[key] = bson.M{nestedKey: nestedValue}
if v, ok := search[key]; !ok {
search[key] = bson.M{nestedKey: nestedValue}
} else {
if nestedQ, ok := v.(bson.M); ok {
nestedQ[nestedKey] = nestedValue
search[key] = nestedQ
}
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions persistent/internal/driver/mgo/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ func TestBuildQuery(t *testing.T) {
},
},
},
{
name: "Test nested query with more than 1 key",
input: model.DBM{
"value": model.DBM{
"$gte": 0,
"$lte": 10,
},
},
output: bson.M{
"value": bson.M{
"$gte": 0,
"$lte": 10,
},
},
},
{
name: "Test with $in query",
input: model.DBM{
Expand Down
9 changes: 8 additions & 1 deletion persistent/internal/driver/mongo/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ func handleNestedQuery(search bson.M, key string, value interface{}) {
search[key] = bson.M{"$regex": primitive.Regex{Pattern: regexp.QuoteMeta(stringValue), Options: "i"}}
}
default:
search[key] = bson.M{nestedKey: nestedValue}
if v, ok := search[key]; !ok {
search[key] = bson.M{nestedKey: nestedValue}
} else {
if nestedQ, ok := v.(bson.M); ok {
nestedQ[nestedKey] = nestedValue
search[key] = nestedQ
}
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions persistent/internal/driver/mongo/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ func TestBuildQuery(t *testing.T) {
},
},
},
{
testName: "Test nested query with more than 1 key",
input: model.DBM{
"value": model.DBM{
"$gte": 0,
"$lte": 10,
},
},
output: bson.M{
"value": bson.M{
"$gte": 0,
"$lte": 10,
},
},
},
{
testName: "Test with $in query",
input: model.DBM{
Expand Down

0 comments on commit 3e6ea91

Please sign in to comment.