Skip to content

Commit 3704775

Browse files
authored
Merge pull request #18 from PowerDNS/functional-options
GetBackend with functional params instead of GetBackendWithParams
2 parents f135622 + e398be7 commit 3704775

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Interface interface {
2424
To instantiate a backend, `_`-import all the backends that you want to register, and call:
2525

2626
```go
27-
func GetBackendWithParams(ctx context.Context, typeName string, params InitParams) (Interface, error)
27+
func GetBackend(ctx context.Context, typeName string, options map[string]any, params ...Param) (Interface, error)
2828
```
2929

3030
An example can be found in `example_test.go`.

example_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ func Example() {
1515
// Do not forget the:
1616
// import _ "github.com/PowerDNS/simpleblob/backends/memory"
1717
ctx := context.Background()
18-
storage, err := simpleblob.GetBackendWithParams(ctx, "memory", simpleblob.InitParams{
19-
OptionMap: map[string]interface{}{}, // add key-value options here
20-
Logger: logr.Discard(), // replace with a real logger
21-
})
18+
storage, err := simpleblob.GetBackend(
19+
ctx,
20+
"memory",
21+
map[string]interface{}{
22+
// add key-value options here
23+
"foo": "example",
24+
},
25+
simpleblob.WithLogger(logr.Discard()), // replace with a real logger
26+
)
2227
check(err)
2328
err = storage.Store(ctx, "example.txt", []byte("hello"))
2429
check(err)

plugins.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ func (ip InitParams) OptionsThroughYAML(dest interface{}) error {
5555
return nil
5656
}
5757

58+
// Param is the type of extra init parameters. It is returned by
59+
// calling functional params like WithLogger.
60+
type Param func(ip *InitParams)
61+
62+
// WithLogger is a GetBackend parameter that sets the logr.Logger to use in the
63+
// backends.
64+
func WithLogger(log logr.Logger) Param {
65+
return func(ip *InitParams) {
66+
ip.Logger = log
67+
}
68+
}
69+
5870
// backends is the internal backend registry
5971
var (
6072
mu sync.Mutex
@@ -70,33 +82,15 @@ func RegisterBackend(typeName string, initFunc InitFunc) {
7082

7183
// GetBackend creates a new backend instance of given typeName. This type must
7284
// have been previously registered with RegisterBackend.
73-
// The options map contains backend dependant key-value options. Some backends
74-
// take no options, others require some specific options.
75-
// The lifetime of the context passed in must span the lifetime of the whole
76-
// backend instance, not just the init time, so do not set any timeout on it!
77-
//
78-
// Deprecated: consider switching to GetBackendWithParams
79-
func GetBackend(ctx context.Context, typeName string, options map[string]interface{}) (Interface, error) {
80-
p := InitParams{OptionMap: options}
81-
return GetBackendWithParams(ctx, typeName, p)
82-
}
83-
84-
// GetBackendWithParams creates a new backend instance of given typeName. This type must
85-
// have been previously registered with RegisterBackend.
86-
// Unlike the old GetBackend, this directly accepts an InitParams struct which allows
87-
// us to add more options on the future.
88-
//
89-
// One notable addition is the InitParams.Logger field that passes a logr.Logger
90-
// to the backend.
9185
//
9286
// The options map contains backend dependant key-value options. Some backends
9387
// take no options, others require some specific options.
9488
//
89+
// Additional parameters can be passed with extra arguments, like WithLogger.
90+
//
9591
// The lifetime of the context passed in must span the lifetime of the whole
9692
// backend instance, not just the init time, so do not set any timeout on it!
97-
// TODO: the context lifetime requirement is perhaps error prone and this does
98-
// not allow setting an init timeout. Not sure what would be a good solution.
99-
func GetBackendWithParams(ctx context.Context, typeName string, params InitParams) (Interface, error) {
93+
func GetBackend(ctx context.Context, typeName string, options OptionMap, params ...Param) (Interface, error) {
10094
if typeName == "" {
10195
return nil, fmt.Errorf("no storage.type configured")
10296
}
@@ -106,8 +100,12 @@ func GetBackendWithParams(ctx context.Context, typeName string, params InitParam
106100
if !exists {
107101
return nil, fmt.Errorf("storage.type %q not found or registered", typeName)
108102
}
109-
if params.Logger.GetSink() == nil {
110-
params.Logger = logr.Discard()
103+
p := InitParams{OptionMap: options}
104+
for _, param := range params {
105+
param(&p)
106+
}
107+
if p.Logger.GetSink() == nil {
108+
p.Logger = logr.Discard()
111109
}
112-
return initFunc(ctx, params)
110+
return initFunc(ctx, p)
113111
}

0 commit comments

Comments
 (0)