-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpatch.go
More file actions
359 lines (309 loc) · 7.79 KB
/
patch.go
File metadata and controls
359 lines (309 loc) · 7.79 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package deep
import (
"encoding/gob"
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/brunoga/deep/v4/cond"
)
// OpKind represents the type of operation in a patch.
type OpKind int
const (
OpAdd OpKind = iota
OpRemove
OpReplace
OpMove
OpCopy
OpTest
OpLog
)
func (k OpKind) String() string {
switch k {
case OpAdd:
return "add"
case OpRemove:
return "remove"
case OpReplace:
return "replace"
case OpMove:
return "move"
case OpCopy:
return "copy"
case OpTest:
return "test"
case OpLog:
return "log"
default:
return "unknown"
}
}
// Patch represents a set of changes that can be applied to a value of type T.
type Patch[T any] interface {
fmt.Stringer
// Apply applies the patch to the value pointed to by v.
// The value v must not be nil.
Apply(v *T)
// ApplyChecked applies the patch only if specific conditions are met.
// 1. If the patch has a global Condition, it must evaluate to true.
// 2. If Strict mode is enabled, every modification must match the 'oldVal' recorded in the patch.
// 3. Any local per-field conditions must evaluate to true.
ApplyChecked(v *T) error
// ApplyResolved applies the patch using a custom ConflictResolver.
// This is used for convergent synchronization (CRDTs).
ApplyResolved(v *T, r ConflictResolver) error
// Walk calls fn for every operation in the patch.
// The path is a JSON Pointer dot-notation path (e.g. "/Field/SubField/0").
// If fn returns an error, walking stops and that error is returned.
Walk(fn func(path string, op OpKind, old, new any) error) error
// WithCondition returns a new Patch with the given global condition attached.
WithCondition(c cond.Condition[T]) Patch[T]
// WithStrict returns a new Patch with the strict consistency check enabled or disabled.
WithStrict(strict bool) Patch[T]
// Reverse returns a new Patch that undoes the changes in this patch.
Reverse() Patch[T]
// ToJSONPatch returns an RFC 6902 compliant JSON Patch representation of this patch.
ToJSONPatch() ([]byte, error)
// Summary returns a human-readable summary of the changes in the patch.
Summary() string
// MarshalSerializable returns a serializable representation of the patch.
MarshalSerializable() (any, error)
}
// NewPatch returns a new, empty patch for type T.
func NewPatch[T any]() Patch[T] {
return &typedPatch[T]{}
}
// UnmarshalPatchSerializable reconstructs a patch from its serializable representation.
func UnmarshalPatchSerializable[T any](data any) (Patch[T], error) {
if data == nil {
return &typedPatch[T]{}, nil
}
m, ok := data.(map[string]any)
if !ok {
// Try direct unmarshal if it's not the wrapped map
inner, err := PatchFromSerializable(data)
if err != nil {
return nil, err
}
return &typedPatch[T]{inner: inner.(diffPatch)}, nil
}
innerData, ok := m["inner"]
if !ok {
// It might be a direct surrogate map
inner, err := PatchFromSerializable(m)
if err != nil {
return nil, err
}
return &typedPatch[T]{inner: inner.(diffPatch)}, nil
}
inner, err := PatchFromSerializable(innerData)
if err != nil {
return nil, err
}
p := &typedPatch[T]{
inner: inner.(diffPatch),
}
if condData, ok := m["cond"]; ok && condData != nil {
c, err := cond.ConditionFromSerializable[T](condData)
if err != nil {
return nil, err
}
p.cond = c
}
if strict, ok := m["strict"].(bool); ok {
p.strict = strict
}
return p, nil
}
// Register registers the Patch implementation for type T with the gob package.
// This is required if you want to use Gob serialization with Patch[T].
func Register[T any]() {
gob.Register(&typedPatch[T]{})
}
// ApplyError represents one or more errors that occurred during patch application.
type ApplyError struct {
errors []error
}
func (e *ApplyError) Error() string {
if len(e.errors) == 1 {
return e.errors[0].Error()
}
var b strings.Builder
b.WriteString(fmt.Sprintf("%d errors during apply:\n", len(e.errors)))
for _, err := range e.errors {
b.WriteString("- " + err.Error() + "\n")
}
return b.String()
}
func (e *ApplyError) Unwrap() []error {
return e.errors
}
func (e *ApplyError) Errors() []error {
return e.errors
}
type typedPatch[T any] struct {
inner diffPatch
cond cond.Condition[T]
strict bool
}
type patchUnwrapper interface {
unwrap() diffPatch
}
func (p *typedPatch[T]) unwrap() diffPatch {
return p.inner
}
func (p *typedPatch[T]) Apply(v *T) {
if p.inner == nil {
return
}
rv := reflect.ValueOf(v).Elem()
p.inner.apply(reflect.ValueOf(v), rv, "/")
}
func (p *typedPatch[T]) ApplyChecked(v *T) error {
if p.cond != nil {
ok, err := p.cond.Evaluate(v)
if err != nil {
return &ApplyError{errors: []error{fmt.Errorf("condition evaluation failed: %w", err)}}
}
if !ok {
return &ApplyError{errors: []error{fmt.Errorf("condition failed")}}
}
}
if p.inner == nil {
return nil
}
rv := reflect.ValueOf(v).Elem()
err := p.inner.applyChecked(reflect.ValueOf(v), rv, p.strict, "/")
if err != nil {
if ae, ok := err.(*ApplyError); ok {
return ae
}
return &ApplyError{errors: []error{err}}
}
return nil
}
func (p *typedPatch[T]) ApplyResolved(v *T, r ConflictResolver) error {
if p.inner == nil {
return nil
}
rv := reflect.ValueOf(v).Elem()
return p.inner.applyResolved(reflect.ValueOf(v), rv, "/", r)
}
func (p *typedPatch[T]) Walk(fn func(path string, op OpKind, old, new any) error) error {
if p.inner == nil {
return nil
}
return p.inner.walk("", func(path string, op OpKind, old, new any) error {
fullPath := path
if fullPath == "" {
fullPath = "/"
} else if fullPath[0] != '/' {
fullPath = "/" + fullPath
}
return fn(fullPath, op, old, new)
})
}
func (p *typedPatch[T]) WithCondition(c cond.Condition[T]) Patch[T] {
return &typedPatch[T]{
inner: p.inner,
cond: c,
strict: p.strict,
}
}
func (p *typedPatch[T]) WithStrict(strict bool) Patch[T] {
return &typedPatch[T]{
inner: p.inner,
cond: p.cond,
strict: strict,
}
}
func (p *typedPatch[T]) Reverse() Patch[T] {
if p.inner == nil {
return &typedPatch[T]{}
}
return &typedPatch[T]{
inner: p.inner.reverse(),
strict: p.strict,
}
}
func (p *typedPatch[T]) ToJSONPatch() ([]byte, error) {
if p.inner == nil {
return json.Marshal([]any{})
}
// We pass empty string because toJSONPatch prepends "/" when needed
// and handles root as "/".
return json.Marshal(p.inner.toJSONPatch(""))
}
func (p *typedPatch[T]) Summary() string {
if p.inner == nil {
return "No changes."
}
return p.inner.summary("/")
}
func (p *typedPatch[T]) MarshalSerializable() (any, error) {
inner, err := PatchToSerializable(p.inner)
if err != nil {
return nil, err
}
c, err := cond.ConditionToSerializable(p.cond)
if err != nil {
return nil, err
}
return map[string]any{
"inner": inner,
"cond": c,
"strict": p.strict,
}, nil
}
func (p *typedPatch[T]) String() string {
if p.inner == nil {
return "<nil>"
}
return p.inner.format(0)
}
func (p *typedPatch[T]) MarshalJSON() ([]byte, error) {
s, err := p.MarshalSerializable()
if err != nil {
return nil, err
}
return json.Marshal(s)
}
func (p *typedPatch[T]) UnmarshalJSON(data []byte) error {
var m map[string]any
if err := json.Unmarshal(data, &m); err != nil {
return err
}
res, err := UnmarshalPatchSerializable[T](m)
if err != nil {
return err
}
if tp, ok := res.(*typedPatch[T]); ok {
p.inner = tp.inner
p.cond = tp.cond
p.strict = tp.strict
}
return nil
}
func (p *typedPatch[T]) GobEncode() ([]byte, error) {
s, err := p.MarshalSerializable()
if err != nil {
return nil, err
}
return json.Marshal(s)
}
func (p *typedPatch[T]) GobDecode(data []byte) error {
var m map[string]any
if err := json.Unmarshal(data, &m); err != nil {
return err
}
res, err := UnmarshalPatchSerializable[T](m)
if err != nil {
return err
}
if tp, ok := res.(*typedPatch[T]); ok {
p.inner = tp.inner
p.cond = tp.cond
p.strict = tp.strict
}
return nil
}