good morning!!!!

Skip to content
Snippets Groups Projects
Commit b7bda721 authored by Maciej Lisiewski's avatar Maciej Lisiewski
Browse files

one fewer reflect

parent 463ad123
No related branches found
No related tags found
No related merge requests found
...@@ -277,9 +277,9 @@ func toNative(val interface{}) interface{} { ...@@ -277,9 +277,9 @@ func toNative(val interface{}) interface{} {
// 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{}) bson.ObjectId {
i := reflect.TypeOf(item) v := reflect.ValueOf(item)
switch i.Kind() { switch v.Kind() {
case reflect.Map: case reflect.Map:
if inItem, ok := item.(map[string]interface{}); ok { if inItem, ok := item.(map[string]interface{}); ok {
if id, ok := inItem["_id"]; ok { if id, ok := inItem["_id"]; ok {
...@@ -290,8 +290,9 @@ func getId(item interface{}) bson.ObjectId { ...@@ -290,8 +290,9 @@ func getId(item interface{}) bson.ObjectId {
} }
} }
case reflect.Struct: case reflect.Struct:
for n := 0; n < i.NumField(); n++ { t := v.Type()
field := i.Field(n) for n := 0; n < t.NumField(); n++ {
field := t.Field(n)
if field.PkgPath != "" { if field.PkgPath != "" {
continue // Private field continue // Private field
} }
...@@ -308,8 +309,7 @@ func getId(item interface{}) bson.ObjectId { ...@@ -308,8 +309,7 @@ func getId(item interface{}) bson.ObjectId {
parts := strings.Split(tag, ",") parts := strings.Split(tag, ",")
if parts[0] == "_id" { if parts[0] == "_id" {
vi := reflect.ValueOf(item) if bsonId, ok := v.FieldByName(field.Name).Interface().(bson.ObjectId); ok {
if bsonId, ok := vi.FieldByName(field.Name).Interface().(bson.ObjectId); ok {
return bsonId return bsonId
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment