good morning!!!!

Skip to content
Snippets Groups Projects
Commit df1d2682 authored by Paul Xue's avatar Paul Xue
Browse files

allow non objectId tpe for mongo _id

parent 9752fdcc
No related branches found
No related tags found
Loading
...@@ -240,9 +240,11 @@ func (col *Collection) Append(item interface{}) (interface{}, error) { ...@@ -240,9 +240,11 @@ func (col *Collection) Append(item interface{}) (interface{}, error) {
} }
// And other interfaces? // And other interfaces?
if setter, ok := item.(ObjectIdIDSetter); ok { if _, ok := id.(bson.ObjectId); ok {
if err := setter.SetID(id); err != nil { if setter, ok := item.(ObjectIdIDSetter); ok {
return nil, err if err := setter.SetID(id.(bson.ObjectId)); err != nil {
return nil, err
}
} }
} }
...@@ -260,7 +262,7 @@ func (col *Collection) Exists() bool { ...@@ -260,7 +262,7 @@ func (col *Collection) Exists() bool {
} }
// Fetches object _id or generates a new one if object doesn't have one or the one it has is invalid // Fetches object _id or generates a new one if object doesn't have one or the one it has is invalid
func getID(item interface{}) bson.ObjectId { func getID(item interface{}) interface{} {
v := reflect.ValueOf(item) v := reflect.ValueOf(item)
switch v.Kind() { switch v.Kind() {
...@@ -312,6 +314,8 @@ func getID(item interface{}) bson.ObjectId { ...@@ -312,6 +314,8 @@ func getID(item interface{}) bson.ObjectId {
if bsonID.Valid() { if bsonID.Valid() {
return bsonID return bsonID
} }
} else {
return v.FieldByName(fieldName).Interface()
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment