Skip to content

Commit 5c851eb

Browse files
authored
Added a helper function to get referenced plugins by type (#1091)
* Added a helper function to get referenced plugins by type Signed-off-by: Shmuel Kallner <[email protected]> * Added a function comment Signed-off-by: Shmuel Kallner <[email protected]> * Update from review comment Signed-off-by: Shmuel Kallner <[email protected]> * Updates from review comments Signed-off-by: Shmuel Kallner <[email protected]> --------- Signed-off-by: Shmuel Kallner <[email protected]>
1 parent 8c1ef1f commit 5c851eb

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

pkg/epp/plugins/plugins.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ limitations under the License.
1616

1717
package plugins
1818

19-
import "context"
19+
import (
20+
"context"
21+
"fmt"
22+
)
2023

2124
// Plugin defines the interface for a plugin.
2225
// This interface should be embedded in all plugins across the code.
@@ -50,3 +53,18 @@ type HandlePlugins interface {
5053
// GetAllPluginsWithNames returns all of the known plugins with their names
5154
GetAllPluginsWithNames() map[string]Plugin
5255
}
56+
57+
// PluginByType retrieves the specified plugin by name and verifies its type
58+
func PluginByType[P Plugin](handlePlugins HandlePlugins, name string) (P, error) {
59+
var zero P
60+
61+
rawPlugin := handlePlugins.Plugin(name)
62+
if rawPlugin == nil {
63+
return zero, fmt.Errorf("there is no plugin with the name '%s' defined", name)
64+
}
65+
thePlugin, ok := rawPlugin.(P)
66+
if !ok {
67+
return zero, fmt.Errorf("the plugin with the name '%s' is not an instance of %T", name, zero)
68+
}
69+
return thePlugin, nil
70+
}

0 commit comments

Comments
 (0)