-
Notifications
You must be signed in to change notification settings - Fork 9
/
option.go
87 lines (75 loc) · 2.5 KB
/
option.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2024 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
package konf
import (
"log/slog"
"github.com/nil-go/konf/internal/convert"
)
// WithDelimiter provides the delimiter used when specifying config paths.
// The delimiter is used to separate keys in the path.
//
// For example, with the default delimiter `.`, a config path might look like `parent.child.key`.
func WithDelimiter(delimiter string) Option {
return func(options *options) {
options.delimiter = delimiter
}
}
// WithTagName provides the tag name that reads for field names.
// The tag name is used when decoding configuration into structs.
//
// For example, with the default tag name `konf`, it would look for `konf` tags on struct fields.
func WithTagName(tagName string) Option {
return func(options *options) {
options.tagName = tagName
}
}
// WithDecodeHook provides the decode hook for decoding.
// The decode hook is a function that can customize how configuration are decoded.
//
// It can be either `func(F) (T, error)` which returns the converted value,
// or `func(F, T) error` which sets the converted value inline.
//
// By default, it composes string to time.Duration, string to []string split by `,`
// and string to encoding.TextUnmarshaler.
func WithDecodeHook[F, T any, FN func(F) (T, error) | func(F, T) error](hook FN) Option {
return func(options *options) {
options.hooks = append(options.hooks, convert.WithHook[F, T](hook))
}
}
// WithLogHandler provides the slog.Handler for logs from watch.
//
// By default, it uses handler from slog.Default().
func WithLogHandler(handler slog.Handler) Option {
return func(options *options) {
if handler != nil {
options.logger = slog.New(handler)
}
}
}
// WithOnStatus provides the callback for monitoring status of configuration loading/watching.
func WithOnStatus(onStatus func(loader Loader, changed bool, err error)) Option {
return func(options *options) {
options.onStatus = onStatus
}
}
// WithCaseSensitive enables the case sensitivity of the configuration keys.
func WithCaseSensitive() Option {
return func(options *options) {
options.caseSensitive = true
}
}
// WithMapKeyCaseSensitive enables the case sensitivity of the map keys.
func WithMapKeyCaseSensitive() Option {
return func(options *options) {
options.mapKeyCaseSensitive = true
}
}
type (
// Option configures a Config with specific options.
Option func(*options)
options struct {
Config
tagName string
hooks []convert.Option
}
)