-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
80 lines (65 loc) · 2.2 KB
/
provider.go
File metadata and controls
80 lines (65 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cfg
import (
"context"
"reflect"
"github.com/pkg/errors"
)
// A Provider loads a configuration value from a predetermined source.
// If no value is provided by the underlying source, the Provider must return a NoValueProvidedError.
type Provider[T any] interface {
Provide(ctx context.Context) (T, error)
}
// The ProviderFunc is an adapter type which allows ordinary functions to be used as Providers.
type ProviderFunc[T any] func(ctx context.Context) (T, error)
// Provide calls f(ctx).
func (f ProviderFunc[T]) Provide(ctx context.Context) (T, error) {
return f(ctx)
}
// MultiProvider allows a slice of Provider[T] to be used as a Provider[T].
type MultiProvider[T any] []Provider[T]
// Provide iterates through each provider in m and returns the first value given by a provider.
// If a provider returns a NoValueProvidedError, the iteration continues.
// If no providers return a value, a NoValueProvidedError is returned.
func (m MultiProvider[T]) Provide(ctx context.Context) (T, error) {
var t T
for _, p := range m {
val, err := p.Provide(ctx)
if err != nil {
if errors.Is(err, NoValueProvidedError) {
continue
}
return t, err
}
return val, nil
}
return t, NoValueProvidedError
}
// StaticProvider adapts v into a Provider[T].
// If allowZero is false, the Provider will return a NoValueProvidedError if v is the zero value.
//
// var p Provider[int] = StaticProvider(5)
func StaticProvider[T any](v T, allowZero bool) Provider[T] {
return ProviderFunc[T](func(ctx context.Context) (T, error) {
if !allowZero && reflect.ValueOf(v).IsZero() {
return v, NoValueProvidedError
}
return v, nil
})
}
// StaticProviderAddr adapts pv into a Provider[T].
// If pv is nil, the Provider will return a NoValueProvidedError.
// If allowZero is false, the Provider will return a NoValueProvidedError if v is the zero value.
//
// var p Provider[int] = StaticProviderAddr(&addr)
func StaticProviderAddr[T any](pv *T, allowZero bool) Provider[T] {
return ProviderFunc[T](func(ctx context.Context) (T, error) {
var zero T
if pv == nil {
return zero, NoValueProvidedError
}
if !allowZero && reflect.ValueOf(*pv).IsZero() {
return zero, NoValueProvidedError
}
return *pv, nil
})
}