From 792716eefc8f59725733bc33a547116a1a681d66 Mon Sep 17 00:00:00 2001 From: visualfc Date: Thu, 14 Nov 2024 10:25:23 +0800 Subject: [PATCH] internal/lib/reflect: Value.SetZero --- internal/lib/reflect/value.go | 101 +++++++++++++++++----------------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/internal/lib/reflect/value.go b/internal/lib/reflect/value.go index ea6641c6..e48b805f 100644 --- a/internal/lib/reflect/value.go +++ b/internal/lib/reflect/value.go @@ -908,58 +908,55 @@ func (v Value) IsZero() bool { // SetZero sets v to be the zero value of v's type. // It panics if CanSet returns false. func (v Value) SetZero() { - /* - v.mustBeAssignable() - switch v.kind() { - case Bool: - *(*bool)(v.ptr) = false - case Int: - *(*int)(v.ptr) = 0 - case Int8: - *(*int8)(v.ptr) = 0 - case Int16: - *(*int16)(v.ptr) = 0 - case Int32: - *(*int32)(v.ptr) = 0 - case Int64: - *(*int64)(v.ptr) = 0 - case Uint: - *(*uint)(v.ptr) = 0 - case Uint8: - *(*uint8)(v.ptr) = 0 - case Uint16: - *(*uint16)(v.ptr) = 0 - case Uint32: - *(*uint32)(v.ptr) = 0 - case Uint64: - *(*uint64)(v.ptr) = 0 - case Uintptr: - *(*uintptr)(v.ptr) = 0 - case Float32: - *(*float32)(v.ptr) = 0 - case Float64: - *(*float64)(v.ptr) = 0 - case Complex64: - *(*complex64)(v.ptr) = 0 - case Complex128: - *(*complex128)(v.ptr) = 0 - case String: - *(*string)(v.ptr) = "" - case Slice: - *(*unsafeheader.Slice)(v.ptr) = unsafeheader.Slice{} - case Interface: - *(*[2]unsafe.Pointer)(v.ptr) = [2]unsafe.Pointer{} - case Chan, Func, Map, Pointer, UnsafePointer: - *(*unsafe.Pointer)(v.ptr) = nil - case Array, Struct: - typedmemclr(v.typ(), v.ptr) - default: - // This should never happen, but will act as a safeguard for later, - // as a default value doesn't makes sense here. - panic(&ValueError{"reflect.Value.SetZero", v.Kind()}) - } - */ - panic("todo: reflect.Value.SetZero") + v.mustBeAssignable() + switch v.kind() { + case Bool: + *(*bool)(v.ptr) = false + case Int: + *(*int)(v.ptr) = 0 + case Int8: + *(*int8)(v.ptr) = 0 + case Int16: + *(*int16)(v.ptr) = 0 + case Int32: + *(*int32)(v.ptr) = 0 + case Int64: + *(*int64)(v.ptr) = 0 + case Uint: + *(*uint)(v.ptr) = 0 + case Uint8: + *(*uint8)(v.ptr) = 0 + case Uint16: + *(*uint16)(v.ptr) = 0 + case Uint32: + *(*uint32)(v.ptr) = 0 + case Uint64: + *(*uint64)(v.ptr) = 0 + case Uintptr: + *(*uintptr)(v.ptr) = 0 + case Float32: + *(*float32)(v.ptr) = 0 + case Float64: + *(*float64)(v.ptr) = 0 + case Complex64: + *(*complex64)(v.ptr) = 0 + case Complex128: + *(*complex128)(v.ptr) = 0 + case String: + *(*string)(v.ptr) = "" + case Slice: + *(*unsafeheaderSlice)(v.ptr) = unsafeheaderSlice{} + case Interface: + *(*[2]unsafe.Pointer)(v.ptr) = [2]unsafe.Pointer{} + case Chan, Func, Map, Pointer, UnsafePointer: + *(*unsafe.Pointer)(v.ptr) = nil + case Array, Struct: + typedmemclr(v.typ(), v.ptr) + default: + // This should never happen, but will act as a safeguard for later, + // as a default value doesn't makes sense here. + panic(&ValueError{"reflect.Value.SetZero", v.Kind()}) + } } // Kind returns v's Kind.