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

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  	"reflect"
     6  )
     7  
     8  var (
     9  	_ IOperation = &PutCompareExchangeValueOperation{}
    10  )
    11  
    12  type PutCompareExchangeValueOperation struct {
    13  	Command *PutCompareExchangeValueCommand
    14  
    15  	_key   string
    16  	_value interface{}
    17  	_index int64
    18  }
    19  
    20  func NewPutCompareExchangeValueOperation(key string, value interface{}, index int64) (*PutCompareExchangeValueOperation, error) {
    21  	if stringIsEmpty(key) {
    22  		return nil, newIllegalArgumentError("The key argument must have value")
    23  	}
    24  
    25  	if index < 0 {
    26  		return nil, newIllegalStateError("Index must be a non-negative number")
    27  	}
    28  
    29  	return &PutCompareExchangeValueOperation{
    30  		_key:   key,
    31  		_value: value,
    32  		_index: index,
    33  	}, nil
    34  }
    35  
    36  func (o *PutCompareExchangeValueOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) {
    37  	var err error
    38  	o.Command, err = NewPutCompareExchangeValueCommand(o._key, o._value, o._index, conventions)
    39  	return o.Command, err
    40  }
    41  
    42  var _ RavenCommand = &PutCompareExchangeValueCommand{}
    43  
    44  type PutCompareExchangeValueCommand struct {
    45  	RavenCommandBase
    46  
    47  	_key         string
    48  	_value       interface{}
    49  	_index       int64
    50  	_conventions *DocumentConventions
    51  
    52  	Result *CompareExchangeResult
    53  }
    54  
    55  func NewPutCompareExchangeValueCommand(key string, value interface{}, index int64, conventions *DocumentConventions) (*PutCompareExchangeValueCommand, error) {
    56  	if stringIsEmpty(key) {
    57  		return nil, newIllegalArgumentError("The key argument must have value")
    58  	}
    59  
    60  	if index < 0 {
    61  		return nil, newIllegalStateError("Index must be a non-negative number")
    62  	}
    63  	cmd := &PutCompareExchangeValueCommand{
    64  		RavenCommandBase: NewRavenCommandBase(),
    65  
    66  		_key:         key,
    67  		_value:       value,
    68  		_index:       index,
    69  		_conventions: conventions,
    70  	}
    71  	return cmd, nil
    72  }
    73  
    74  func (c *PutCompareExchangeValueCommand) createRequest(node *ServerNode) (*http.Request, error) {
    75  	url := node.URL + "/databases/" + node.Database + "/cmpxchg?key=" + c._key + "&index=" + i64toa(c._index)
    76  
    77  	m := map[string]interface{}{
    78  		"Object": c._value,
    79  	}
    80  	d, err := jsonMarshal(m)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	return newHttpPut(url, d)
    85  
    86  }
    87  
    88  func (c *PutCompareExchangeValueCommand) setResponse(response []byte, fromCache bool) error {
    89  	tp := reflect.TypeOf(c._value)
    90  	res, err := parseCompareExchangeResultFromString(tp, response, c._conventions)
    91  	if err != nil {
    92  		return err
    93  	}
    94  	c.Result = res
    95  	return nil
    96  }