Skip to content

Commit 6a5eab8

Browse files
committed
refactor: check if the value is a MongoDB ObjectID
1 parent eb72502 commit 6a5eab8

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

pkg/mgo/query/query_condition.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,13 @@ func (c *Column) convert() error {
164164
return err
165165
}
166166

167-
if c.Name == "id" || c.Name == "_id" {
168-
if str, ok := c.Value.(string); ok {
169-
c.Name = "_id"
170-
c.Value, _ = primitive.ObjectIDFromHex(str)
171-
}
172-
} else if strings.Contains(c.Name, ":oid") {
173-
if str, ok := c.Value.(string); ok {
174-
c.Name = strings.Replace(c.Name, ":oid", "", 1)
175-
c.Value, _ = primitive.ObjectIDFromHex(str)
167+
if oid, ok := isObjectID(c.Value); ok {
168+
c.Value = oid
169+
170+
if c.Name == "id" {
171+
c.Name = "_id" // force to "_id"
172+
} else if strings.HasSuffix(c.Name, ":oid") {
173+
c.Name = strings.TrimSuffix(c.Name, ":oid")
176174
}
177175
}
178176

@@ -358,6 +356,16 @@ func (p *Params) convertMultiColumns(whitelistNames map[string]bool) (bson.M, er
358356
return filter, nil
359357
}
360358

359+
func isObjectID(v interface{}) (primitive.ObjectID, bool) {
360+
if str, ok := v.(string); ok && len(str) == 24 {
361+
value, err := primitive.ObjectIDFromHex(str)
362+
if err == nil {
363+
return value, true
364+
}
365+
}
366+
return [12]byte{}, false
367+
}
368+
361369
func checkSameLogic(columns []Column) (int, [][]int, error) {
362370
orIndexes := []int{}
363371
l := len(columns)

pkg/mgo/query/query_condition_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,9 @@ func TestParams_ConvertToMongoFilter(t *testing.T) {
440440
wantErr: false,
441441
},
442442

443+
// --------------------------- object id ------------------------------
443444
{
444-
name: "convert to object id",
445+
name: "convert to object id 1",
445446
args: args{
446447
columns: []Column{
447448
{
@@ -458,6 +459,42 @@ func TestParams_ConvertToMongoFilter(t *testing.T) {
458459
wantErr: false,
459460
},
460461

462+
{
463+
name: "convert to object id 2",
464+
args: args{
465+
columns: []Column{
466+
{
467+
Name: "userId",
468+
Value: "65ce48483f11aff697e30d6d",
469+
},
470+
{
471+
Name: "orderID",
472+
Value: "65ce48483f11aff697e30d6d",
473+
},
474+
},
475+
},
476+
want: bson.M{"$and": []bson.M{{"userId": primitive.ObjectID{0x65, 0xce, 0x48, 0x48, 0x3f, 0x11, 0xaf, 0xf6, 0x97, 0xe3, 0xd, 0x6d}}, {"orderID": primitive.ObjectID{0x65, 0xce, 0x48, 0x48, 0x3f, 0x11, 0xaf, 0xf6, 0x97, 0xe3, 0xd, 0x6d}}}},
477+
wantErr: false,
478+
},
479+
480+
{
481+
name: "convert to object id 3",
482+
args: args{
483+
columns: []Column{
484+
{
485+
Name: "_id",
486+
Value: "65ce48483f11aff697e30d6d",
487+
},
488+
{
489+
Name: "my_order",
490+
Value: "65ce48483f11aff697e30d6d",
491+
},
492+
},
493+
},
494+
want: bson.M{"$and": []bson.M{{"_id": primitive.ObjectID{0x65, 0xce, 0x48, 0x48, 0x3f, 0x11, 0xaf, 0xf6, 0x97, 0xe3, 0xd, 0x6d}}, {"my_order": primitive.ObjectID{0x65, 0xce, 0x48, 0x48, 0x3f, 0x11, 0xaf, 0xf6, 0x97, 0xe3, 0xd, 0x6d}}}},
495+
wantErr: false,
496+
},
497+
461498
// ---------------------------- error ----------------------------------------------
462499
{
463500
name: "exp type err",

0 commit comments

Comments
 (0)