github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/scan.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package dosa
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"reflect"
    27  
    28  	"github.com/golang/mock/gomock"
    29  )
    30  
    31  // ScanOp represents the scan query
    32  type ScanOp struct {
    33  	pager
    34  	object DomainObject
    35  }
    36  
    37  // NewScanOp returns a new ScanOp instance
    38  func NewScanOp(obj DomainObject) *ScanOp {
    39  	return &ScanOp{object: obj}
    40  }
    41  
    42  // Limit sets the number of rows returned per call. Default is 100
    43  func (s *ScanOp) Limit(n int) *ScanOp {
    44  	s.limit = n
    45  	return s
    46  }
    47  
    48  // Offset sets the pagination token. If not set, an empty token would be used.
    49  func (s *ScanOp) Offset(token string) *ScanOp {
    50  	s.token = token
    51  	return s
    52  }
    53  
    54  // Fields list the non-key fields users want to fetch.
    55  // PrimaryKey fields are always fetched.
    56  func (s *ScanOp) Fields(fields []string) *ScanOp {
    57  	s.fieldsToRead = fields
    58  	return s
    59  }
    60  
    61  // String satisfies the Stringer interface
    62  func (s *ScanOp) String() string {
    63  	result := &bytes.Buffer{}
    64  	result.WriteString("ScanOp")
    65  	addLimitTokenString(result, s.limit, s.token)
    66  	return result.String()
    67  }
    68  
    69  type scanOpMatcher struct {
    70  	p   pager
    71  	typ reflect.Type
    72  }
    73  
    74  // EqScanOp provides a gomock Matcher that matches any ScanOp with a limit,
    75  // token, and fields to read that are the same as those specificed by the op argument.
    76  func EqScanOp(op *ScanOp) gomock.Matcher {
    77  	return scanOpMatcher{
    78  		p:   op.pager,
    79  		typ: reflect.TypeOf(op.object).Elem(),
    80  	}
    81  }
    82  
    83  // Matches satisfies the gomock.Matcher interface
    84  func (m scanOpMatcher) Matches(x interface{}) bool {
    85  	op, ok := x.(*ScanOp)
    86  	if !ok {
    87  		return false
    88  	}
    89  
    90  	return m.p.equals(op.pager) && reflect.TypeOf(op.object).Elem() == m.typ
    91  }
    92  
    93  // String satisfies the gomock.Matcher and Stringer interface
    94  func (m scanOpMatcher) String() string {
    95  	return fmt.Sprintf(
    96  		" is equal to ScanOp with token %s, limit %d, fields %v, and entity type %v",
    97  		m.p.token,
    98  		m.p.limit,
    99  		m.p.fieldsToRead,
   100  		m.typ)
   101  }