ソースを参照

Add panics for potential nil types from reflect

Signed-off-by: Matt Bolt <mbolt35@gmail.com>
Matt Bolt 2 年 前
コミット
76c8d95004
1 ファイル変更11 行追加1 行削除
  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
 	// this should not be possible, but in the event that it does, we want to be loud about it
 	if t == nil {
 	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
 	// combine the prefix, package path, and the type name
@@ -37,6 +37,11 @@ func PackageOf[T any]() string {
 		t = t.Elem()
 		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()
 	return t.PkgPath()
 }
 }
 
 
@@ -48,6 +53,11 @@ func PackageFor(value any) string {
 		t = t.Elem()
 		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()
 	return t.PkgPath()
 }
 }