-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpatch_utils.go
More file actions
75 lines (61 loc) · 1.6 KB
/
patch_utils.go
File metadata and controls
75 lines (61 loc) · 1.6 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
package deep
import (
"fmt"
"github.com/brunoga/deep/v4/internal/core"
)
// applyToBuilder recursively applies an operation to a PatchBuilder.
// This is used by patch.Reverse and patch.Merge to construct new patches.
func applyToBuilder[T any](b *PatchBuilder[T], op OpInfo) error {
// Apply conditions if present.
if op.Conditions != nil {
// Placeholder for condition re-attachment
}
switch op.Kind {
case OpReplace:
b.Navigate(op.Path).Put(op.Val)
case OpAdd:
parentPath, lastPart, err := core.DeepPath(op.Path).ResolveParentPath()
if err != nil {
return fmt.Errorf("invalid path for Add %s: %w", op.Path, err)
}
node := b.Navigate(string(parentPath))
if lastPart.IsIndex {
node.Add(lastPart.Index, op.Val)
} else {
node.Add(lastPart.Key, op.Val)
}
case OpRemove:
parentPath, lastPart, err := core.DeepPath(op.Path).ResolveParentPath()
if err != nil {
return fmt.Errorf("invalid path for Remove %s: %w", op.Path, err)
}
node := b.Navigate(string(parentPath))
if lastPart.IsIndex {
node.Delete(lastPart.Index, op.Val)
} else {
node.Delete(lastPart.Key, op.Val)
}
case OpMove:
b.Navigate(op.Path).Move(op.From)
case OpCopy:
b.Navigate(op.Path).Copy(op.From)
case OpTest:
b.Navigate(op.Path).Test(op.Val)
case OpLog:
if msg, ok := op.Val.(string); ok {
b.Navigate(op.Path).Log(msg)
}
}
if b.state.err != nil {
return b.state.err
}
return nil
}
// OpInfo represents a flattened operation from a patch.
type OpInfo struct {
Kind OpKind
Path string
From string // For Move/Copy
Val any
Conditions any // Placeholder
}