github.com/go-kivik/kivik/v4@v4.3.2/mockdb/changes.go (about)

     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  	"time"
    18  
    19  	"github.com/go-kivik/kivik/v4/driver"
    20  )
    21  
    22  // Changes is a mocked collection of Changes results.
    23  type Changes struct {
    24  	iter
    25  	lastSeq string
    26  	pending int64
    27  	etag    string
    28  }
    29  
    30  type driverChanges struct {
    31  	context.Context
    32  	*Changes
    33  }
    34  
    35  func coalesceChanges(changes *Changes) *Changes {
    36  	if changes != nil {
    37  		return changes
    38  	}
    39  	return &Changes{}
    40  }
    41  
    42  var _ driver.Changes = &driverChanges{}
    43  
    44  func (r *driverChanges) Next(res *driver.Change) error {
    45  	result, err := r.unshift(r.Context)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	*res = *result.(*driver.Change)
    50  	return nil
    51  }
    52  
    53  func (r *driverChanges) LastSeq() string { return r.lastSeq }
    54  func (r *driverChanges) Pending() int64  { return r.pending }
    55  func (r *driverChanges) ETag() string    { return r.etag }
    56  
    57  // CloseError sets an error to be returned when the iterator is closed.
    58  func (r *Changes) CloseError(err error) *Changes {
    59  	r.closeErr = err
    60  	return r
    61  }
    62  
    63  // LastSeq sets the last_seq value to be returned by the changes iterator.
    64  func (r *Changes) LastSeq(seq string) *Changes {
    65  	r.lastSeq = seq
    66  	return r
    67  }
    68  
    69  // Pending sets the pending value to be returned by the changes iterator.
    70  func (r *Changes) Pending(pending int64) *Changes {
    71  	r.pending = pending
    72  	return r
    73  }
    74  
    75  // ETag sets the etag value to be returned by the changes iterator.
    76  func (r *Changes) ETag(etag string) *Changes {
    77  	r.etag = etag
    78  	return r
    79  }
    80  
    81  // AddChange adds a change result to be returned by the iterator. If
    82  // AddResultError has been set, this method will panic.
    83  func (r *Changes) AddChange(change *driver.Change) *Changes {
    84  	if r.resultErr != nil {
    85  		panic("It is invalid to set more changes after AddChangeError is defined.")
    86  	}
    87  	r.push(&item{item: change})
    88  	return r
    89  }
    90  
    91  // AddChangeError adds an error to be returned during iteration.
    92  func (r *Changes) AddChangeError(err error) *Changes {
    93  	r.resultErr = err
    94  	return r
    95  }
    96  
    97  // AddDelay adds a delay before the next iteration will complete.
    98  func (r *Changes) AddDelay(delay time.Duration) *Changes {
    99  	r.push(&item{delay: delay})
   100  	return r
   101  }
   102  
   103  // Final converts the Changes object to a driver.Changes. This method is
   104  // intended for use within WillExecute() to return results.
   105  func (r *Changes) Final() driver.Changes {
   106  	return &driverChanges{Changes: r}
   107  }