forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
155 lines (136 loc) · 3.24 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package jsondiff
import (
"encoding/json"
"strings"
"unsafe"
"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 = len(`,"from":""`)
valueFieldLen = len(`,"value":`)
opBaseLen = len(`{"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 string `json:"from,omitempty"`
Path string `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)
}
type jsonNull struct{}
// MarshalJSON implements the json.Marshaler interface.
func (jn jsonNull) MarshalJSON() ([]byte, error) {
return []byte("null"), nil
}
// MarshalJSON implements the json.Marshaler interface.
func (o Operation) MarshalJSON() ([]byte, error) {
type op Operation
if !o.marshalWithValue() {
o.Value = nil
} else {
// Generic check that passes for nil and type nil interface values.
if (*[2]uintptr)(unsafe.Pointer(&o.Value))[1] == 0 {
o.Value = jsonNull{}
}
}
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.marshalWithValue() {
valueLen := len(targetBytes)
if len(o.Path) != 0 {
r := gjson.GetBytes(targetBytes, toJSONPath(o.Path))
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) marshalWithValue() bool {
switch o.Type {
case OperationCopy, OperationMove, OperationRemove:
return false
default:
return true
}
}
// String implements the fmt.Stringer interface.
func (p *Patch) String() string {
if p == nil || len(*p) == 0 {
return ""
}
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 string, 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 {
if p == nil {
return 0
}
var length int
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
}