diff --git a/mongo/collection.go b/mongo/collection.go index 99e3b711ae3b7481d88082e922a6baaa12c4a46d..fdd7685e7d6f3750d0d87c7694d5f8368bacc3d8 100644 --- a/mongo/collection.go +++ b/mongo/collection.go @@ -263,7 +263,8 @@ 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 func getID(item interface{}) interface{} { - v := reflect.ValueOf(item) + v := reflect.ValueOf(item) // convert interface to Value + v = reflect.Indirect(v) // convert pointers switch v.Kind() { case reflect.Map: diff --git a/mongo/database_test.go b/mongo/database_test.go index 57336d667003f489f28c00c5a30e21a9fbe3cb76..7144cee00110edd7be0a837ac8386c0d7713d4d1 100644 --- a/mongo/database_test.go +++ b/mongo/database_test.go @@ -365,6 +365,42 @@ func TestAppend(t *testing.T) { t.Fatalf("Expecting a valid bson.ObjectId.") } + // Appending a pointer to a struct + id, err = artist.Append(&struct { + ArtistName string `bson:"name"` + }{ + "Metallica", + }) + + if id == nil { + t.Fatalf("Expecting an ID.") + } + + if _, ok := id.(bson.ObjectId); ok != true { + t.Fatalf("Expecting a bson.ObjectId.") + } + + if id.(bson.ObjectId).Valid() != true { + t.Fatalf("Expecting a valid bson.ObjectId.") + } + + // Appending a pointer to a map + id, err = artist.Append(&map[string]string{ + "name": "Freddie", + }) + + if id == nil { + t.Fatalf("Expecting an ID.") + } + + if _, ok := id.(bson.ObjectId); ok != true { + t.Fatalf("Expecting a bson.ObjectId.") + } + + if id.(bson.ObjectId).Valid() != true { + t.Fatalf("Expecting a valid bson.ObjectId.") + } + // Attempt to append and update a private key itemStruct3 := artistWithObjectIdKey{ Name: "Janus", @@ -380,13 +416,13 @@ func TestAppend(t *testing.T) { var total uint64 - // Counting elements, must be exactly 4 elements. + // Counting elements, must be exactly 6 elements. if total, err = artist.Find().Count(); err != nil { t.Fatal(err) } - if total != 4 { - t.Fatalf("Expecting exactly 4 rows.") + if total != 6 { + t.Fatalf("Expecting exactly 6 rows.") } }