Skip to content

Commit 934a190

Browse files
authored
Don't panic on indexed int64 fields (#23)
* Stop all db instances after tests (go-mgo#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: * #23 * https://github.com/go-mgo/mgo/issues/475 * go-mgo#476
1 parent 1f4c10f commit 934a190

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
2121
* 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))
2222
* Fixes cursor timeouts ([details](https://jira.mongodb.org/browse/SERVER-24899))
2323
* Support index hints and timeouts for count queries ([details](https://github.com/globalsign/mgo/pull/17))
24-
* Allow dropping all indexes on a collection ([details](https://github.com/globalsign/mgo/pull/25))
24+
* Don't panic when handling indexed `int64` fields ([detials](https://github.com/go-mgo/mgo/issues/475))
25+
* Supports dropping all indexes on a collection ([details](https://github.com/globalsign/mgo/pull/25))
2526
* Annotates log entries/profiler output with optional appName on 3.4+ ([details](https://github.com/globalsign/mgo/pull/28))
2627
* Support for read-only [views](https://docs.mongodb.com/manual/core/views/) in 3.4+ ([details](https://github.com/globalsign/mgo/pull/33))
2728

@@ -36,6 +37,7 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
3637
* @feliixx
3738
* @fmpwizard
3839
* @jameinel
40+
* @mapete94
3941
* @Reenjii
4042
* @smoya
4143
* @wgallagher

session.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,12 +1659,25 @@ func (idxs indexSlice) Swap(i, j int) { idxs[i], idxs[j] = idxs[j], idxs[i]
16591659

16601660
func simpleIndexKey(realKey bson.D) (key []string) {
16611661
for i := range realKey {
1662+
var vi int
16621663
field := realKey[i].Name
1663-
vi, ok := realKey[i].Value.(int)
1664-
if !ok {
1664+
1665+
switch realKey[i].Value.(type) {
1666+
case int64:
1667+
vf, _ := realKey[i].Value.(int64)
1668+
vi = int(vf)
1669+
case float64:
16651670
vf, _ := realKey[i].Value.(float64)
16661671
vi = int(vf)
1672+
case string:
1673+
if vs, ok := realKey[i].Value.(string); ok {
1674+
key = append(key, "$"+vs+":"+field)
1675+
continue
1676+
}
1677+
case int:
1678+
vi = realKey[i].Value.(int)
16671679
}
1680+
16681681
if vi == 1 {
16691682
key = append(key, field)
16701683
continue
@@ -1673,10 +1686,6 @@ func simpleIndexKey(realKey bson.D) (key []string) {
16731686
key = append(key, "-"+field)
16741687
continue
16751688
}
1676-
if vs, ok := realKey[i].Value.(string); ok {
1677-
key = append(key, "$"+vs+":"+field)
1678-
continue
1679-
}
16801689
panic("Got unknown index key type for field " + field)
16811690
}
16821691
return

session_internal_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package mgo
2+
3+
import (
4+
"testing"
5+
6+
"github.com/globalsign/mgo/bson"
7+
)
8+
9+
// This file is for testing functions that are not exported outside the mgo
10+
// package - avoid doing so if at all possible.
11+
12+
// Ensures indexed int64 fields do not cause mgo to panic.
13+
//
14+
// See https://github.com/globalsign/mgo/pull/23
15+
func TestIndexedInt64FieldsBug(t *testing.T) {
16+
input := bson.D{
17+
{Name: "testkey", Value: int(1)},
18+
{Name: "testkey", Value: int64(1)},
19+
{Name: "testkey", Value: "test"},
20+
{Name: "testkey", Value: float64(1)},
21+
}
22+
23+
_ = simpleIndexKey(input)
24+
}

0 commit comments

Comments
 (0)