reflect: implement and test Value.Comparable
Этот коммит содержится в:
родитель
a11f2436e3
коммит
d4bdd836bc
3 изменённых файлов: 40 добавлений и 0 удалений
|
@ -8105,6 +8105,8 @@ func TestValue_Len(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
func TestValue_Comparable(t *testing.T) {
|
func TestValue_Comparable(t *testing.T) {
|
||||||
var a int
|
var a int
|
||||||
var s []int
|
var s []int
|
||||||
|
@ -8286,6 +8288,8 @@ func TestValue_Comparable(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
type ValueEqualTest struct {
|
type ValueEqualTest struct {
|
||||||
v, u any
|
v, u any
|
||||||
eq bool
|
eq bool
|
||||||
|
|
|
@ -958,6 +958,8 @@ func (t *rawType) Implements(u Type) bool {
|
||||||
// Comparable returns whether values of this type can be compared to each other.
|
// Comparable returns whether values of this type can be compared to each other.
|
||||||
func (t *rawType) Comparable() bool {
|
func (t *rawType) Comparable() bool {
|
||||||
switch t.Kind() {
|
switch t.Kind() {
|
||||||
|
case Invalid:
|
||||||
|
return false
|
||||||
case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
|
case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
|
||||||
return true
|
return true
|
||||||
case Float32, Float64, Complex64, Complex128:
|
case Float32, Float64, Complex64, Complex128:
|
||||||
|
|
|
@ -217,6 +217,40 @@ func (v Value) CanAddr() bool {
|
||||||
return v.flags&(valueFlagIndirect) == valueFlagIndirect
|
return v.flags&(valueFlagIndirect) == valueFlagIndirect
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v Value) Comparable() bool {
|
||||||
|
k := v.Kind()
|
||||||
|
switch k {
|
||||||
|
case Invalid:
|
||||||
|
return false
|
||||||
|
|
||||||
|
case Array:
|
||||||
|
switch v.Type().Elem().Kind() {
|
||||||
|
case Interface, Array, Struct:
|
||||||
|
for i := 0; i < v.Type().Len(); i++ {
|
||||||
|
if !v.Index(i).Comparable() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return v.Type().Comparable()
|
||||||
|
|
||||||
|
case Interface:
|
||||||
|
return v.Elem().Comparable()
|
||||||
|
|
||||||
|
case Struct:
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
if !v.Field(i).Comparable() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
|
||||||
|
default:
|
||||||
|
return v.Type().Comparable()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (v Value) Addr() Value {
|
func (v Value) Addr() Value {
|
||||||
if !v.CanAddr() {
|
if !v.CanAddr() {
|
||||||
panic("reflect.Value.Addr of unaddressable value")
|
panic("reflect.Value.Addr of unaddressable value")
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче