Skip to content

Commit 06d612a

Browse files
committed
Reorganize file for sections to be more logically distinct
1 parent e1b5e06 commit 06d612a

1 file changed

Lines changed: 79 additions & 71 deletions

File tree

internal/pluginlookup/plugin_lookup.go

Lines changed: 79 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ const (
2424
PluginKindMiddlewareWorker PluginKind = "middleware_worker"
2525
)
2626

27-
//
28-
// PluginLookupInterface
29-
//
30-
31-
// PluginLookupInterface looks up plugins by kind. It's commonly implemented by
32-
// PluginLookup, but may also be EmptyPluginLookup as a memory allocation
33-
// optimization for bundles where no plugins are present.
34-
type PluginLookupInterface interface {
35-
ByKind(kind PluginKind) []rivertype.Plugin
36-
}
37-
3827
// InitBaseServices initializes base services embedded in plugins, including
3928
// those hidden behind wrappers for legacy hooks and middleware.
4029
func InitBaseServices(archetype *baseservice.Archetype, plugins []rivertype.Plugin) {
@@ -50,50 +39,6 @@ func InitBaseServices(archetype *baseservice.Archetype, plugins []rivertype.Plug
5039
}
5140
}
5241

53-
// NewPluginLookup returns a new plugin lookup interface based on the given
54-
// plugins that satisfies PluginLookupInterface. This is often pluginLookup,
55-
// but may be emptyPluginLookup as an optimization for the common case of an
56-
// empty plugin bundle.
57-
func NewPluginLookup(plugins []rivertype.Plugin) PluginLookupInterface {
58-
if len(plugins) < 1 {
59-
return &emptyPluginLookup{}
60-
}
61-
62-
pluginsByKind := make(map[PluginKind][]rivertype.Plugin)
63-
64-
for _, plugin := range plugins {
65-
if plugin == nil {
66-
continue
67-
}
68-
69-
extension := any(plugin)
70-
if legacyPlugin, ok := plugin.(*legacyPlugin); ok {
71-
extension = legacyPlugin.extension
72-
}
73-
74-
if _, ok := extension.(rivertype.HookInsertBegin); ok {
75-
pluginsByKind[PluginKindHookInsertBegin] = append(pluginsByKind[PluginKindHookInsertBegin], plugin)
76-
}
77-
if _, ok := extension.(rivertype.HookPeriodicJobsStart); ok {
78-
pluginsByKind[PluginKindHookPeriodicJobsStart] = append(pluginsByKind[PluginKindHookPeriodicJobsStart], plugin)
79-
}
80-
if _, ok := extension.(rivertype.HookWorkBegin); ok {
81-
pluginsByKind[PluginKindHookWorkBegin] = append(pluginsByKind[PluginKindHookWorkBegin], plugin)
82-
}
83-
if _, ok := extension.(rivertype.HookWorkEnd); ok {
84-
pluginsByKind[PluginKindHookWorkEnd] = append(pluginsByKind[PluginKindHookWorkEnd], plugin)
85-
}
86-
if _, ok := extension.(rivertype.JobInsertMiddleware); ok {
87-
pluginsByKind[PluginKindMiddlewareJobInsert] = append(pluginsByKind[PluginKindMiddlewareJobInsert], plugin)
88-
}
89-
if _, ok := extension.(rivertype.WorkerMiddleware); ok {
90-
pluginsByKind[PluginKindMiddlewareWorker] = append(pluginsByKind[PluginKindMiddlewareWorker], plugin)
91-
}
92-
}
93-
94-
return &pluginLookup{pluginsByKind: pluginsByKind}
95-
}
96-
9742
// NormalizePlugins converts hook, middleware, and plugin registrations into a
9843
// single plugin slice while preserving legacy hook and middleware registrations
9944
// that don't yet opt into Plugin.
@@ -157,27 +102,59 @@ func NormalizePlugins(hooks []rivertype.Hook, middlewares []rivertype.Middleware
157102
return normalizedPlugins
158103
}
159104

