Skip to content

Commit bd250b6

Browse files
committed
refine ToValue
1 parent d2a750b commit bd250b6

File tree

1 file changed

+4
-77
lines changed

1 file changed

+4
-77
lines changed

object.go

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -294,38 +294,21 @@ func ToValue(obj Object, v reflect.Value) bool {
294294
if !v.IsValid() || !v.CanSet() {
295295
return false
296296
}
297+
297298
switch v.Kind() {
298-
case reflect.Int8:
299-
fallthrough
300-
case reflect.Int16:
301-
fallthrough
302-
case reflect.Int32:
303-
fallthrough
304-
case reflect.Int64:
305-
fallthrough
306-
case reflect.Int:
299+
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
307300
if obj.IsLong() {
308301
v.SetInt(Cast[Long](obj).Int64())
309302
} else {
310303
return false
311304
}
312-
case reflect.Uint8:
313-
fallthrough
314-
case reflect.Uint16:
315-
fallthrough
316-
case reflect.Uint32:
317-
fallthrough
318-
case reflect.Uint64:
319-
fallthrough
320-
case reflect.Uint:
305+
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
321306
if obj.IsLong() {
322307
v.SetUint(Cast[Long](obj).Uint64())
323308
} else {
324309
return false
325310
}
326-
case reflect.Float32:
327-
fallthrough
328-
case reflect.Float64:
311+
case reflect.Float32, reflect.Float64:
329312
if obj.IsFloat() || obj.IsLong() {
330313
v.SetFloat(Cast[Float](obj).Float64())
331314
} else {
@@ -407,62 +390,6 @@ func ToValue(obj Object, v reflect.Value) bool {
407390
return true
408391
}
409392

410-
func To[T any](obj Object) (ret T) {
411-
switch any(ret).(type) {
412-
case int8:
413-
return any(int8(Cast[Long](obj).Int64())).(T)
414-
case int16:
415-
return any(int16(Cast[Long](obj).Int64())).(T)
416-
case int32:
417-
return any(int32(Cast[Long](obj).Int64())).(T)
418-
case int64:
419-
return any(Cast[Long](obj).Int64()).(T)
420-
case int:
421-
return any(int(Cast[Long](obj).Int64())).(T)
422-
case uint8:
423-
return any(uint8(Cast[Long](obj).Uint64())).(T)
424-
case uint16:
425-
return any(uint16(Cast[Long](obj).Uint64())).(T)
426-
case uint32:
427-
return any(uint32(Cast[Long](obj).Uint64())).(T)
428-
case uint64:
429-
return any(Cast[Long](obj).Uint64()).(T)
430-
case uint:
431-
return any(uint(Cast[Long](obj).Uint64())).(T)
432-
case float32:
433-
return any(float32(Cast[Float](obj).Float64())).(T)
434-
case float64:
435-
return any(Cast[Float](obj).Float64()).(T)
436-
case complex64:
437-
return any(complex64(Cast[Complex](obj).Complex128())).(T)
438-
case complex128:
439-
return any(Cast[Complex](obj).Complex128()).(T)
440-
case string:
441-
return any(Cast[Str](obj).String()).(T)
442-
case bool:
443-
return any(Cast[Bool](obj).Bool()).(T)
444-
case []byte:
445-
return any(Cast[Bytes](obj).Bytes()).(T)
446-
default:
447-
v := reflect.ValueOf(ret)
448-
switch v.Kind() {
449-
case reflect.Slice:
450-
return toSlice[T](obj, v)
451-
}
452-
panic(fmt.Errorf("unsupported type conversion from Python object to %T", ret))
453-
}
454-
}
455-
456-
func toSlice[T any](obj Object, v reflect.Value) T {
457-
list := Cast[List](obj)
458-
l := list.Len()
459-
v = reflect.MakeSlice(v.Type(), l, l)
460-
for i := 0; i < l; i++ {
461-
v.Index(i).Set(reflect.ValueOf(To[T](list.GetItem(i))))
462-
}
463-
return v.Interface().(T)
464-
}
465-
466393
func fromSlice(v reflect.Value) List {
467394
l := v.Len()
468395
list := newList(C.PyList_New(C.Py_ssize_t(l)))

0 commit comments

Comments
 (0)