Skip to content

Commit cc7b82b

Browse files
committed
Cleanup plugin interface
Separate Get for set and init context, which are used differently. Ensure init context always returns instances and does not return an underlying map. Signed-off-by: Derek McGowan <derek@mcg.dev>
1 parent 832b974 commit cc7b82b

1 file changed

Lines changed: 29 additions & 20 deletions

File tree

context.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ func NewContext(ctx context.Context, plugins *Set, properties map[string]string)
5151
}
5252
}
5353

54-
// Get returns the first plugin by its type
55-
func (i *InitContext) Get(t Type) (interface{}, error) {
56-
return i.plugins.Get(t)
57-
}
58-
5954
// Meta contains information gathered from the registration and initialization
6055
// process.
6156
type Meta struct {
@@ -119,19 +114,28 @@ func (ps *Set) Add(p *Plugin) error {
119114
return nil
120115
}
121116

122-
// Get returns the first plugin by its type
123-
func (ps *Set) Get(t Type) (interface{}, error) {
124-
for _, v := range ps.byTypeAndID[t] {
125-
return v.Instance()
117+
// Get returns the plugin with the given type and id
118+
func (ps *Set) Get(t Type, id string) *Plugin {
119+
p, ok := ps.byTypeAndID[t]
120+
if !ok {
121+
return nil
126122
}
127-
return nil, fmt.Errorf("no plugins registered for %s: %w", t, ErrPluginNotFound)
123+
return p[id]
128124
}
129125

130126
// GetAll returns all initialized plugins
131127
func (ps *Set) GetAll() []*Plugin {
132128
return ps.ordered
133129
}
134130

131+
// Get returns the first plugin by its type
132+
func (i *InitContext) Get(t Type) (interface{}, error) {
133+
for _, v := range i.plugins.byTypeAndID[t] {
134+
return v.Instance()
135+
}
136+
return nil, fmt.Errorf("no plugins registered for %s: %w", t, ErrPluginNotFound)
137+
}
138+
135139
// Plugins returns plugin set
136140
func (i *InitContext) Plugins() *Set {
137141
return i.plugins
@@ -144,23 +148,28 @@ func (i *InitContext) GetAll() []*Plugin {
144148

145149
// GetByID returns the plugin of the given type and ID
146150
func (i *InitContext) GetByID(t Type, id string) (interface{}, error) {
147-
ps, err := i.GetByType(t)
148-
if err != nil {
149-
return nil, err
150-
}
151-
p, ok := ps[id]
152-
if !ok {
153-
return nil, fmt.Errorf("no %s plugins with id %s: %w", t, id, ErrPluginNotFound)
151+
p := i.plugins.Get(t, id)
152+
if p == nil {
153+
return nil, fmt.Errorf("no plugins registered for %s.%s: %w", t, id, ErrPluginNotFound)
154154
}
155155
return p.Instance()
156156
}
157157

158158
// GetByType returns all plugins with the specific type.
159-
func (i *InitContext) GetByType(t Type) (map[string]*Plugin, error) {
160-
p, ok := i.plugins.byTypeAndID[t]
159+
func (i *InitContext) GetByType(t Type) (map[string]interface{}, error) {
160+
pt, ok := i.plugins.byTypeAndID[t]
161161
if !ok {
162162
return nil, fmt.Errorf("no plugins registered for %s: %w", t, ErrPluginNotFound)
163163
}
164164

165-
return p, nil
165+
pi := make(map[string]interface{}, len(pt))
166+
for id, p := range pt {
167+
i, err := p.Instance()
168+
if err != nil {
169+
return nil, err
170+
}
171+
pi[id] = i
172+
}
173+
174+
return pi, nil
166175
}

0 commit comments

Comments
 (0)