/
method_item_patch.go
119 lines (110 loc) · 3.42 KB
/
method_item_patch.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
package rest
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"github.com/clarify/rested/resource"
"github.com/clarify/rested/schema/query"
jsonpatch "github.com/evanphx/json-patch"
)
func isJSONPatch(r *http.Request) bool {
if ct := r.Header.Get("Content-Type"); ct != "" && strings.TrimSpace(strings.SplitN(ct, ";", 2)[0]) == "application/json-patch+json" {
return true
}
return false
}
// itemPatch handles PATCH requests on an item URL.
//
// Reference: http://tools.ietf.org/html/rfc5789, http://tools.ietf.org/html/rfc6902
func itemPatch(ctx context.Context, r *http.Request, route *RouteMatch) (status int, headers http.Header, body interface{}) {
var payload map[string]interface{}
var patchJSON []byte
isJSONPatch := isJSONPatch(r)
if isJSONPatch {
if r.Body != nil {
patchJSON, _ = ioutil.ReadAll(r.Body)
r.Body.Close()
}
} else {
if e := decodePayload(r, &payload); e != nil {
return e.Code, nil, e
}
}
q, e := route.Query()
if e != nil {
return e.Code, nil, e
}
// Get original item if any.
rsrc := route.Resource()
var original *resource.Item
q.Window = &query.Window{Limit: 1}
if l, err := rsrc.Find(ctx, q); err != nil {
// If item can't be fetch, return an error.
e = NewError(err)
return e.Code, nil, e
} else if len(l.Items) == 0 {
return ErrNotFound.Code, nil, ErrNotFound
} else {
original = l.Items[0]
}
// If-Match / If-Unmodified-Since handling.
if err := checkIntegrityRequest(r, original); err != nil {
return err.Code, nil, err
}
if isJSONPatch {
// Recreate the new document
originalJSON, err := json.Marshal(original.Payload)
if err != nil {
return 422, nil, &Error{422, err.Error(), nil}
}
patch, err := jsonpatch.DecodePatch(patchJSON)
if err != nil {
return 400, nil, &Error{400, "Malformed patch document: " + err.Error(), nil}
}
payloadJSON, err := patch.Apply(originalJSON)
if err != nil {
return 422, nil, &Error{422, err.Error(), nil}
}
err = json.Unmarshal(payloadJSON, &payload)
if err != nil {
return 422, nil, &Error{422, err.Error(), nil}
}
}
// If JSON-Patch then `replace=true`, because we can delete fields
changes, base := rsrc.Validator().Prepare(ctx, payload, &original.Payload, isJSONPatch)
// Append lookup fields to base payload so it isn't caught by ReadOnly
// (i.e.: contains id and parent resource refs if any).
for k, v := range route.ResourcePath.Values() {
base[k] = v
}
doc, errs := rsrc.Validator().Validate(changes, base)
if len(errs) > 0 {
return 422, nil, &Error{422, "Document contains error(s)", errs}
}
if id, found := doc["id"]; found && id != original.ID {
return 422, nil, &Error{422, "Cannot change document ID", nil}
}
item, err := resource.NewItem(doc)
if err != nil {
e = NewError(err)
return e.Code, nil, e
}
// Store the modified document by providing the original doc to instruct
// handler to ensure the stored document didn't change between in the
// interval. An ErrPreconditionFailed will be thrown in case of race
// condition (i.e.: another thread modified the document between the Find()
// and the Store()).
if err = rsrc.Update(ctx, item, original); err != nil {
e = NewError(err)
return e.Code, nil, e
}
// Evaluate projection so response gets the same format as read requests.
item.Payload, err = q.Projection.Eval(ctx, item.Payload, restResource{rsrc})
if err != nil {
e = NewError(err)
return e.Code, nil, e
}
return 200, nil, item
}