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

This commit is contained in:
Damian Gryski
2023-03-24 10:43:19 -07:00
committed by Ron Evans
parent 53b95cad08
commit 181d2ad2b4
2 changed files with 42 additions and 2 deletions
+2 -2
View File
@@ -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{
+40
View File
@@ -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: