Skip to content

Commit d0f9c4e

Browse files
committed
Provide a helper to check registration uniqueness
This wouldn't be sound in a public package, since everyone can touch the registry map directly. However, since this is internal, I'm only doing it because I find it more readable.
1 parent 1fbe2e3 commit d0f9c4e

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

internal/store/bbolt/factory.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import (
1111
)
1212

1313
func init() {
14-
registry.Registry["bbolt"] = registry.Entry{
15-
EnvironmentKeys: []string{"DB_PATH"},
16-
FactoryFromEnv: FactoryFromEnv,
17-
}
14+
registry.Provide("bbolt", FactoryFromEnv, "DB_PATH")
1815
}
1916

2017
// FactoryFromEnv returns a store.Factory whose stores are backed by a local

internal/store/dynamodb/factory.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import (
1313
)
1414

1515
func init() {
16-
registry.Registry["dynamodb"] = registry.Entry{
17-
EnvironmentKeys: []string{"DYNAMODB", "DYNAMODB_TABLE", "DYNAMODB_ENDPOINT"},
18-
FactoryFromEnv: FactoryFromEnv,
19-
}
16+
registry.Provide("dynamodb", FactoryFromEnv,
17+
"DYNAMODB", "DYNAMODB_TABLE", "DYNAMODB_ENDPOINT")
2018
}
2119

2220
// FactoryFromEnv returns a store.Factory whose stores are backed by Amazon

internal/store/firestore/factory.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ import (
1212
)
1313

1414
func init() {
15-
registry.Registry["firestore"] = registry.Entry{
16-
EnvironmentKeys: []string{"FIRESTORE_PROJECT_ID", "FIRESTORE_DATABASE_ID"},
17-
FactoryFromEnv: FactoryFromEnv,
18-
}
15+
registry.Provide("firestore", FactoryFromEnv,
16+
"FIRESTORE_PROJECT_ID", "FIRESTORE_DATABASE_ID")
1917
}
2018

2119
// FactoryFromEnv returns a store.Factory whose stores are backed by a Google

internal/store/registry/registry.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package registry
44

55
import (
66
"context"
7+
"fmt"
78

89
"github.com/ahamlinman/randomizer/internal/randomizer"
910
)
@@ -24,3 +25,19 @@ type Entry struct {
2425
// variables.
2526
FactoryFromEnv func(context.Context) (func(partition string) randomizer.Store, error)
2627
}
28+
29+
// Provide registers a new store backend, or panics if a backend is already
30+
// registered under this name.
31+
func Provide(
32+
name string,
33+
factoryFromEnv func(context.Context) (func(partition string) randomizer.Store, error),
34+
environmentKeys ...string,
35+
) {
36+
if _, ok := Registry[name]; ok {
37+
panic(fmt.Errorf("%s already registered as a store backend", name))
38+
}
39+
Registry[name] = Entry{
40+
EnvironmentKeys: environmentKeys,
41+
FactoryFromEnv: factoryFromEnv,
42+
}
43+
}

0 commit comments

Comments
 (0)