File tree Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change 2828// }
2929package pluggable
3030
31- import "fmt"
31+ import (
32+ "fmt"
33+
34+ . "github.com/majewsky/gg/option"
35+ )
3236
3337// Plugin is the base interface for plugins that type Registry can instantiate.
3438type Plugin interface {
@@ -74,10 +78,15 @@ func (r *Registry[T]) Add(factory func() T) {
7478// Since T is usually an application-specific interface type, this means that
7579// nil will be returned.
7680func (r * Registry [T ]) Instantiate (pluginTypeID string ) T {
81+ var zero T
82+ return r .TryInstantiate (pluginTypeID ).UnwrapOr (zero )
83+ }
84+
85+ // TryInstantiate is like Instantiate, but returns None instead of nil for unknown plugins.
86+ func (r * Registry [T ]) TryInstantiate (pluginTypeID string ) Option [T ] {
7787 factory , exists := r .factories [pluginTypeID ]
7888 if exists {
79- return factory ()
89+ return Some ( factory () )
8090 }
81- var zero T
82- return zero
91+ return None [T ]()
8392}
You can’t perform that action at this time.
0 commit comments