/
patch.go
174 lines (158 loc) · 4.18 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package jsonpatch2
// 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/jsonpatch2/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 string `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 string `json:"from"`
// Value is the Value to be used for add, replace, and test operations.
Value interface{} `json:"value"`
path, from Pointer
}
func (o *Operation) fixPointers() error {
path, err := NewPointer(o.Path)
if err != nil {
return err
}
o.path = path
switch o.Op {
case "copy", "move":
from, err := NewPointer(o.From)
if err != nil {
return err
}
o.from = from
}
return nil
}
func (o *Operation) UnmarshalJSON(buf []byte) error {
type op struct {
Op string `json:"op"`
Path string `json:"path"`
From string `json:"from"`
Value interface{} `json:"value"`
}
ref := op{}
if err := json.Unmarshal(buf, &ref); err != nil {
return err
}
o.Op, o.Path, o.From, o.Value = ref.Op, ref.Path, ref.From, ref.Value
return o.fixPointers()
}
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", "replace", "add":
if op.Value == nil {
return res, fmt.Errorf("%v must have a valid value", op.Op)
}
case "move", "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
}
func (p Patch) apply(base interface{}) (result interface{}, err error, loc int) {
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
}
func (p Patch) fixPointers() error {
if len(p) > 0 && p[0].path == nil {
for i := range p {
if err := p[i].fixPointers(); err != nil {
return err
}
}
}
return nil
}
// Apply applies p to base (which must be a byte array containing
// valid JSON), yielding result (which will also be a byte array
// containing valid JSON). If err is returned, the returned int is
// the index of the operation that failed.
// ApplyJSON does the same thing as Apply, except the inputs should be
// JSON-containing byte arrays instead of unmarshalled JSON
func (p Patch) Apply(base []byte) (result []byte, err error, loc int) {
var rawBase interface{}
err = json.Unmarshal(base, &rawBase)
if err != nil {
return nil, err, 0
}
if err := p.fixPointers(); err != nil {
return nil, err, 0
}
rawRes, err, loc := p.apply(rawBase)
if err != nil {
return nil, err, loc
}
result, err = json.Marshal(rawRes)
return result, err, loc
}