瀏覽代碼

修改bug

yjp 6 月之前
父節點
當前提交
8c6020942e
共有 2 個文件被更改,包括 9 次插入51 次删除
  1. 0 28
      reflectutils/type.go
  2. 9 23
      reflectutils/value.go

+ 0 - 28
reflectutils/type.go

@@ -71,31 +71,3 @@ func IsTypeTime(t reflect.Type) bool {
 func IsTypeTimePointer(t reflect.Type) bool {
 	return t.Kind() == reflect.Pointer && IsTypeTime(t.Elem())
 }
-
-func ZeroType(t reflect.Type) reflect.Value {
-	zeroValue := reflect.New(t).Elem()
-	if zeroValue.Kind() != reflect.Pointer {
-		zero(&zeroValue)
-		return zeroValue
-	}
-
-	zeroValue.Set(reflect.New(zeroValue.Type().Elem()))
-	elemValue := PointerValueElem(zeroValue)
-	zero(&elemValue)
-
-	return zeroValue
-}
-
-func ZeroTypeToAny(t reflect.Type) any {
-	zeroValue := reflect.New(t).Elem()
-	if zeroValue.Kind() != reflect.Pointer {
-		zero(&zeroValue)
-		return zeroValue.Interface()
-	}
-
-	zeroValue.Set(reflect.New(zeroValue.Type().Elem()))
-	elemValue := PointerValueElem(zeroValue)
-	zero(&elemValue)
-
-	return zeroValue.Interface()
-}

+ 9 - 23
reflectutils/value.go

@@ -355,36 +355,22 @@ func ToFloat64(data any) (float64, error) {
 }
 
 func Zero[T any]() T {
-	var zeroAny T
+	var zeroT T
 
-	zeroValue := reflect.New(reflect.TypeOf(zeroAny)).Elem()
-	if zeroValue.Kind() != reflect.Pointer {
-		zero(&zeroValue)
-		return zeroValue.Interface().(T)
+	zeroAnyValue := reflect.ValueOf(zeroT)
+	zeroAny := ZeroValueToAny(zeroAnyValue)
+	if zeroAny == nil {
+		return zeroT
 	}
 
-	zeroValue.Set(reflect.New(zeroValue.Type().Elem()))
-	elemValue := PointerValueElem(zeroValue)
-	zero(&elemValue)
-
-	return zeroValue.Interface().(T)
+	return zeroAny.(T)
 }
 
-func ZeroValue(v reflect.Value) reflect.Value {
-	zeroValue := reflect.New(v.Type()).Elem()
-	if zeroValue.Kind() != reflect.Pointer {
-		zero(&zeroValue)
-		return zeroValue
+func ZeroValueToAny(v reflect.Value) any {
+	if v.Kind() == reflect.Invalid {
+		return nil
 	}
 
-	zeroValue.Set(reflect.New(zeroValue.Type().Elem()))
-	elemValue := PointerValueElem(zeroValue)
-	zero(&elemValue)
-
-	return zeroValue
-}
-
-func ZeroValueToAny(v reflect.Value) any {
 	zeroValue := reflect.New(v.Type()).Elem()
 	if zeroValue.Kind() != reflect.Pointer {
 		zero(&zeroValue)