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