mirror of https://github.com/zhufuyi/sponge
refactor: check if the value is a MongoDB ObjectID
This commit is contained in:
parent
eb725020d4
commit
6a5eab8a6a
|
@ -164,15 +164,13 @@ func (c *Column) convert() error {
|
|||
return err
|
||||
}
|
||||
|
||||
if c.Name == "id" || c.Name == "_id" {
|
||||
if str, ok := c.Value.(string); ok {
|
||||
c.Name = "_id"
|
||||
c.Value, _ = primitive.ObjectIDFromHex(str)
|
||||
}
|
||||
} else if strings.Contains(c.Name, ":oid") {
|
||||
if str, ok := c.Value.(string); ok {
|
||||
c.Name = strings.Replace(c.Name, ":oid", "", 1)
|
||||
c.Value, _ = primitive.ObjectIDFromHex(str)
|
||||
if oid, ok := isObjectID(c.Value); ok {
|
||||
c.Value = oid
|
||||
|
||||
if c.Name == "id" {
|
||||
c.Name = "_id" // force to "_id"
|
||||
} else if strings.HasSuffix(c.Name, ":oid") {
|
||||
c.Name = strings.TrimSuffix(c.Name, ":oid")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -358,6 +356,16 @@ func (p *Params) convertMultiColumns(whitelistNames map[string]bool) (bson.M, er
|
|||
return filter, nil
|
||||
}
|
||||
|
||||
func isObjectID(v interface{}) (primitive.ObjectID, bool) {
|
||||
if str, ok := v.(string); ok && len(str) == 24 {
|
||||
value, err := primitive.ObjectIDFromHex(str)
|
||||
if err == nil {
|
||||
return value, true
|
||||
}
|
||||
}
|
||||
return [12]byte{}, false
|
||||
}
|
||||
|
||||
func checkSameLogic(columns []Column) (int, [][]int, error) {
|
||||
orIndexes := []int{}
|
||||
l := len(columns)
|
||||
|
|
|
@ -440,8 +440,9 @@ func TestParams_ConvertToMongoFilter(t *testing.T) {
|
|||
wantErr: false,
|
||||
},
|
||||
|
||||
// --------------------------- object id ------------------------------
|
||||
{
|
||||
name: "convert to object id",
|
||||
name: "convert to object id 1",
|
||||
args: args{
|
||||
columns: []Column{
|
||||
{
|
||||
|
@ -458,6 +459,42 @@ func TestParams_ConvertToMongoFilter(t *testing.T) {
|
|||
wantErr: false,
|
||||
},
|
||||
|
||||
{
|
||||
name: "convert to object id 2",
|
||||
args: args{
|
||||
columns: []Column{
|
||||
{
|
||||
Name: "userId",
|
||||
Value: "65ce48483f11aff697e30d6d",
|
||||
},
|
||||
{
|
||||
Name: "orderID",
|
||||
Value: "65ce48483f11aff697e30d6d",
|
||||
},
|
||||
},
|
||||
},
|
||||
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}}}},
|
||||
wantErr: false,
|
||||
},
|
||||
|
||||
{
|
||||
name: "convert to object id 3",
|
||||
args: args{
|
||||
columns: []Column{
|
||||
{
|
||||
Name: "_id",
|
||||
Value: "65ce48483f11aff697e30d6d",
|
||||
},
|
||||
{
|
||||
Name: "my_order",
|
||||
Value: "65ce48483f11aff697e30d6d",
|
||||
},
|
||||
},
|
||||
},
|
||||
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}}}},
|
||||
wantErr: false,
|
||||
},
|
||||
|
||||
// ---------------------------- error ----------------------------------------------
|
||||
{
|
||||
name: "exp type err",
|
||||
|
|
Loading…
Reference in New Issue