github.com/altipla-consulting/ravendb-go-client@v0.1.3/get_compare_exchange_values_operation.go (about)

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  	"reflect"
     6  	"strconv"
     7  )
     8  
     9  var (
    10  	_ IOperation = &GetCompareExchangeValuesOperation{}
    11  )
    12  
    13  type GetCompareExchangeValuesOperation struct {
    14  	Command *GetCompareExchangeValuesCommand
    15  
    16  	_clazz reflect.Type
    17  	_keys  []string
    18  
    19  	_startWith string
    20  	_start     int // -1 for unset
    21  	_pageSize  int
    22  }
    23  
    24  func NewGetCompareExchangeValuesOperationWithKeys(clazz reflect.Type, keys []string) (*GetCompareExchangeValuesOperation, error) {
    25  	if len(keys) == 0 {
    26  		return nil, newIllegalArgumentError("Keys cannot be null or empty array")
    27  	}
    28  	return &GetCompareExchangeValuesOperation{
    29  		_keys:  keys,
    30  		_clazz: clazz,
    31  
    32  		_start:     -1,
    33  		_pageSize:  0,
    34  		_startWith: "",
    35  	}, nil
    36  }
    37  
    38  func NewGetCompareExchangeValuesOperation(clazz reflect.Type, startWith string, start int, pageSize int) (*GetCompareExchangeValuesOperation, error) {
    39  	return &GetCompareExchangeValuesOperation{
    40  		_clazz: clazz,
    41  
    42  		_start:     start,
    43  		_pageSize:  pageSize,
    44  		_startWith: startWith,
    45  	}, nil
    46  }
    47  
    48  var _ RavenCommand = &GetCompareExchangeValuesCommand{}
    49  
    50  func (o *GetCompareExchangeValuesOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) {
    51  	var err error
    52  	o.Command, err = NewGetCompareExchangeValuesCommand(o, conventions)
    53  	return o.Command, err
    54  }
    55  
    56  type GetCompareExchangeValuesCommand struct {
    57  	RavenCommandBase
    58  
    59  	_operation   *GetCompareExchangeValuesOperation
    60  	_conventions *DocumentConventions
    61  	Result       map[string]*CompareExchangeValue
    62  }
    63  
    64  func NewGetCompareExchangeValuesCommand(operation *GetCompareExchangeValuesOperation, conventions *DocumentConventions) (*GetCompareExchangeValuesCommand, error) {
    65  	cmd := &GetCompareExchangeValuesCommand{
    66  		RavenCommandBase: NewRavenCommandBase(),
    67  
    68  		_operation:   operation,
    69  		_conventions: conventions,
    70  	}
    71  	cmd.IsReadRequest = true
    72  	return cmd, nil
    73  }
    74  
    75  func (c *GetCompareExchangeValuesCommand) createRequest(node *ServerNode) (*http.Request, error) {
    76  	url := node.URL + "/databases/" + node.Database + "/cmpxchg?"
    77  
    78  	if c._operation._keys != nil {
    79  		for _, key := range c._operation._keys {
    80  			url += "&key=" + urlUtilsEscapeDataString(key)
    81  		}
    82  	} else {
    83  		if !stringIsEmpty(c._operation._startWith) {
    84  			url += "&startsWith=" + urlUtilsEscapeDataString(c._operation._startWith)
    85  		}
    86  
    87  		if c._operation._start >= 0 {
    88  			url += "&start=" + strconv.Itoa(c._operation._start)
    89  		}
    90  
    91  		if c._operation._pageSize > 0 {
    92  			url += "&pageSize=" + strconv.Itoa(c._operation._pageSize)
    93  		}
    94  	}
    95  
    96  	return newHttpGet(url)
    97  }
    98  
    99  func (c *GetCompareExchangeValuesCommand) setResponse(response []byte, fromCache bool) error {
   100  	res, err := compareExchangeValueResultParserGetValues(c._operation._clazz, response, c._conventions)
   101  	if err != nil {
   102  		return err
   103  	}
   104  	c.Result = res
   105  	return nil
   106  }