Skip to content

Commit 7c69abf

Browse files
author
Robert Zaremba
committed
cursor.One should work without checking IsNil on a user side.
`cursor.One` is a higher level funcion to get a single element from the result set. It already returns `ErrEmptyResult`, so to be consistent with a function responsibility it should also return that error even when user didn't check `IsNil` before performing `One`.
1 parent 7e2df5c commit 7c69abf

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

query_control_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (s *RethinkSuite) TestControlExecNil(c *test.C) {
1515

1616
err = res.One(&response)
1717

18-
c.Assert(err, test.IsNil)
18+
c.Assert(err, test.Equals, ErrEmptyResult)
1919
c.Assert(response, test.Equals, nil)
2020
}
2121

results.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ func (c *Cursor) All(result interface{}) error {
199199
// One retrieves a single document from the result set into the provided
200200
// slice and closes the cursor.
201201
func (c *Cursor) One(result interface{}) error {
202+
if c.IsNil() {
203+
return ErrEmptyResult
204+
}
202205
ok := c.Next(result)
203206
if !ok {
204207
err := c.Err()

results_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ func (s *RethinkSuite) TestEmptyResults(c *test.C) {
163163
res, err = Db("test").Table("test").Get("missing value").Run(sess)
164164
c.Assert(err, test.IsNil)
165165
var response interface{}
166-
res.One(&response)
166+
err = res.One(&response)
167+
c.Assert(err, test.Equals, ErrEmptyResult)
167168
c.Assert(res.IsNil(), test.Equals, true)
168169

169170
res, err = Db("test").Table("test").Get("missing value").Run(sess)

0 commit comments

Comments
 (0)