Skip to content

Commit 32dc5d1

Browse files
committed
...
1 parent ba514f2 commit 32dc5d1

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

diff/ascii.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ func (f *ASCIIFormatter) processObject(object map[string]interface{}, deltas []D
115115

116116
// Added
117117
for _, delta := range deltas {
118-
switch dt := delta.(type) {
119-
case *Added:
118+
if dt, ok := delta.(*Added); ok {
120119
d := dt
121120
f.printRecursive(d.String(), d.Value, ASCIIAdded)
122121
}
@@ -131,10 +130,8 @@ func (f *ASCIIFormatter) processItem(value interface{}, deltas []Delta, position
131130

132131
if len(matchedDeltas) > 0 {
133132
for _, matchedDelta := range matchedDeltas {
134-
switch matchedDelta.(type) {
133+
switch d := matchedDelta.(type) {
135134
case *Object:
136-
d := matchedDelta.(*Object)
137-
138135
switch value.(type) {
139136
case map[string]interface{}:
140137
// ok
@@ -157,8 +154,6 @@ func (f *ASCIIFormatter) processItem(value interface{}, deltas []Delta, position
157154
f.closeLine()
158155

159156
case *Array:
160-
d := matchedDelta.(*Array)
161-
162157
switch value.(type) {
163158
case []interface{}:
164159
// ok
@@ -181,31 +176,27 @@ func (f *ASCIIFormatter) processItem(value interface{}, deltas []Delta, position
181176
f.closeLine()
182177

183178
case *Added:
184-
d := matchedDelta.(*Added)
185179
f.printRecursive(positionStr, d.Value, ASCIIAdded)
186180

187181
f.size[len(f.size)-1]++
188182

189183
case *Modified:
190-
d := matchedDelta.(*Modified)
191184
savedSize := f.size[len(f.size)-1]
192185
f.printRecursive(positionStr, d.OldValue, ASCIIDeleted)
193186
f.size[len(f.size)-1] = savedSize
194187
f.printRecursive(positionStr, d.NewValue, ASCIIAdded)
195188

196189
case *TextDiff:
197190
savedSize := f.size[len(f.size)-1]
198-
d := matchedDelta.(*TextDiff)
199191
f.printRecursive(positionStr, d.OldValue, ASCIIDeleted)
200192
f.size[len(f.size)-1] = savedSize
201193
f.printRecursive(positionStr, d.NewValue, ASCIIAdded)
202194

203195
case *Deleted:
204-
d := matchedDelta.(*Deleted)
205196
f.printRecursive(positionStr, d.Value, ASCIIDeleted)
206197

207198
default:
208-
return errors.New("Unknown Delta type detected")
199+
return errors.New("unknown Delta type detected")
209200
}
210201
}
211202
} else {

diff/deltas.go

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,11 @@ func NewObject(position Position, deltas []Delta) *Object {
162162

163163
// PostApply processes the given object by applying deltas at positions determined by the object's type (map or slice).
164164
func (d *Object) PostApply(object interface{}) interface{} {
165-
switch object.(type) {
165+
switch o := object.(type) {
166166
case map[string]interface{}:
167-
o := object.(map[string]interface{})
168167
n := string(d.PostPosition().(Name))
169168
o[n] = applyDeltas(d.Deltas, o[n])
170169
case []interface{}:
171-
o := object.([]interface{})
172170
n := int(d.PostPosition().(Index))
173171
o[n] = applyDeltas(d.Deltas, o[n])
174172
}
@@ -201,13 +199,11 @@ func NewArray(position Position, deltas []Delta) *Array {
201199

202200
// PostApply applies the stored deltas to the provided object based on their positions, modifying and returning the object.
203201
func (d *Array) PostApply(object interface{}) interface{} {
204-
switch object.(type) {
202+
switch o := object.(type) {
205203
case map[string]interface{}:
206-
o := object.(map[string]interface{})
207204
n := string(d.PostPosition().(Name))
208205
o[n] = applyDeltas(d.Deltas, o[n])
209206
case []interface{}:
210-
o := object.([]interface{})
211207
n := int(d.PostPosition().(Index))
212208
o[n] = applyDeltas(d.Deltas, o[n])
213209
}
@@ -239,21 +235,21 @@ func NewAdded(position Position, value interface{}) *Added {
239235

240236
// PostApply applies the added value to the given object at the position specified by the PostPosition method.
241237
func (d *Added) PostApply(object interface{}) interface{} {
242-
switch object.(type) {
238+
switch o := object.(type) {
243239
case map[string]interface{}:
244240
object.(map[string]interface{})[string(d.PostPosition().(Name))] = d.Value
245241
case []interface{}:
246242
i := int(d.PostPosition().(Index))
247-
o := object.([]interface{})
248243

249244
if i < len(o) {
250245
o = append(o, 0) // dummy
251246
copy(o[i+1:], o[i:])
252247
o[i] = d.Value
253-
object = o
254-
} else {
255-
object = append(o, d.Value)
248+
249+
return o
256250
}
251+
252+
return append(o, d.Value)
257253
}
258254

259255
return object
@@ -289,11 +285,11 @@ func NewModified(position Position, oldValue, newValue interface{}) *Modified {
289285

290286
// PostApply updates a map or slice at a specific position with a new value and returns the modified object.
291287
func (d *Modified) PostApply(object interface{}) interface{} {
292-
switch object.(type) {
288+
switch o := object.(type) {
293289
case map[string]interface{}:
294-
object.(map[string]interface{})[string(d.PostPosition().(Name))] = d.NewValue
290+
o[string(d.PostPosition().(Name))] = d.NewValue
295291
case []interface{}:
296-
object.([]interface{})[int(d.PostPosition().(Index))] = d.NewValue
292+
o[(d.PostPosition().(Index))] = d.NewValue
297293
}
298294

299295
return object
@@ -304,11 +300,11 @@ func (d *Modified) similarity() (similarity float64) {
304300
if reflect.TypeOf(d.OldValue) == reflect.TypeOf(d.NewValue) {
305301
similarity += 0.3 // types are same
306302

307-
switch d.OldValue.(type) {
303+
switch t := d.OldValue.(type) {
308304
case string:
309-
similarity += 0.4 * stringSimilarity(d.OldValue.(string), d.NewValue.(string))
305+
similarity += 0.4 * stringSimilarity(t, d.NewValue.(string))
310306
case float64:
311-
ratio := d.OldValue.(float64) / d.NewValue.(float64)
307+
ratio := t / d.NewValue.(float64)
312308
if ratio > 1 {
313309
ratio = 1 / ratio
314310
}
@@ -340,15 +336,13 @@ func NewTextDiff(position Position, diff []dmp.Patch, oldValue, newValue interfa
340336

341337
// PostApply updates the provided object with the changes specified in the TextDiff and returns the modified object.
342338
func (d *TextDiff) PostApply(object interface{}) interface{} {
343-
switch object.(type) {
339+
switch o := object.(type) {
344340
case map[string]interface{}:
345-
o := object.(map[string]interface{})
346341
i := string(d.PostPosition().(Name))
347342
d.OldValue = o[i]
348343
d.patch()
349344
o[i] = d.NewValue
350345
case []interface{}:
351-
o := object.([]interface{})
352346
i := d.PostPosition().(Index)
353347
d.OldValue = o[i]
354348
d.patch()
@@ -403,13 +397,13 @@ func NewDeleted(position Position, value interface{}) *Deleted {
403397

404398
// PreApply removes an element from a map or slice based on the position specified in the Deleted instance.
405399
func (d Deleted) PreApply(object interface{}) interface{} {
406-
switch object.(type) {
400+
switch o := object.(type) {
407401
case map[string]interface{}:
408402
delete(object.(map[string]interface{}), string(d.PrePosition().(Name)))
409403
case []interface{}:
410404
i := int(d.PrePosition().(Index))
411-
o := object.([]interface{})
412-
object = append(o[:i], o[i+1:]...)
405+
406+
return append(o[:i], o[i+1:]...)
413407
}
414408

415409
return object
@@ -450,27 +444,27 @@ func NewMoved(oldPosition Position, newPosition Position, value interface{}, del
450444

451445
// PreApply modifies the given object by removing the element at the pre-move index and storing its value in the Moved instance.
452446
func (d *Moved) PreApply(object interface{}) interface{} {
453-
switch object.(type) {
447+
switch o := object.(type) {
454448
case map[string]interface{}:
455449
// not supported
456450
case []interface{}:
457451
i := int(d.PrePosition().(Index))
458-
o := object.([]interface{})
459452
d.Value = o[i]
460-
object = append(o[:i], o[i+1:]...)
453+
454+
return append(o[:i], o[i+1:]...)
461455
}
462456

463457
return object
464458
}
465459

466460
// PostApply applies the stored delta after a move operation and adjusts the position of a value in the object.
467461
func (d *Moved) PostApply(object interface{}) interface{} {
468-
switch object.(type) {
462+
switch o := object.(type) {
469463
case map[string]interface{}:
470464
// not supported
471465
case []interface{}:
472466
i := int(d.PostPosition().(Index))
473-
o := object.([]interface{})
467+
474468
o = append(o, 0) // dummy
475469
copy(o[i+1:], o[i:])
476470
o[i] = d.Value

diff/diff.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,17 +260,14 @@ func (differ *Differ) compareValues(
260260
return false, NewModified(position, left, right)
261261
}
262262

263-
switch left.(type) {
263+
switch l := left.(type) {
264264
case map[string]interface{}:
265-
l := left.(map[string]interface{})
266-
267265
childDeltas := differ.compareMaps(l, right.(map[string]interface{}))
268266
if len(childDeltas) > 0 {
269267
return false, NewObject(position, childDeltas)
270268
}
271269

272270
case []interface{}:
273-
l := left.([]interface{})
274271
childDeltas := differ.compareArrays(l, right.([]interface{}))
275272

276273
if len(childDeltas) > 0 {
@@ -291,9 +288,9 @@ func (differ *Differ) compareValues(
291288
reflect.ValueOf(right).Kind() == reflect.String &&
292289
differ.textDiffMinimumLength <= len(reflect.ValueOf(left).String()) {
293290
textDiff := dmp.New()
294-
patchs := textDiff.PatchMake(reflect.ValueOf(left).String(), reflect.ValueOf(right).String())
291+
patches := textDiff.PatchMake(reflect.ValueOf(left).String(), reflect.ValueOf(right).String())
295292

296-
return false, NewTextDiff(position, patchs, left, right)
293+
return false, NewTextDiff(position, patches, left, right)
297294
}
298295

299296
return false, NewModified(position, left, right)

0 commit comments

Comments
 (0)