reflect: add CanInt() and friends and uncomments tests that pass

Этот коммит содержится в:
Damian Gryski 2023-03-24 10:43:19 -07:00 коммит произвёл Ron Evans
родитель 53b95cad08
коммит 181d2ad2b4
2 изменённых файлов: 42 добавлений и 2 удалений

Просмотреть файл

@ -430,6 +430,8 @@ func TestMapIterSet(t *testing.T) {
} }
} }
*/
func TestCanIntUintFloatComplex(t *testing.T) { func TestCanIntUintFloatComplex(t *testing.T) {
type integer int type integer int
type uinteger uint type uinteger uint
@ -617,8 +619,6 @@ func TestCanSetField(t *testing.T) {
} }
} }
*/
var _i = 7 var _i = 7
var valueToStringTests = []pair{ var valueToStringTests = []pair{

Просмотреть файл

@ -250,6 +250,16 @@ func (v Value) Bool() bool {
} }
} }
// CanInt reports whether Uint can be used without panicking.
func (v Value) CanInt() bool {
switch v.Kind() {
case Int, Int8, Int16, Int32, Int64:
return true
default:
return false
}
}
func (v Value) Int() int64 { func (v Value) Int() int64 {
switch v.Kind() { switch v.Kind() {
case Int: case Int:
@ -287,6 +297,16 @@ func (v Value) Int() int64 {
} }
} }
// CanUint reports whether Uint can be used without panicking.
func (v Value) CanUint() bool {
switch v.Kind() {
case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
return true
default:
return false
}
}
func (v Value) Uint() uint64 { func (v Value) Uint() uint64 {
switch v.Kind() { switch v.Kind() {
case Uintptr: case Uintptr:
@ -330,6 +350,16 @@ func (v Value) Uint() uint64 {
} }
} }
// CanFloat reports whether Float can be used without panicking.
func (v Value) CanFloat() bool {
switch v.Kind() {
case Float32, Float64:
return true
default:
return false
}
}
func (v Value) Float32() float32 { func (v Value) Float32() float32 {
switch v.Kind() { switch v.Kind() {
case Float32: case Float32:
@ -377,6 +407,16 @@ func (v Value) Float() float64 {
} }
} }
// CanComplex reports whether Complex can be used without panicking.
func (v Value) CanComplex() bool {
switch v.Kind() {
case Complex64, Complex128:
return true
default:
return false
}
}
func (v Value) Complex() complex128 { func (v Value) Complex() complex128 {
switch v.Kind() { switch v.Kind() {
case Complex64: case Complex64: