Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing slice pointer as an interface pointer to Iter.All #181

Merged
merged 8 commits into from
Jun 5, 2018
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
_harness
.vscode
.vscode
12 changes: 10 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,6 @@ type Index struct {
// Collation allows users to specify language-specific rules for string comparison,
// such as rules for lettercase and accent marks.
type Collation struct {

// Locale defines the collation locale.
Locale string `bson:"locale"`

Expand Down Expand Up @@ -4408,10 +4407,19 @@ func (iter *Iter) Next(result interface{}) bool {
//
func (iter *Iter) All(result interface{}) error {
resultv := reflect.ValueOf(result)
if resultv.Kind() != reflect.Ptr || resultv.Elem().Kind() != reflect.Slice {
if resultv.Kind() != reflect.Ptr {
panic("result argument must be a slice address")
}

slicev := resultv.Elem()

if slicev.Kind() == reflect.Interface {
slicev = slicev.Elem()
}
if slicev.Kind() != reflect.Slice {
panic("result argument must be a slice address")
}

slicev = slicev.Slice(0, slicev.Cap())
elemt := slicev.Type().Elem()
i := 0
Expand Down
12 changes: 12 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ func (s *S) TestInsertFindAll(c *C) {
// Ensure result is backed by the originally allocated array
c.Assert(&result[0], Equals, &allocd[0])

// Re-run test destination as a pointer to interface{}
var resultInterface interface{}

anotherslice := make([]R, 5)
resultInterface = anotherslice
err = coll.Find(nil).Sort("a").All(&resultInterface)
c.Assert(err, IsNil)
assertResult()

// Ensure result is backed by the originally allocated array
c.Assert(&result[0], Equals, &allocd[0])

// Non-pointer slice error
f := func() { coll.Find(nil).All(result) }
c.Assert(f, Panics, "result argument must be a slice address")
Expand Down