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

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  	"reflect"
     6  )
     7  
     8  var (
     9  	_ IOperation = &GetCompareExchangeValueOperation{}
    10  )
    11  
    12  type GetCompareExchangeValueOperation struct {
    13  	Command *GetCompareExchangeValueCommand
    14  
    15  	_key   string
    16  	_clazz reflect.Type
    17  }
    18  
    19  func NewGetCompareExchangeValueOperation(clazz reflect.Type, key string) (*GetCompareExchangeValueOperation, error) {
    20  	if stringIsEmpty(key) {
    21  		return nil, newIllegalArgumentError("The key argument must have value")
    22  	}
    23  
    24  	return &GetCompareExchangeValueOperation{
    25  		_clazz: clazz,
    26  		_key:   key,
    27  	}, nil
    28  }
    29  
    30  func (o *GetCompareExchangeValueOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) {
    31  	var err error
    32  	o.Command, err = NewGetCompareExchangeValueCommand(o._clazz, o._key, conventions)
    33  	return o.Command, err
    34  }
    35  
    36  var _ RavenCommand = &GetCompareExchangeValueCommand{}
    37  
    38  type GetCompareExchangeValueCommand struct {
    39  	RavenCommandBase
    40  
    41  	_key         string
    42  	_clazz       reflect.Type
    43  	_conventions *DocumentConventions
    44  
    45  	Result *CompareExchangeValue
    46  }
    47  
    48  func NewGetCompareExchangeValueCommand(clazz reflect.Type, key string, conventions *DocumentConventions) (*GetCompareExchangeValueCommand, error) {
    49  	if stringIsEmpty(key) {
    50  		return nil, newIllegalArgumentError("The key argument must have value")
    51  	}
    52  
    53  	cmd := &GetCompareExchangeValueCommand{
    54  		RavenCommandBase: NewRavenCommandBase(),
    55  
    56  		_clazz:       clazz,
    57  		_key:         key,
    58  		_conventions: conventions,
    59  	}
    60  	cmd.IsReadRequest = true
    61  	return cmd, nil
    62  }
    63  
    64  func (c *GetCompareExchangeValueCommand) createRequest(node *ServerNode) (*http.Request, error) {
    65  	url := node.URL + "/databases/" + node.Database + "/cmpxchg?key=" + urlEncode(c._key)
    66  	return newHttpGet(url)
    67  
    68  }
    69  
    70  func (c *GetCompareExchangeValueCommand) setResponse(response []byte, fromCache bool) error {
    71  	res, err := compareExchangeValueResultParserGetValue(c._clazz, response, c._conventions)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	c.Result = res
    76  	return nil
    77  }