@@ -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).
164164func (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.
203201func (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.
241237func (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.
291287func (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.
342338func (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.
405399func (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.
452446func (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.
467461func (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
0 commit comments