diff --git a/pkg/epp/plugins/plugins.go b/pkg/epp/plugins/plugins.go index ac6bca9f2..2875e06da 100644 --- a/pkg/epp/plugins/plugins.go +++ b/pkg/epp/plugins/plugins.go @@ -16,7 +16,10 @@ limitations under the License. package plugins -import "context" +import ( + "context" + "fmt" +) // Plugin defines the interface for a plugin. // This interface should be embedded in all plugins across the code. @@ -50,3 +53,18 @@ type HandlePlugins interface { // GetAllPluginsWithNames returns all of the known plugins with their names GetAllPluginsWithNames() map[string]Plugin } + +// PluginByType retrieves the specified plugin by name and verifies its type +func PluginByType[P Plugin](handlePlugins HandlePlugins, name string) (P, error) { + var zero P + + rawPlugin := handlePlugins.Plugin(name) + if rawPlugin == nil { + return zero, fmt.Errorf("there is no plugin with the name '%s' defined", name) + } + thePlugin, ok := rawPlugin.(P) + if !ok { + return zero, fmt.Errorf("the plugin with the name '%s' is not an instance of %T", name, zero) + } + return thePlugin, nil +}