-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
62 lines (50 loc) · 1.53 KB
/
schema.go
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
package cfg
import (
"context"
"github.com/pkg/errors"
)
// Schemas is a named type for a map of Schemas.
type Schemas map[string]interface {
Load(context.Context) error
schema()
}
// A Schema models a configuration setting for an application.
type Schema[T any] struct {
// Dest is a required field which points where to store the configuration value when Load is called.
Dest *T
// Provider is a required field which provides the configuration value.
Provider Provider[T]
// Default is an optional field which specifies the fallback value to use if Provider returns a NoValueProvidedError.
Default *T
// Validator is an optional field which ensures the value provided by Provider is valid.
Validator Validator[T]
}
func (Schema[T]) schema() {}
// Load calls s.Provider.Provide to get the configuration value and store it into s.Dest.
// If s.Provider.Provide returns a NoValueProvidedError and s.Default is not nil, s.Default will be used instead.
// If s.Validator is set, it will be used to validate the provided value.
func (s Schema[T]) Load(ctx context.Context) error {
if s.Dest == nil {
return errors.New("dest is nil")
}
if s.Provider == nil {
return errors.New("provider is nil")
}
val, err := s.Provider.Provide(ctx)
if err != nil {
if !errors.Is(err, NoValueProvidedError) {
return err
}
if s.Default == nil {
return err
}
val = *s.Default
}
if s.Validator != nil {
if err := s.Validator.Validate(val); err != nil {
return errors.Wrapf(err, "validation failed")
}
}
*s.Dest = val
return nil
}