/
patch.go
152 lines (139 loc) · 3.82 KB
/
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
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
package jsonpatch
// jsonpatch is a library for creating and applying JSON Patches as defined in RFC 6902.
//
// A JSON patch is a list of operations in the form of:
// [
// {"op":"test","path":"/foo","value":"bar"},
// {"op":"replace","path":"/foo","value":"baz"}
// ...
// ]
//
// See http://tools.ietf.org/html/rfc6902 for more information.
import (
"encoding/json"
"fmt"
"github.com/VictorLowther/jsonpatch/utils"
)
// operation represents a valid JSON Patch operation as defined by RFC 6902
type operation struct {
// Op can be one of:
// * "add"
// * "remove"
// * "replace"
// * "move"
// * "copy"
// * "test"
// All Operations must have an Op.
Op string `json:"op"`
// Path is a JSON Pointer as defined in RFC 6901
// All Operations must have a Path
Path pointer `json:"path"`
// From is a JSON pointer indicating where a value should be
// copied/moved from. From is only used by copy and move operations.
From pointer `json:"from"`
// Value is the Value to be used for add, replace, and test operations.
Value interface{} `json:"value"`
}
func (o *operation) MarshalJSON() ([]byte, error) {
res := map[string]interface{}{}
res["op"] = o.Op
res["path"] = o.Path
switch o.Op {
case "copy", "move":
res["from"] = o.From
case "add", "replace", "test":
res["value"] = o.Value
}
return json.Marshal(res)
}
const ContentType = "application/json-patch+json"
// Apply performs a single patch operation
func (o *operation) Apply(to interface{}) (interface{}, error) {
switch o.Op {
case "test":
return to, o.Path.Test(to, o.Value)
case "replace":
return o.Path.Replace(to, o.Value)
case "add":
return o.Path.Put(to, o.Value)
case "remove":
return o.Path.Remove(to)
case "move":
return o.From.Move(to, o.Path)
case "copy":
return o.From.Copy(to, o.Path)
default:
return to, fmt.Errorf("Invalid op %v", o.Op)
}
}
// Patch is an array of individual JSON Patch operations.
type patch []operation
// NewPatch takes a byte array and tries to unmarshal it.
func newPatch(buf []byte) (res patch, err error) {
res = make(patch, 0)
if err = json.Unmarshal(buf, &res); err != nil {
return nil, err
}
for _, op := range res {
if op.Path == nil {
return res, fmt.Errorf("Did not get valid path")
}
switch op.Op {
case "test":
fallthrough
case "replace":
fallthrough
case "add":
if op.Value == nil {
return res, fmt.Errorf("%v must have a valid value", op.Op)
}
case "move":
fallthrough
case "copy":
if op.From == nil {
return res, fmt.Errorf("%v must have a from", op.Op)
}
case "remove":
continue
default:
return res, fmt.Errorf("%v is not a valid JSON Patch operator", op.Op)
}
}
return res, nil
}
// Apply applies rawPatch (which must be a []byte containing a valid
// JSON Patch) to base, yielding result. If err is returned, the
// returned int is the index of the operation that failed. If the
// error is that rawPatch is not a valid JSON Patch, loc will be 0,
//
// base must be the result of unmarshaling JSON to interface{}, and
// will not be modified.
func Apply(base interface{}, rawPatch []byte) (result interface{}, err error, loc int) {
p, err := newPatch(rawPatch)
if err != nil {
return nil, err, 0
}
result = utils.Clone(base)
for i, op := range p {
result, err = op.Apply(result)
if err != nil {
return result, err, i
}
}
return result, nil, 0
}
// ApplyJSON does the same thing as Apply, except the inputs should be
// JSON-containing byte arrays instead of unmarshalled JSON
func ApplyJSON(base, rawPatch []byte) (result []byte, err error, loc int) {
var rawBase interface{}
err = json.Unmarshal(base, &rawBase)
if err != nil {
return nil, err, 0
}
rawRes, err, loc := Apply(rawBase, rawPatch)
if err != nil {
return nil, err, loc
}
result, err = json.Marshal(rawRes)
return result, err, loc
}