-
Notifications
You must be signed in to change notification settings - Fork 300
Added a helper function to get referenced plugins by type #1091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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](handle Handle, name string) (P, error) { | ||||||
| var zero P | ||||||
|
|
||||||
| rawPlugin := handle.Plugins().Plugin(name) | ||||||
| if rawPlugin == nil { | ||||||
| return zero, fmt.Errorf("there was no plugin with the name '%s' defined", name) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit1:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||||||
| } | ||||||
| thePlugin, ok := rawPlugin.(P) | ||||||
| if !ok { | ||||||
| return zero, fmt.Errorf("the plugin with the name '%s' was not an instance of %T", name, zero) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit2:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||||||
| } | ||||||
| return thePlugin, nil | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good, can we pass plugins instead of handle as arg?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done