Skip to content

Commit

Permalink
Don't panic on indexed int64 fields (#23)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
domodwyer authored Sep 11, 2017
1 parent 1f4c10f commit 934a190
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -36,6 +37,7 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
* @feliixx
* @fmpwizard
* @jameinel
* @mapete94
* @Reenjii
* @smoya
* @wgallagher
21 changes: 15 additions & 6 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions session_internal_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 934a190

Please sign in to comment.