Skip to content

Commit

Permalink
Merge pull request #811 from go-kivik/ready
Browse files Browse the repository at this point in the history
Minor refactoring re ready iterators
  • Loading branch information
flimzy authored Oct 10, 2023
2 parents 89094ff + 6e5d12f commit 6d7ff8b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 57 deletions.
31 changes: 0 additions & 31 deletions bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,6 @@ import (
"github.com/go-kivik/kivik/v4/internal/mock"
)

func Test_iter_isReady(t *testing.T) {
tests := []struct {
name string
iter *iter
err string
}{
{
name: "not ready",
iter: &iter{},
err: "kivik: Iterator access before calling Next",
},
{
name: "closed",
iter: &iter{state: stateClosed},
err: "kivik: Iterator is closed",
},
{
name: "success",
iter: &iter{state: stateRowReady},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.iter.isReady()
if !testy.ErrorMatches(test.err, err) {
t.Errorf("Unexpected error: %s", err)
}
})
}
}

func TestDocsInterfaceSlice(t *testing.T) {
type diTest struct {
name string
Expand Down
26 changes: 0 additions & 26 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,6 @@ func stateIsReady(state int) bool {
return false
}

// makeReady ensures that the iterator is ready to be read from. If i.err is
// set, it is returned. In the case that [iter.Next] has not been called, the
// returned unlock function will also close the iterator, and set e if
// [iter.Close] errors and e != nil.
func (i *iter) makeReady(e *error) (unlock func(), err error) {
i.mu.Lock()
if i.err != nil {
i.mu.Unlock()
return nil, i.err
}
if !stateIsReady(i.state) {
i.mu.Unlock()
if !i.Next() {
return nil, &internal.Error{Status: http.StatusNotFound, Message: "no results"}
}
return func() {
i.mu.Lock()
if err := i.close(nil); err != nil && e != nil {
*e = err
}
i.mu.Unlock()
}, nil
}
return i.mu.Unlock, nil
}

// newIterator instantiates a new iterator.
//
// ctx is a possibly-cancellable context. zeroValue is an empty instance of
Expand Down
31 changes: 31 additions & 0 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,34 @@ func TestCancelledIterator(t *testing.T) {
t.Errorf("Unexpected error: %s", err)
}
}

func Test_iter_isReady(t *testing.T) {
tests := []struct {
name string
iter *iter
err string
}{
{
name: "not ready",
iter: &iter{},
err: "kivik: Iterator access before calling Next",
},
{
name: "closed",
iter: &iter{state: stateClosed},
err: "kivik: Iterator is closed",
},
{
name: "success",
iter: &iter{state: stateRowReady},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.iter.isReady()
if !testy.ErrorMatches(test.err, err) {
t.Errorf("Unexpected error: %s", err)
}
})
}
}
24 changes: 24 additions & 0 deletions resultset.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,30 @@ func (r *ResultSet) Attachments() (*AttachmentsIterator, error) {
return &AttachmentsIterator{atti: row.Attachments}, nil
}

// makeReady ensures that the iterator is ready to be read from. If i.err is
// set, it is returned. In the case that [iter.Next] has not been called, the
// returned unlock function will also close the iterator, and set e if
// [iter.Close] errors and e != nil.
func (r *ResultSet) makeReady(e *error) (unlock func(), err error) {
r.mu.Lock()
if r.err != nil {
r.mu.Unlock()
return nil, r.err
}
if !stateIsReady(r.state) {
r.mu.Unlock()
if !r.Next() {
return nil, &internal.Error{Status: http.StatusNotFound, Message: "no results"}
}
return func() {
if err := r.Close(); err != nil && e != nil {
*e = err
}
}, nil
}
return r.mu.Unlock, nil
}

type rowsIterator struct {
driver.Rows
*ResultMetadata
Expand Down

0 comments on commit 6d7ff8b

Please sign in to comment.