forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.go
136 lines (119 loc) · 2.79 KB
/
operation.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
package jsondiff
import (
"encoding/json"
"strings"
"github.com/tidwall/gjson"
)
// JSON Patch operation types.
// These are defined in RFC 6902 section 4.
// https://datatracker.ietf.org/doc/html/rfc6902#section-4
const (
OperationAdd = "add"
OperationReplace = "replace"
OperationRemove = "remove"
OperationMove = "move"
OperationCopy = "copy"
OperationTest = "test"
)
const (
fromFieldLen = 10 // ,"from":""
valueFieldLen = 9 // ,"value":
opBaseLen = 19 // {"op":"","path":""}
)
// Patch represents a series of JSON Patch operations.
type Patch []Operation
// Operation represents a single RFC6902 JSON Patch operation.
type Operation struct {
Type string `json:"op"`
From pointer `json:"from,omitempty"`
Path pointer `json:"path"`
OldValue interface{} `json:"-"`
Value interface{} `json:"value,omitempty"`
}
// String implements the fmt.Stringer interface.
func (o Operation) String() string {
b, err := json.Marshal(o)
if err != nil {
return "<invalid operation>"
}
return string(b)
}
// MarshalJSON implements the json.Marshaler interface.
func (o Operation) MarshalJSON() ([]byte, error) {
type op Operation
if !o.hasValue() {
o.Value = nil
}
if !o.hasFrom() {
o.From = emptyPtr
}
return json.Marshal(op(o))
}
// jsonLength returns the length in bytes that the
// operation would occupy when marshaled as JSON.
func (o Operation) jsonLength(targetBytes []byte) int {
l := opBaseLen + len(o.Type) + len(o.Path)
if o.hasValue() {
valueLen := len(targetBytes)
if !o.Path.isRoot() {
r := gjson.GetBytes(targetBytes, o.Path.toJSONPath())
valueLen = len(r.Raw)
}
l += valueFieldLen + valueLen
}
if o.hasFrom() {
l += fromFieldLen + len(o.From)
}
return l
}
func (o Operation) hasFrom() bool {
switch o.Type {
case OperationAdd, OperationReplace, OperationTest:
return false
default:
return true
}
}
func (o Operation) hasValue() bool {
switch o.Type {
case OperationCopy, OperationMove:
return false
default:
return true
}
}
// String implements the fmt.Stringer interface.
func (p Patch) String() string {
sb := strings.Builder{}
for i, op := range p {
if i != 0 {
sb.WriteByte('\n')
}
sb.WriteString(op.String())
}
return sb.String()
}
func (p *Patch) remove(idx int) Patch {
return (*p)[:idx+copy((*p)[idx:], (*p)[idx+1:])]
}
func (p *Patch) append(typ string, from, path pointer, src, tgt interface{}) Patch {
return append(*p, Operation{
Type: typ,
From: from,
Path: path,
OldValue: src,
Value: tgt,
})
}
func (p Patch) jsonLength(targetBytes []byte) int {
length := 0
for _, op := range p {
length += op.jsonLength(targetBytes)
}
// Count comma-separators if the patch
// has more than one operation.
if len(p) > 1 {
length += len(p) - 1
}
return length
}