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

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  	"reflect"
     6  )
     7  
     8  var (
     9  	_ IOperation = &PatchOperation{}
    10  )
    11  
    12  // PatchOperationPayload represents payload of patch operation
    13  // Note: in Java it's Payload nested in PatchOperation
    14  type PatchOperationPayload struct {
    15  	patch          *PatchRequest
    16  	patchIfMissing *PatchRequest
    17  }
    18  
    19  // PatchOperationResult represents result of patch operation
    20  // Note: in Java it's Result nested in PatchOperation
    21  type PatchOperationResult struct {
    22  	Status   PatchStatus            `json:"Status"`
    23  	Document map[string]interface{} `json:"Document"`
    24  }
    25  
    26  func (r *PatchOperationResult) GetResult(result interface{}) error {
    27  	entityType := reflect.TypeOf(result)
    28  	entity, err := makeStructFromJSONMap(entityType, r.Document)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	return setInterfaceToValue(result, entity)
    33  }
    34  
    35  // PatchOperation represents patch operation
    36  type PatchOperation struct {
    37  	Command *PatchCommand
    38  
    39  	id                              string
    40  	changeVector                    *string
    41  	patch                           *PatchRequest
    42  	patchIfMissing                  *PatchRequest
    43  	skipPatchIfChangeVectorMismatch bool
    44  }
    45  
    46  // NewPatchOperation returns new PatchOperation
    47  func NewPatchOperation(id string, changeVector *string, patch *PatchRequest, patchIfMissing *PatchRequest, skipPatchIfChangeVectorMismatch bool) (*PatchOperation, error) {
    48  	if patch == nil {
    49  		return nil, newIllegalArgumentError("Patch cannot be null")
    50  	}
    51  
    52  	if stringIsBlank(patch.Script) {
    53  		return nil, newIllegalArgumentError("Patch script cannot be null")
    54  	}
    55  
    56  	if patchIfMissing != nil && stringIsBlank(patchIfMissing.Script) {
    57  		return nil, newIllegalArgumentError("PatchIfMissing script cannot be null")
    58  	}
    59  	return &PatchOperation{
    60  		id:                              id,
    61  		changeVector:                    changeVector,
    62  		patch:                           patch,
    63  		patchIfMissing:                  patchIfMissing,
    64  		skipPatchIfChangeVectorMismatch: skipPatchIfChangeVectorMismatch,
    65  	}, nil
    66  }
    67  
    68  func (o *PatchOperation) GetCommand(store *DocumentStore, conventions *DocumentConventions, cache *httpCache) (RavenCommand, error) {
    69  	var err error
    70  	o.Command, err = NewPatchCommand(conventions, o.id, o.changeVector, o.patch, o.patchIfMissing, o.skipPatchIfChangeVectorMismatch, false, false)
    71  	return o.Command, err
    72  }
    73  
    74  var _ RavenCommand = &PatchCommand{}
    75  
    76  // PatchCommand represents patch command
    77  type PatchCommand struct {
    78  	RavenCommandBase
    79  
    80  	// TODO: unused
    81  	//conventions                     *DocumentConventions
    82  
    83  	id                              string
    84  	changeVector                    *string
    85  	patch                           *PatchOperationPayload
    86  	skipPatchIfChangeVectorMismatch bool
    87  	returnDebugInformation          bool
    88  	test                            bool
    89  
    90  	Result *PatchResult
    91  }
    92  
    93  // NewPatchCommand returns new PatchCommand
    94  func NewPatchCommand(conventions *DocumentConventions, id string, changeVector *string,
    95  	patch *PatchRequest, patchIfMissing *PatchRequest, skipPatchIfChangeVectorMismatch bool,
    96  	returnDebugInformation bool, test bool) (*PatchCommand, error) {
    97  
    98  	/* TODO: used only for json mapper, not used in Go
    99  	if conventions == nil {
   100  		return nil, newIllegalArgumentError("Conventions cannot be null")
   101  	}
   102  	*/
   103  
   104  	if patch == nil {
   105  		return nil, newIllegalArgumentError("Patch cannot be null")
   106  	}
   107  
   108  	if stringIsBlank(patch.Script) {
   109  		return nil, newIllegalArgumentError("Patch script cannot be null")
   110  	}
   111  
   112  	if patchIfMissing != nil && stringIsBlank(patchIfMissing.Script) {
   113  		return nil, newIllegalArgumentError("PatchIfMissing script cannot be null")
   114  	}
   115  
   116  	if id == "" {
   117  		return nil, newIllegalArgumentError("Id cannot be null")
   118  	}
   119  
   120  	payload := &PatchOperationPayload{
   121  		patch:          patch,
   122  		patchIfMissing: patchIfMissing,
   123  	}
   124  	cmd := &PatchCommand{
   125  		RavenCommandBase: NewRavenCommandBase(),
   126  
   127  		id:                              id,
   128  		changeVector:                    changeVector,
   129  		patch:                           payload,
   130  		skipPatchIfChangeVectorMismatch: skipPatchIfChangeVectorMismatch,
   131  		returnDebugInformation:          returnDebugInformation,
   132  		test:                            test,
   133  	}
   134  
   135  	return cmd, nil
   136  }
   137  
   138  func (c *PatchCommand) createRequest(node *ServerNode) (*http.Request, error) {
   139  	url := node.URL + "/databases/" + node.Database + "/docs?id=" + urlUtilsEscapeDataString(c.id)
   140  
   141  	if c.skipPatchIfChangeVectorMismatch {
   142  		url += "&skipPatchIfChangeVectorMismatch=true"
   143  	}
   144  
   145  	if c.returnDebugInformation {
   146  		url += "&debug=true"
   147  	}
   148  
   149  	if c.test {
   150  		url += "&test=true"
   151  	}
   152  
   153  	patch := map[string]interface{}{}
   154  	if c.patch.patch != nil {
   155  		patch = c.patch.patch.Serialize()
   156  	}
   157  
   158  	var patchIfMissing map[string]interface{}
   159  	if c.patch.patchIfMissing != nil {
   160  		patchIfMissing = c.patch.patchIfMissing.Serialize()
   161  	}
   162  
   163  	m := map[string]interface{}{
   164  		"Patch":          patch,
   165  		"PatchIfMissing": patchIfMissing,
   166  	}
   167  	d, err := jsonMarshal(m)
   168  	panicIf(err != nil, "jsonMarshal failed with %s", err)
   169  
   170  	request, err := newHttpPatch(url, d)
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	addChangeVectorIfNotNull(c.changeVector, request)
   175  	return request, nil
   176  }
   177  
   178  func (c *PatchCommand) setResponse(response []byte, fromCache bool) error {
   179  	if len(response) == 0 {
   180  		return nil
   181  	}
   182  
   183  	return jsonUnmarshal(response, &c.Result)
   184  }