From 934a190f5f79d5923a85a3816fe80d2a085393b9 Mon Sep 17 00:00:00 2001 From: Dom Date: Mon, 11 Sep 2017 19:52:51 +0100 Subject: [PATCH] Don't panic on indexed int64 fields (#23) * Stop all db instances after tests (#462) If all tests pass, the builds for mongo earlier than 2.6 are still failing. Running a clean up fixes the issue. * fixing int64 type failing when getting indexes and trying to type them * requested changes relating to case statement and panic * Update README.md to credit @mapete94. * tests: ensure indexed int64 fields do not cause a panic in Indexes() See: * https://github.com/globalsign/mgo/pull/23 * https://github.com/go-mgo/mgo/issues/475 * https://github.com/go-mgo/mgo/pull/476 --- README.md | 4 +++- session.go | 21 +++++++++++++++------ session_internal_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 session_internal_test.go diff --git a/README.md b/README.md index a319b1ab4..349aaee43 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili * Improved multi-document transaction performance ([details](https://github.com/globalsign/mgo/pull/10), [more](https://github.com/globalsign/mgo/pull/11), [more](https://github.com/globalsign/mgo/pull/16)) * Fixes cursor timeouts ([details](https://jira.mongodb.org/browse/SERVER-24899)) * Support index hints and timeouts for count queries ([details](https://github.com/globalsign/mgo/pull/17)) -* Allow dropping all indexes on a collection ([details](https://github.com/globalsign/mgo/pull/25)) +* Don't panic when handling indexed `int64` fields ([detials](https://github.com/go-mgo/mgo/issues/475)) +* Supports dropping all indexes on a collection ([details](https://github.com/globalsign/mgo/pull/25)) * Annotates log entries/profiler output with optional appName on 3.4+ ([details](https://github.com/globalsign/mgo/pull/28)) * Support for read-only [views](https://docs.mongodb.com/manual/core/views/) in 3.4+ ([details](https://github.com/globalsign/mgo/pull/33)) @@ -36,6 +37,7 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili * @feliixx * @fmpwizard * @jameinel +* @mapete94 * @Reenjii * @smoya * @wgallagher \ No newline at end of file diff --git a/session.go b/session.go index b1bbcdbf7..5e7c99775 100644 --- a/session.go +++ b/session.go @@ -1659,12 +1659,25 @@ func (idxs indexSlice) Swap(i, j int) { idxs[i], idxs[j] = idxs[j], idxs[i] func simpleIndexKey(realKey bson.D) (key []string) { for i := range realKey { + var vi int field := realKey[i].Name - vi, ok := realKey[i].Value.(int) - if !ok { + + switch realKey[i].Value.(type) { + case int64: + vf, _ := realKey[i].Value.(int64) + vi = int(vf) + case float64: vf, _ := realKey[i].Value.(float64) vi = int(vf) + case string: + if vs, ok := realKey[i].Value.(string); ok { + key = append(key, "$"+vs+":"+field) + continue + } + case int: + vi = realKey[i].Value.(int) } + if vi == 1 { key = append(key, field) continue @@ -1673,10 +1686,6 @@ func simpleIndexKey(realKey bson.D) (key []string) { key = append(key, "-"+field) continue } - if vs, ok := realKey[i].Value.(string); ok { - key = append(key, "$"+vs+":"+field) - continue - } panic("Got unknown index key type for field " + field) } return diff --git a/session_internal_test.go b/session_internal_test.go new file mode 100644 index 000000000..f5f796c99 --- /dev/null +++ b/session_internal_test.go @@ -0,0 +1,24 @@ +package mgo + +import ( + "testing" + + "github.com/globalsign/mgo/bson" +) + +// This file is for testing functions that are not exported outside the mgo +// package - avoid doing so if at all possible. + +// Ensures indexed int64 fields do not cause mgo to panic. +// +// See https://github.com/globalsign/mgo/pull/23 +func TestIndexedInt64FieldsBug(t *testing.T) { + input := bson.D{ + {Name: "testkey", Value: int(1)}, + {Name: "testkey", Value: int64(1)}, + {Name: "testkey", Value: "test"}, + {Name: "testkey", Value: float64(1)}, + } + + _ = simpleIndexKey(input) +}