-
Notifications
You must be signed in to change notification settings - Fork 48
Don't use globals for handling components and platforms registration #597
Comments
IMO we should rethink the whole notion of "registration". AFAICT the only advantage of the registration pattern is that when adding a new implementation you don't have to add it in some "central" location. The downsides are many though:
That said, what alternative do you have in mind @invidian? The simplest approach is probably to have a slice of strings which represents all known platforms/components. This slice can then be used in a |
I personally prefer map + lookup by key, as it's more compact than a However, I don't think constructing structs on our own in central place is a good approach, because right now we have this So I'd do something like |
As you know I'd rather be a bit more elaborate but clearer than compact and clever. A map of functions feels very "clever" to me and isn't something I'd expect to encounter as a developer who is new to the codebase. Anyway, we can discuss things in more detail once there is something implemented since then the pros/cons of the chosen approach would be clearer. |
So, what I propose is the following: package components
import (
"fmt"
"github.com/kinvolk/lokomotive/pkg/components/openebs"
"github.com/kinvolk/lokomotive/pkg/components/dex"
)
func DefaultConfig(name string) (Component, error) {
c := map[string]func() Component {
openebs.Name: openebs.DefaultConfig,
dex.Name: dex.DefaultConfig,
// We could also call config here, but then it will be called for all components.
// dex.Name: dex.DefaultConfig(),
}
if f, ok := c[name]; ok {
return f()
}
return fmt.Errorf("component %q not found")
} What I understand @johananl proposes is the following (if I'm mistaken, please edit this comment): package components
import (
"fmt"
"github.com/kinvolk/lokomotive/pkg/components/openebs"
"github.com/kinvolk/lokomotive/pkg/components/dex"
)
func DefaultConfig(name string) (Component, error) {
switch name {
case openebs.Name:
return openebs.DefaultConfig()
case dex.Name:
return dex.DefaultConfig()
default:
return fmt.Errorf("component %q not found")
}
//panic("should never happen")
return fmt.Errorf("should never happen")
} I think what we agree on is the following:
And arguments for using map over switch/case statement:
|
Created PR #1147 to definitely address this. |
The overhead of adding them is very little if we use the implementation, which do not use globals.
Similar to #596.
The text was updated successfully, but these errors were encountered: