-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathoperation.go
165 lines (146 loc) · 3.43 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
156
157
158
159
160
161
162
163
164
165
package jsondiff
import (
"encoding/json"
"strings"
"unsafe"
)
// 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":""}`)
)
// null represents a JSON null value.
type null struct{}
// Patch represents a series of JSON Patch operations.
type Patch []Operation
// Operation represents a single JSON Patch (RFC6902) operation.
type Operation struct {
Value interface{} `json:"value,omitempty"`
OldValue interface{} `json:"-"`
Type string `json:"op"`
From string `json:"from,omitempty"`
Path string `json:"path"`
valueLen int
}
// MarshalJSON implements the json.Marshaler interface.
func (null) MarshalJSON() ([]byte, error) {
return []byte("null"), nil
}
// 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.marshalWithValue() {
o.Value = nil
} else if (*[2]uintptr)(unsafe.Pointer(&o.Value))[1] == 0 {
// Generic check that works for nil
// and typed nil interface values.
o.Value = null{}
}
if !o.hasFrom() {
o.From = emptyPointer
}
return json.Marshal(op(o))
}
// jsonLength returns the length in bytes that the
// operation would occupy when marshaled to JSON.
func (o Operation) jsonLength() int {
l := opBaseLen + len(o.Type) + len(o.Path)
if o.marshalWithValue() {
l += valueFieldLen + o.valueLen
}
if o.hasFrom() {
l += fromFieldLen + len(o.From)
}
return l
}
func (o Operation) hasFrom() bool {
switch o.Type {
case OperationCopy, OperationMove:
return true
default:
return false
}
}
func (o Operation) marshalWithValue() bool {
switch o.Type {
case OperationAdd, OperationReplace, OperationTest:
return true
default:
return false
}
}
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{}, vl int) Patch {
return append(*p, Operation{
Type: typ,
From: from,
Path: path,
OldValue: src,
Value: tgt,
valueLen: vl,
})
}
func (p *Patch) insert(pos int, typ string, from, path string, src, tgt interface{}, vl int) Patch {
if pos > len(*p) {
return p.append(typ, from, path, src, tgt, vl)
}
op := Operation{
Type: typ,
From: from,
Path: path,
OldValue: src,
Value: tgt,
valueLen: vl,
}
return append((*p)[:pos], append([]Operation{op}, (*p)[pos:]...)...)
}
func (p *Patch) jsonLength() int {
if p == nil {
return 0
}
var length int
for _, op := range *p {
length += op.jsonLength()
}
// Count comma-separators if the patch
// has more than one operation.
if len(*p) > 1 {
length += len(*p) - 1
}
return length
}
// 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()
}