Jelajahi Sumber

Add panics for potential nil types from reflect

Signed-off-by: Matt Bolt <mbolt35@gmail.com>
Matt Bolt 2 tahun lalu
induk
melakukan
76c8d95004
1 mengubah file dengan 11 tambahan dan 1 penghapusan
  1. 11 1
      core/pkg/util/typeutil/typeutil.go

+ 11 - 1
core/pkg/util/typeutil/typeutil.go

@@ -22,7 +22,7 @@ func TypeOf[T any]() string {
 
 	// this should not be possible, but in the event that it does, we want to be loud about it
 	if t == nil {
-		panic(fmt.Sprintf("Unable to generate a key for type: %+v", reflect.TypeFor[T]()))
+		panic(fmt.Sprintf("failed to locate non-pointer type: %+v", reflect.TypeFor[T]()))
 	}
 
 	// combine the prefix, package path, and the type name
@@ -37,6 +37,11 @@ func PackageOf[T any]() string {
 		t = t.Elem()
 	}
 
+	// this should not be possible, but in the event that it does, we want to be loud about it
+	if t == nil {
+		panic(fmt.Sprintf("failed to locate package for: %+v", reflect.TypeFor[T]()))
+	}
+
 	return t.PkgPath()
 }
 
@@ -48,6 +53,11 @@ func PackageFor(value any) string {
 		t = t.Elem()
 	}
 
+	// this should not be possible, but in the event that it does, we want to be loud about it
+	if t == nil {
+		panic(fmt.Sprintf("failed to locate package for: %+v", reflect.TypeOf(value)))
+	}
+
 	return t.PkgPath()
 }