github.com/ravendb/ravendb-go-client@v0.0.0-20240229102137-4474ee7aa0fa/compare_exchange_value.go (about)

     1  package ravendb
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  )
     7  
     8  // CompareExchangeValue represents value for compare exchange
     9  type CompareExchangeValue struct {
    10  	Key          string
    11  	Index        int64
    12  	Value        interface{}
    13  	ChangeVector *string
    14  	Metadata     *MetadataAsDictionary
    15  }
    16  
    17  type ICompareExchangeValue interface {
    18  	GetKey() string
    19  	GetIndex() int64
    20  	SetIndex(int64)
    21  	GetValue() interface{}
    22  	GetMetadata() *MetadataAsDictionary
    23  	HasMetadata() bool
    24  }
    25  
    26  func (cev *CompareExchangeValue) getPropertyFromValue(key string) interface{} {
    27  	if cev.Value == nil {
    28  		return nil
    29  	}
    30  
    31  	object, isMap := cev.Value.(map[string]interface{})
    32  	if isMap == false {
    33  		return nil
    34  	}
    35  
    36  	keyValue, exists := object[key]
    37  	if exists == false {
    38  		return nil
    39  	}
    40  
    41  	return keyValue
    42  }
    43  
    44  func (cev *CompareExchangeValue) hasChanged(other *CompareExchangeValue) (bool, error) {
    45  	if cev == other {
    46  		return false, nil
    47  	} // ptr equals
    48  
    49  	if strings.EqualFold(cev.GetKey(), other.GetKey()) == false {
    50  		return false, newIllegalArgumentError("Keys do not match. Expected: " + cev.Key + " but was " + other.Key)
    51  	}
    52  
    53  	if cev.Index != other.Index {
    54  		return true, nil
    55  	}
    56  
    57  	first, err := jsonMarshal(cev.Value)
    58  	if err != nil {
    59  		return false, err
    60  	}
    61  	second, err := jsonMarshal(other.Value)
    62  	if err != nil {
    63  		return false, err
    64  	}
    65  
    66  	return bytes.Equal(first, second) == false, nil //compare
    67  }
    68  
    69  func (cev *CompareExchangeValue) GetKey() string                     { return cev.Key }
    70  func (cev *CompareExchangeValue) GetIndex() int64                    { return cev.Index }
    71  func (cev *CompareExchangeValue) SetIndex(index int64)               { cev.Index = index }
    72  func (cev *CompareExchangeValue) GetValue() interface{}              { return cev.Value }
    73  func (cev *CompareExchangeValue) GetMetadata() *MetadataAsDictionary { return cev.Metadata }
    74  func (cev *CompareExchangeValue) HasMetadata() bool                  { return cev.Metadata != nil }
    75  
    76  // NewCompareExchangeValue returns new CompareExchangeValue
    77  func NewCompareExchangeValue(key string, index int64, value interface{}) *CompareExchangeValue {
    78  	return NewCompareExchangeValueBase(key, index, value, nil, nil)
    79  }
    80  
    81  func NewCompareExchangeValueBase(key string, index int64, value interface{}, changeVector *string, dictionary *MetadataAsDictionary) *CompareExchangeValue {
    82  	return &CompareExchangeValue{
    83  		Key:          key,
    84  		Index:        index,
    85  		Value:        value,
    86  		ChangeVector: changeVector,
    87  		Metadata:     dictionary,
    88  	}
    89  }