...

Source file src/github.com/go-kivik/kivik/v4/mockdb/iter.go

Documentation: github.com/go-kivik/kivik/v4/mockdb

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package mockdb
    14  
    15  import (
    16  	"context"
    17  	"io"
    18  	"time"
    19  )
    20  
    21  type item struct {
    22  	delay time.Duration
    23  	item  interface{}
    24  }
    25  
    26  type iter struct {
    27  	items     []*item
    28  	closeErr  error
    29  	resultErr error
    30  }
    31  
    32  func (i *iter) Close() error { return i.closeErr }
    33  
    34  func (i *iter) push(item *item) {
    35  	i.items = append(i.items, item)
    36  }
    37  
    38  func (i *iter) unshift(ctx context.Context) (interface{}, error) {
    39  	if len(i.items) == 0 {
    40  		if i.resultErr != nil {
    41  			return nil, i.resultErr
    42  		}
    43  		return nil, io.EOF
    44  	}
    45  	var item *item
    46  	item, i.items = i.items[0], i.items[1:]
    47  	if item.delay == 0 {
    48  		return item.item, nil
    49  	}
    50  	if err := pause(ctx, item.delay); err != nil {
    51  		return nil, err
    52  	}
    53  	return i.unshift(ctx)
    54  }
    55  
    56  func (i *iter) count() int {
    57  	if len(i.items) == 0 {
    58  		return 0
    59  	}
    60  	var count int
    61  	for _, result := range i.items {
    62  		if result != nil && result.item != nil {
    63  			count++
    64  		}
    65  	}
    66  
    67  	return count
    68  }
    69  

View as plain text