-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpatchwerk.go
169 lines (150 loc) · 4.34 KB
/
patchwerk.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
166
167
168
169
package patchwerk
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
)
var errBadJSONDoc = fmt.Errorf("Invalid JSON Document")
type JSONPatchOperation struct {
Operation string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}
func (j *JSONPatchOperation) JSON() string {
b, _ := json.Marshal(j)
return string(b)
}
func (j *JSONPatchOperation) MarshalJSON() ([]byte, error) {
var b bytes.Buffer
b.WriteString("{")
b.WriteString(fmt.Sprintf(`"op":"%s"`, j.Operation))
b.WriteString(fmt.Sprintf(`,"path":"%s"`, j.Path))
// Consider omitting Value for non-nullable operations.
if j.Value != nil || j.Operation == "replace" || j.Operation == "add" {
v, err := json.Marshal(j.Value)
if err != nil {
return nil, err
}
b.WriteString(`,"value":`)
b.Write(v)
}
b.WriteString("}")
return b.Bytes(), nil
}
type ByPath []*JSONPatchOperation
func (a ByPath) Len() int { return len(a) }
func (a ByPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByPath) Less(i, j int) bool { return a[i].Path < a[j].Path }
func NewPatch(operation, path string, value interface{}) *JSONPatchOperation {
return &JSONPatchOperation{Operation: operation, Path: path, Value: value}
}
// Diff creates a patch as specified in http://jsonpatch.com/
//
// 'a' is original, 'b' is the modified document. Both are to be given as json encoded content.
// The function will return an array of JSONPatchOperations
func Diff(a, b []byte) ([]*JSONPatchOperation, error) {
var aI interface{}
var bI interface{}
err := json.Unmarshal(a, &aI)
if err != nil {
return nil, errBadJSONDoc
}
err = json.Unmarshal(b, &bI)
if err != nil {
return nil, errBadJSONDoc
}
return diff(aI, bI, "")
}
// DiffBytes creates a patch as specified in http://jsonpatch.com/
//
// 'a' is original, 'b' is the modified document. Both are to be given as json encoded content.
// The function will return JSON as a byte array
func DiffBytes(a, b []byte) ([]byte, error) {
ops, err := Diff(a, b)
if err != nil {
return nil, err
}
return json.Marshal(ops)
}
// From http://tools.ietf.org/html/rfc6901#section-4 :
//
// Evaluation of each reference token begins by decoding any escaped
// character sequence. This is performed by first transforming any
// occurrence of the sequence '~1' to '/', and then transforming any
// occurrence of the sequence '~0' to '~'.
// TODO decode support:
// var rfc6901Decoder = strings.NewReplacer("~1", "/", "~0", "~")
var rfc6901Encoder = strings.NewReplacer("~", "~0", "/", "~1")
func makePath(path string, newPart interface{}) string {
key := rfc6901Encoder.Replace(fmt.Sprintf("%v", newPart))
if path == "" {
return "/" + key
}
if strings.HasSuffix(path, "/") {
return path + key
}
return path + "/" + key
}
func diff(a, b interface{}, p string) ([]*JSONPatchOperation, error) {
fullReplace := []*JSONPatchOperation{NewPatch("replace", p, b)}
patch := []*JSONPatchOperation{}
// If values are not of the same type simply replace
if reflect.TypeOf(a) != reflect.TypeOf(b) {
return fullReplace, nil
}
var err error
var tempPatch []*JSONPatchOperation
switch at := a.(type) {
case map[string]interface{}:
bt := b.(map[string]interface{})
tempPatch, err = diffObjects(at, bt, p)
if err != nil {
return nil, err
}
patch = append(patch, tempPatch...)
case string, float64, bool:
if !reflect.DeepEqual(a, b) {
patch = append(patch, NewPatch("replace", p, b))
}
case []interface{}:
bt, ok := b.([]interface{})
if !ok {
// array replaced by non-array
patch = append(patch, NewPatch("replace", p, b))
} else {
// arrays are not the same length
tempPatch, err = diffArrays(at, bt, p)
if err != nil {
return nil, err
}
patch = append(patch, tempPatch...)
}
case nil:
switch b.(type) {
case nil:
// Both nil, fine.
default:
patch = append(patch, NewPatch("add", p, b))
}
default:
panic(fmt.Sprintf("Unknown type:%T ", a))
}
return getSmallestPatch(fullReplace, patch), nil
}
func getSmallestPatch(patches ...[]*JSONPatchOperation) []*JSONPatchOperation {
smallestPatch := patches[0]
b, _ := json.Marshal(patches[0])
smallestSize := len(b)
for i := 1; i < len(patches); i++ {
p := patches[i]
b, _ := json.Marshal(p)
size := len(b)
if size < smallestSize {
smallestPatch = p
smallestSize = size
}
}
return smallestPatch
}