160-
// pluginPointerIdentity identifies a non-zero-sized pointer-backed extension so
161-
// NormalizePlugins can collapse the same instance registered through multiple
162-
// plugin, hook, or middleware config fields. Zero-sized pointers are excluded
163-
// because Go allows distinct zero-sized values to have the same address.
164-
type pluginPointerIdentity struct {
165-
pointer uintptr
166-
typeOf reflect.Type
105+
//
106+
// PluginLookupInterface
107+
//
108+
109+
// PluginLookupInterface looks up plugins by kind. It's commonly implemented by
110+
// PluginLookup, but may also be EmptyPluginLookup as a memory allocation
111+
// optimization for bundles where no plugins are present.
112+
type PluginLookupInterface interface {
113+
ByKind(kind PluginKind) []rivertype.Plugin
167114
}
168115

169-
func pluginPointerIdentityFor(plugin any) (pluginPointerIdentity, bool) {
170-
value := reflect.ValueOf(plugin)
171-
if !value.IsValid() || value.Kind() != reflect.Ptr || value.Type().Elem().Size() == 0 {
172-
return pluginPointerIdentity{}, false
116+
// NewPluginLookup returns a new plugin lookup interface based on the given
117+
// plugins that satisfies PluginLookupInterface. This is often pluginLookup,
118+
// but may be emptyPluginLookup as an optimization for the common case of an
119+
// empty plugin bundle.
120+
func NewPluginLookup(plugins []rivertype.Plugin) PluginLookupInterface {
121+
if len(plugins) < 1 {
122+
return &emptyPluginLookup{}
173123
}
174124

175-
return pluginPointerIdentity{pointer: value.Pointer(), typeOf: value.Type()}, true
176-
}
125+
pluginsByKind := make(map[PluginKind][]rivertype.Plugin)
177126

178-
type pluginRegistration struct {
179-
original any
180-
plugin rivertype.Plugin
127+
for _, plugin := range plugins {
128+
if plugin == nil {
129+
continue
130+
}
131+
132+
extension := any(plugin)
133+
if legacyPlugin, ok := plugin.(*legacyPlugin); ok {
134+
extension = legacyPlugin.extension
135+
}
136+
137+
if _, ok := extension.(rivertype.HookInsertBegin); ok {
138+
pluginsByKind[PluginKindHookInsertBegin] = append(pluginsByKind[PluginKindHookInsertBegin], plugin)
139+
}
140+
if _, ok := extension.(rivertype.HookPeriodicJobsStart); ok {
141+
pluginsByKind[PluginKindHookPeriodicJobsStart] = append(pluginsByKind[PluginKindHookPeriodicJobsStart], plugin)
142+
}
143+
if _, ok := extension.(rivertype.HookWorkBegin); ok {
144+
pluginsByKind[PluginKindHookWorkBegin] = append(pluginsByKind[PluginKindHookWorkBegin], plugin)
145+
}
146+
if _, ok := extension.(rivertype.HookWorkEnd); ok {
147+
pluginsByKind[PluginKindHookWorkEnd] = append(pluginsByKind[PluginKindHookWorkEnd], plugin)
148+
}
149+
if _, ok := extension.(rivertype.JobInsertMiddleware); ok {
150+
pluginsByKind[PluginKindMiddlewareJobInsert] = append(pluginsByKind[PluginKindMiddlewareJobInsert], plugin)
151+
}
152+
if _, ok := extension.(rivertype.WorkerMiddleware); ok {
153+
pluginsByKind[PluginKindMiddlewareWorker] = append(pluginsByKind[PluginKindMiddlewareWorker], plugin)
154+
}
155+
}
156+
157+
return &pluginLookup{pluginsByKind: pluginsByKind}
181158
}
182159

183160
//
@@ -285,6 +262,37 @@ func (p *legacyPlugin) WorkEnd(ctx context.Context, job *rivertype.JobRow, err e
285262
return hook.WorkEnd(ctx, job, err)
286263
}
287264

265+
//
266+
// pluginPointerIdentity
267+
//
268+
269+
// pluginPointerIdentity identifies a non-zero-sized pointer-backed extension so
270+
// NormalizePlugins can collapse the same instance registered through multiple
271+
// plugin, hook, or middleware config fields. Zero-sized pointers are excluded
272+
// because Go allows distinct zero-sized values to have the same address.
273+
type pluginPointerIdentity struct {
274+
pointer uintptr
275+
typeOf reflect.Type
276+
}
277+
278+
func pluginPointerIdentityFor(plugin any) (pluginPointerIdentity, bool) {
279+
value := reflect.ValueOf(plugin)
280+
if !value.IsValid() || value.Kind() != reflect.Ptr || value.Type().Elem().Size() == 0 {
281+
return pluginPointerIdentity{}, false
282+
}
283+
284+
return pluginPointerIdentity{pointer: value.Pointer(), typeOf: value.Type()}, true
285+
}
286+
287+
//
288+
// pluginRegistration
289+
//
290+
291+
type pluginRegistration struct {
292+
original any
293+
plugin rivertype.Plugin
294+
}
295+
288296
//
289297
// JobPluginLookup
290298
//

0 commit comments

Comments
 (0)