Repository navigation
Expand file tree
/
Copy pathtypes.go
More file actions
124 lines (106 loc) · 3.66 KB
/
Copy pathtypes.go
File metadata and controls
124 lines (106 loc) · 3.66 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package tinyconf
import (
"context"
"errors"
"fmt"
"strings"
"github.com/insei/fmap/v3"
)
// MergeTag is the struct tag of the slices and maps that are merged from several drivers: merge:"true".
// Slices of the next drivers are appended, maps of the next drivers add and override the keys.
// Without the tag the value of the last driver replaces the whole slice or map.
const MergeTag = "merge"
var (
ErrNotRegisteredConfig = fmt.Errorf("config is not registered")
ErrValueNotFound = fmt.Errorf("value was not found")
ErrIncorrectTagSettings = fmt.Errorf("incorrect tag settings")
ErrRequired = fmt.Errorf("required value is not set")
ErrNoWatchers = fmt.Errorf("no drivers that can watch for changes")
// ErrAccessDenied is returned by drivers if the source denies access to the values: the token has no permission, for example.
ErrAccessDenied = fmt.Errorf("access denied")
)
// parseErrors contains all driver errors collected during Manager.Parse.
type parseErrors []error
func (e parseErrors) Error() string {
msgs := make([]string, 0, len(e))
for _, err := range e {
msgs = append(msgs, err.Error())
}
return strings.Join(msgs, "; ")
}
func (e parseErrors) Is(target error) bool {
for _, err := range e {
if errors.Is(err, target) {
return true
}
}
return false
}
func (e parseErrors) As(target any) bool {
for _, err := range e {
if errors.As(err, target) {
return true
}
}
return false
}
// FieldSource describes the source of the config field value.
type FieldSource struct {
// Field is a struct path of the field in the config, HTTP.Port for example.
Field string
// Driver is a name of the driver that has set the value.
Driver string
// Source is a source of the value inside of the driver: name of the env variable, flag and so on.
Source string
}
type Value struct {
Source string
Value interface{}
// Warning is an optional message about the value that should be seen by the user,
// it is logged by Manager with the warn level: the value is found, but probably not where the user expects.
Warning string
}
type Driver interface {
GenDoc(...*Registered) string
GetName() string
GetValue(field fmap.Field) (*Value, error)
}
// Loader is an optional interface of the driver.
// Load is called once at the start of every Manager.Parse call, before any GetValue call,
// so driver can read its source once and not for every field.
// If Load returns an error, the driver is skipped and the error is returned from Manager.Parse.
type Loader interface {
Load() error
}
// ContextLoader is an optional interface of the driver that reads its source by the network, a remote storage for example.
// Manager calls LoadContext instead of Load with the context of Parse, or with the context of watching for reloads,
// the driver should stop the requests when the context is done.
type ContextLoader interface {
LoadContext(ctx context.Context) error
}
// Watcher is an optional interface of the driver that can notify about changes of its source.
// Watch should block until ctx is done and call onChange after every change of the source.
// Manager calls it while there are subscriptions, see Subscribe; an error is logged and stops watching of the driver.
type Watcher interface {
Watch(ctx context.Context, onChange func()) error
}
type Option interface {
apply(*Manager)
}
type Field struct {
Key string
Value interface{}
}
func LogField(key string, value interface{}) Field {
return Field{
Key: key,
Value: value,
}
}
type Logger interface {
Debug(msg string, fields ...Field)
Warn(msg string, fields ...Field)
Error(msg string, fields ...Field)
Info(msg string, fields ...Field)
With(fields ...Field) Logger
}