Skip to content

Commit 6d5c3df

Browse files
committed
Fully remove dead code
Signed-off-by: John Howard <[email protected]>
1 parent 45186ed commit 6d5c3df

File tree

1 file changed

+12
-107
lines changed

1 file changed

+12
-107
lines changed

v2/jsonpatch.go

Lines changed: 12 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -214,22 +214,18 @@ func handleValues(av, bv interface{}, p string, patch []Operation) ([]Operation,
214214
}
215215
case []interface{}:
216216
bt := bv.([]interface{})
217-
if false && isSimpleArray(at) && isSimpleArray(bt) {
218-
patch = append(patch, compareEditDistance(at, bt, p)...)
219-
} else {
220-
n := min(len(at), len(bt))
221-
for i := len(at) - 1; i >= n; i-- {
222-
patch = append(patch, NewOperation("remove", makePath(p, i), nil))
223-
}
224-
for i := n; i < len(bt); i++ {
225-
patch = append(patch, NewOperation("add", makePath(p, i), bt[i]))
226-
}
227-
for i := 0; i < n; i++ {
228-
var err error
229-
patch, err = handleValues(at[i], bt[i], makePath(p, i), patch)
230-
if err != nil {
231-
return nil, err
232-
}
217+
n := min(len(at), len(bt))
218+
for i := len(at) - 1; i >= n; i-- {
219+
patch = append(patch, NewOperation("remove", makePath(p, i), nil))
220+
}
221+
for i := n; i < len(bt); i++ {
222+
patch = append(patch, NewOperation("add", makePath(p, i), bt[i]))
223+
}
224+
for i := 0; i < n; i++ {
225+
var err error
226+
patch, err = handleValues(at[i], bt[i], makePath(p, i), patch)
227+
if err != nil {
228+
return nil, err
233229
}
234230
}
235231
default:
@@ -238,100 +234,9 @@ func handleValues(av, bv interface{}, p string, patch []Operation) ([]Operation,
238234
return patch, nil
239235
}
240236

241-
func isBasicType(a interface{}) bool {
242-
switch a.(type) {
243-
case string, float64, bool:
244-
default:
245-
return false
246-
}
247-
return true
248-
}
249-
250-
func isSimpleArray(a []interface{}) bool {
251-
for i := range a {
252-
switch a[i].(type) {
253-
case string, float64, bool:
254-
default:
255-
val := reflect.ValueOf(a[i])
256-
if val.Kind() == reflect.Map {
257-
for _, k := range val.MapKeys() {
258-
av := val.MapIndex(k)
259-
if av.Kind() == reflect.Ptr || av.Kind() == reflect.Interface {
260-
if av.IsNil() {
261-
continue
262-
}
263-
av = av.Elem()
264-
}
265-
if av.Kind() != reflect.String && av.Kind() != reflect.Float64 && av.Kind() != reflect.Bool {
266-
return false
267-
}
268-
}
269-
return true
270-
}
271-
return false
272-
}
273-
}
274-
return true
275-
}
276-
277-
// https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm
278-
// Adapted from https://github.com/texttheater/golang-levenshtein
279-
func compareEditDistance(s, t []interface{}, p string) []Operation {
280-
m := len(s)
281-
n := len(t)
282-
283-
d := make([][]int, m+1)
284-
for i := 0; i <= m; i++ {
285-
d[i] = make([]int, n+1)
286-
d[i][0] = i
287-
}
288-
for j := 0; j <= n; j++ {
289-
d[0][j] = j
290-
}
291-
292-
for j := 1; j <= n; j++ {
293-
for i := 1; i <= m; i++ {
294-
if reflect.DeepEqual(s[i-1], t[j-1]) {
295-
d[i][j] = d[i-1][j-1] // no op required
296-
} else {
297-
del := d[i-1][j] + 1
298-
add := d[i][j-1] + 1
299-
rep := d[i-1][j-1] + 1
300-
d[i][j] = min(rep, min(add, del))
301-
}
302-
}
303-
}
304-
305-
return backtrace(s, t, p, m, n, d)
306-
}
307-
308237
func min(x int, y int) int {
309238
if y < x {
310239
return y
311240
}
312241
return x
313242
}
314-
315-
func backtrace(s, t []interface{}, p string, i int, j int, matrix [][]int) []Operation {
316-
if i > 0 && matrix[i-1][j]+1 == matrix[i][j] {
317-
op := NewOperation("remove", makePath(p, i-1), nil)
318-
return append([]Operation{op}, backtrace(s, t, p, i-1, j, matrix)...)
319-
}
320-
if j > 0 && matrix[i][j-1]+1 == matrix[i][j] {
321-
op := NewOperation("add", makePath(p, i), t[j-1])
322-
return append([]Operation{op}, backtrace(s, t, p, i, j-1, matrix)...)
323-
}
324-
if i > 0 && j > 0 && matrix[i-1][j-1]+1 == matrix[i][j] {
325-
if isBasicType(s[0]) {
326-
op := NewOperation("replace", makePath(p, i-1), t[j-1])
327-
return append([]Operation{op}, backtrace(s, t, p, i-1, j-1, matrix)...)
328-
}
329-
330-
p2, _ := handleValues(s[i-1], t[j-1], makePath(p, i-1), []Operation{})
331-
return append(p2, backtrace(s, t, p, i-1, j-1, matrix)...)
332-
}
333-
if i > 0 && j > 0 && matrix[i-1][j-1] == matrix[i][j] {
334-
return backtrace(s, t, p, i-1, j-1, matrix)
335-
}
336-
return []Operation{}
337-
}

0 commit comments

Comments
 (0)