@@ -185,41 +185,41 @@ func indexArg(index reflect.Value, cap int) (int, error) {
185
185
// arguments. Thus "index x 1 2 3" is, in Go syntax, x[1][2][3]. Each
186
186
// indexed item must be a map, slice, or array.
187
187
func index (item reflect.Value , indexes ... reflect.Value ) (reflect.Value , error ) {
188
- v : = indirectInterface (item )
189
- if ! v .IsValid () {
188
+ item = indirectInterface (item )
189
+ if ! item .IsValid () {
190
190
return reflect.Value {}, fmt .Errorf ("index of untyped nil" )
191
191
}
192
- for _ , i := range indexes {
193
- index : = indirectInterface (i )
192
+ for _ , index := range indexes {
193
+ index = indirectInterface (index )
194
194
var isNil bool
195
- if v , isNil = indirect (v ); isNil {
195
+ if item , isNil = indirect (item ); isNil {
196
196
return reflect.Value {}, fmt .Errorf ("index of nil pointer" )
197
197
}
198
- switch v .Kind () {
198
+ switch item .Kind () {
199
199
case reflect .Array , reflect .Slice , reflect .String :
200
- x , err := indexArg (index , v .Len ())
200
+ x , err := indexArg (index , item .Len ())
201
201
if err != nil {
202
202
return reflect.Value {}, err
203
203
}
204
- v = v .Index (x )
204
+ item = item .Index (x )
205
205
case reflect .Map :
206
- index , err := prepareArg (index , v .Type ().Key ())
206
+ index , err := prepareArg (index , item .Type ().Key ())
207
207
if err != nil {
208
208
return reflect.Value {}, err
209
209
}
210
- if x := v .MapIndex (index ); x .IsValid () {
211
- v = x
210
+ if x := item .MapIndex (index ); x .IsValid () {
211
+ item = x
212
212
} else {
213
- v = reflect .Zero (v .Type ().Elem ())
213
+ item = reflect .Zero (item .Type ().Elem ())
214
214
}
215
215
case reflect .Invalid :
216
- // the loop holds invariant: v .IsValid()
216
+ // the loop holds invariant: item .IsValid()
217
217
panic ("unreachable" )
218
218
default :
219
- return reflect.Value {}, fmt .Errorf ("can't index item of type %s" , v .Type ())
219
+ return reflect.Value {}, fmt .Errorf ("can't index item of type %s" , item .Type ())
220
220
}
221
221
}
222
- return v , nil
222
+ return item , nil
223
223
}
224
224
225
225
// Slicing.
@@ -229,29 +229,27 @@ func index(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error)
229
229
// is x[:], "slice x 1" is x[1:], and "slice x 1 2 3" is x[1:2:3]. The first
230
230
// argument must be a string, slice, or array.
231
231
func slice (item reflect.Value , indexes ... reflect.Value ) (reflect.Value , error ) {
232
- var (
233
- cap int
234
- v = indirectInterface (item )
235
- )
236
- if ! v .IsValid () {
232
+ item = indirectInterface (item )
233
+ if ! item .IsValid () {
237
234
return reflect.Value {}, fmt .Errorf ("slice of untyped nil" )
238
235
}
239
236
if len (indexes ) > 3 {
240
237
return reflect.Value {}, fmt .Errorf ("too many slice indexes: %d" , len (indexes ))
241
238
}
242
- switch v .Kind () {
239
+ var cap int
240
+ switch item .Kind () {
243
241
case reflect .String :
244
242
if len (indexes ) == 3 {
245
243
return reflect.Value {}, fmt .Errorf ("cannot 3-index slice a string" )
246
244
}
247
- cap = v .Len ()
245
+ cap = item .Len ()
248
246
case reflect .Array , reflect .Slice :
249
- cap = v .Cap ()
247
+ cap = item .Cap ()
250
248
default :
251
- return reflect.Value {}, fmt .Errorf ("can't slice item of type %s" , v .Type ())
249
+ return reflect.Value {}, fmt .Errorf ("can't slice item of type %s" , item .Type ())
252
250
}
253
251
// set default values for cases item[:], item[i:].
254
- idx := [3 ]int {0 , v .Len ()}
252
+ idx := [3 ]int {0 , item .Len ()}
255
253
for i , index := range indexes {
256
254
x , err := indexArg (index , cap )
257
255
if err != nil {
@@ -264,44 +262,40 @@ func slice(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error)
264
262
return reflect.Value {}, fmt .Errorf ("invalid slice index: %d > %d" , idx [0 ], idx [1 ])
265
263
}
266
264
if len (indexes ) < 3 {
267
- return v .Slice (idx [0 ], idx [1 ]), nil
265
+ return item .Slice (idx [0 ], idx [1 ]), nil
268
266
}
269
267
// given item[i:j:k], make sure i <= j <= k.
270
268
if idx [1 ] > idx [2 ] {
271
269
return reflect.Value {}, fmt .Errorf ("invalid slice index: %d > %d" , idx [1 ], idx [2 ])
272
270
}
273
- return v .Slice3 (idx [0 ], idx [1 ], idx [2 ]), nil
271
+ return item .Slice3 (idx [0 ], idx [1 ], idx [2 ]), nil
274
272
}
275
273
276
274
// Length
277
275
278
276
// length returns the length of the item, with an error if it has no defined length.
279
- func length (item interface {}) (int , error ) {
280
- v := reflect .ValueOf (item )
281
- if ! v .IsValid () {
282
- return 0 , fmt .Errorf ("len of untyped nil" )
283
- }
284
- v , isNil := indirect (v )
277
+ func length (item reflect.Value ) (int , error ) {
278
+ item , isNil := indirect (item )
285
279
if isNil {
286
280
return 0 , fmt .Errorf ("len of nil pointer" )
287
281
}
288
- switch v .Kind () {
282
+ switch item .Kind () {
289
283
case reflect .Array , reflect .Chan , reflect .Map , reflect .Slice , reflect .String :
290
- return v .Len (), nil
284
+ return item .Len (), nil
291
285
}
292
- return 0 , fmt .Errorf ("len of type %s" , v .Type ())
286
+ return 0 , fmt .Errorf ("len of type %s" , item .Type ())
293
287
}
294
288
295
289
// Function invocation
296
290
297
291
// call returns the result of evaluating the first argument as a function.
298
292
// The function must return 1 result, or 2 results, the second of which is an error.
299
293
func call (fn reflect.Value , args ... reflect.Value ) (reflect.Value , error ) {
300
- v : = indirectInterface (fn )
301
- if ! v .IsValid () {
294
+ fn = indirectInterface (fn )
295
+ if ! fn .IsValid () {
302
296
return reflect.Value {}, fmt .Errorf ("call of nil" )
303
297
}
304
- typ := v .Type ()
298
+ typ := fn .Type ()
305
299
if typ .Kind () != reflect .Func {
306
300
return reflect.Value {}, fmt .Errorf ("non-function of type %s" , typ )
307
301
}
@@ -322,19 +316,19 @@ func call(fn reflect.Value, args ...reflect.Value) (reflect.Value, error) {
322
316
}
323
317
argv := make ([]reflect.Value , len (args ))
324
318
for i , arg := range args {
325
- value : = indirectInterface (arg )
319
+ arg = indirectInterface (arg )
326
320
// Compute the expected type. Clumsy because of variadics.
327
321
argType := dddType
328
322
if ! typ .IsVariadic () || i < numIn - 1 {
329
323
argType = typ .In (i )
330
324
}
331
325
332
326
var err error
333
- if argv [i ], err = prepareArg (value , argType ); err != nil {
327
+ if argv [i ], err = prepareArg (arg , argType ); err != nil {
334
328
return reflect.Value {}, fmt .Errorf ("arg %d: %s" , i , err )
335
329
}
336
330
}
337
- return safeCall (v , argv )
331
+ return safeCall (fn , argv )
338
332
}
339
333
340
334
// safeCall runs fun.Call(args), and returns the resulting value and error, if
@@ -440,52 +434,52 @@ func basicKind(v reflect.Value) (kind, error) {
440
434
441
435
// eq evaluates the comparison a == b || a == c || ...
442
436
func eq (arg1 reflect.Value , arg2 ... reflect.Value ) (bool , error ) {
443
- v1 : = indirectInterface (arg1 )
444
- if v1 != zero {
445
- if t1 := v1 .Type (); ! t1 .Comparable () {
446
- return false , fmt .Errorf ("uncomparable type %s: %v" , t1 , v1 )
437
+ arg1 = indirectInterface (arg1 )
438
+ if arg1 != zero {
439
+ if t1 := arg1 .Type (); ! t1 .Comparable () {
440
+ return false , fmt .Errorf ("uncomparable type %s: %v" , t1 , arg1 )
447
441
}
448
442
}
449
443
if len (arg2 ) == 0 {
450
444
return false , errNoComparison
451
445
}
452
- k1 , _ := basicKind (v1 )
446
+ k1 , _ := basicKind (arg1 )
453
447
for _ , arg := range arg2 {
454
- v2 : = indirectInterface (arg )
455
- k2 , _ := basicKind (v2 )
448
+ arg = indirectInterface (arg )
449
+ k2 , _ := basicKind (arg )
456
450
truth := false
457
451
if k1 != k2 {
458
452
// Special case: Can compare integer values regardless of type's sign.
459
453
switch {
460
454
case k1 == intKind && k2 == uintKind :
461
- truth = v1 .Int () >= 0 && uint64 (v1 .Int ()) == v2 .Uint ()
455
+ truth = arg1 .Int () >= 0 && uint64 (arg1 .Int ()) == arg .Uint ()
462
456
case k1 == uintKind && k2 == intKind :
463
- truth = v2 .Int () >= 0 && v1 .Uint () == uint64 (v2 .Int ())
457
+ truth = arg .Int () >= 0 && arg1 .Uint () == uint64 (arg .Int ())
464
458
default :
465
459
return false , errBadComparison
466
460
}
467
461
} else {
468
462
switch k1 {
469
463
case boolKind :
470
- truth = v1 .Bool () == v2 .Bool ()
464
+ truth = arg1 .Bool () == arg .Bool ()
471
465
case complexKind :
472
- truth = v1 .Complex () == v2 .Complex ()
466
+ truth = arg1 .Complex () == arg .Complex ()
473
467
case floatKind :
474
- truth = v1 .Float () == v2 .Float ()
468
+ truth = arg1 .Float () == arg .Float ()
475
469
case intKind :
476
- truth = v1 .Int () == v2 .Int ()
470
+ truth = arg1 .Int () == arg .Int ()
477
471
case stringKind :
478
- truth = v1 .String () == v2 .String ()
472
+ truth = arg1 .String () == arg .String ()
479
473
case uintKind :
480
- truth = v1 .Uint () == v2 .Uint ()
474
+ truth = arg1 .Uint () == arg .Uint ()
481
475
default :
482
- if v2 == zero {
483
- truth = v1 == v2
476
+ if arg == zero {
477
+ truth = arg1 == arg
484
478
} else {
485
- if t2 := v2 .Type (); ! t2 .Comparable () {
486
- return false , fmt .Errorf ("uncomparable type %s: %v" , t2 , v2 )
479
+ if t2 := arg .Type (); ! t2 .Comparable () {
480
+ return false , fmt .Errorf ("uncomparable type %s: %v" , t2 , arg )
487
481
}
488
- truth = v1 .Interface () == v2 .Interface ()
482
+ truth = arg1 .Interface () == arg .Interface ()
489
483
}
490
484
}
491
485
}
@@ -505,13 +499,13 @@ func ne(arg1, arg2 reflect.Value) (bool, error) {
505
499
506
500
// lt evaluates the comparison a < b.
507
501
func lt (arg1 , arg2 reflect.Value ) (bool , error ) {
508
- v1 : = indirectInterface (arg1 )
509
- k1 , err := basicKind (v1 )
502
+ arg1 = indirectInterface (arg1 )
503
+ k1 , err := basicKind (arg1 )
510
504
if err != nil {
511
505
return false , err
512
506
}
513
- v2 : = indirectInterface (arg2 )
514
- k2 , err := basicKind (v2 )
507
+ arg2 = indirectInterface (arg2 )
508
+ k2 , err := basicKind (arg2 )
515
509
if err != nil {
516
510
return false , err
517
511
}
@@ -520,9 +514,9 @@ func lt(arg1, arg2 reflect.Value) (bool, error) {
520
514
// Special case: Can compare integer values regardless of type's sign.
521
515
switch {
522
516
case k1 == intKind && k2 == uintKind :
523
- truth = v1 .Int () < 0 || uint64 (v1 .Int ()) < v2 .Uint ()
517
+ truth = arg1 .Int () < 0 || uint64 (arg1 .Int ()) < arg2 .Uint ()
524
518
case k1 == uintKind && k2 == intKind :
525
- truth = v2 .Int () >= 0 && v1 .Uint () < uint64 (v2 .Int ())
519
+ truth = arg2 .Int () >= 0 && arg1 .Uint () < uint64 (arg2 .Int ())
526
520
default :
527
521
return false , errBadComparison
528
522
}
@@ -531,13 +525,13 @@ func lt(arg1, arg2 reflect.Value) (bool, error) {
531
525
case boolKind , complexKind :
532
526
return false , errBadComparisonType
533
527
case floatKind :
534
- truth = v1 .Float () < v2 .Float ()
528
+ truth = arg1 .Float () < arg2 .Float ()
535
529
case intKind :
536
- truth = v1 .Int () < v2 .Int ()
530
+ truth = arg1 .Int () < arg2 .Int ()
537
531
case stringKind :
538
- truth = v1 .String () < v2 .String ()
532
+ truth = arg1 .String () < arg2 .String ()
539
533
case uintKind :
540
- truth = v1 .Uint () < v2 .Uint ()
534
+ truth = arg1 .Uint () < arg2 .Uint ()
541
535
default :
542
536
panic ("invalid kind" )
543
537
}
0 commit comments