-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
247 lines (203 loc) · 6.92 KB
/
Copy pathconfig.go
File metadata and controls
247 lines (203 loc) · 6.92 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package forge
import (
"fmt"
"reflect"
"time"
"github.com/xraph/confy"
"github.com/xraph/confy/sources"
"github.com/xraph/forge/config"
)
// =============================================================================
// CONFIGURATION CONSTANTS
// =============================================================================
// ConfigKey is the service key for configuration manager
// Use config.ManagerKey or shared.ConfigKey for consistency.
const ConfigKey = "config"
// =============================================================================
// RE-EXPORT CONFIG TYPES
// =============================================================================
// ConfigManager is the configuration manager interface.
type ConfigManager = confy.Confy
// Configuration Sources.
type (
ConfigSource = confy.ConfigSource
ConfigSourceOptions = confy.ConfigSourceOptions
ConfigSourceFactory = confy.ConfigSourceFactory
SourceConfig = confy.SourceConfig
SourceMetadata = confy.SourceMetadata
SourceRegistry = confy.SourceRegistry
SourceEvent = confy.SourceEvent
SourceEventHandler = confy.SourceEventHandler
WatchContext = confy.WatchContext
)
// Configuration Options.
type (
GetOption = confy.GetOption
GetOptions = confy.GetOptions
BindOptions = confy.BindOptions
)
// Configuration Changes.
type (
ConfigChange = confy.ConfigChange
ChangeType = confy.ChangeType
)
// Constants.
const (
ChangeTypeSet = confy.ChangeTypeSet
ChangeTypeUpdate = confy.ChangeTypeUpdate
ChangeTypeDelete = confy.ChangeTypeDelete
ChangeTypeReload = confy.ChangeTypeReload
)
// Validation.
type (
ValidationOptions = confy.ValidationOptions
ValidationRule = confy.ValidationRule
ValidationConfig = confy.ValidationConfig
ValidationMode = confy.ValidationMode
Validator = confy.Validator
)
const (
ValidationModePermissive = confy.ValidationModePermissive
ValidationModeStrict = confy.ValidationModeStrict
ValidationModeLoose = confy.ValidationModeLoose
)
// Secrets.
type (
SecretsManager = confy.SecretsManager
SecretProvider = confy.SecretProvider
SecretsConfig = confy.SecretsConfig
)
// Manager Configuration.
type (
ConfigConfig = confy.Config
)
// Environment Variable Source Types.
type (
EnvSourceOptions = sources.EnvSourceOptions
EnvSourceConfig = sources.EnvSourceConfig
)
// Auto-Discovery Types.
type (
AutoDiscoveryConfig = confy.AutoDiscoveryConfig
AutoDiscoveryResult = confy.AutoDiscoveryResult
)
// Watcher.
type (
Watcher = confy.Watcher
WatcherConfig = confy.WatcherConfig
)
// =============================================================================
// RE-EXPORT CONFIG CONSTRUCTORS
// =============================================================================
var (
NewManager = confy.New
NewSourceRegistry = confy.NewSourceRegistry
NewValidator = confy.NewValidator
NewWatcher = confy.NewWatcher
NewSecretsManager = confy.NewSecretsManager
// NewEnvSource creates an environment variable source.
NewEnvSource = sources.NewEnvSource
// DiscoverAndLoadConfigs discovers and loads configuration files automatically.
DiscoverAndLoadConfigs = confy.DiscoverAndLoadConfigs
// DefaultAutoDiscoveryConfig returns the default auto-discovery configuration.
DefaultAutoDiscoveryConfig = confy.DefaultAutoDiscoveryConfig
)
// =============================================================================
// RE-EXPORT CONFIG OPTIONS (Functional Options)
// =============================================================================
var (
WithDefault = confy.WithDefault
WithRequired = confy.WithRequired
WithValidator = confy.WithValidator
WithTransform = confy.WithTransform
WithOnMissing = confy.WithOnMissing
AllowEmpty = confy.AllowEmpty
WithCacheKey = confy.WithCacheKey
)
// =============================================================================
// DEFAULT BIND OPTIONS
// =============================================================================
// DefaultBindOptions returns default bind options.
func DefaultBindOptions() BindOptions {
return BindOptions{
UseDefaults: true,
IgnoreCase: false,
TagName: "yaml",
Required: []string{},
ErrorOnMissing: false,
DeepMerge: true,
}
}
// =============================================================================
// ERROR HANDLER FOR CONFIG
// =============================================================================
// ConfigErrorHandler defines how errors should be handled in the config system.
type ConfigErrorHandler interface {
// HandleError handles an error
HandleError(ctx Context, err error) error
// ShouldRetry determines if an operation should be retried
ShouldRetry(err error) bool
// GetRetryDelay returns the delay before retrying
GetRetryDelay(attempt int, err error) time.Duration
}
// DefaultConfigErrorHandler is the default error handler for config.
type DefaultConfigErrorHandler struct {
logger Logger
}
// NewDefaultConfigErrorHandler creates a new default config error handler.
func NewDefaultConfigErrorHandler(logger Logger) *DefaultConfigErrorHandler {
return &DefaultConfigErrorHandler{
logger: logger,
}
}
// HandleError handles an error.
func (h *DefaultConfigErrorHandler) HandleError(ctx Context, err error) error {
if h.logger != nil {
h.logger.Error("config error occurred",
Error(err),
)
}
return err
}
// ShouldRetry determines if an operation should be retried.
func (h *DefaultConfigErrorHandler) ShouldRetry(err error) bool {
return false // Config errors generally shouldn't auto-retry
}
// GetRetryDelay returns the delay before retrying.
func (h *DefaultConfigErrorHandler) GetRetryDelay(attempt int, err error) time.Duration {
return time.Duration(100*attempt*attempt) * time.Millisecond
}
// =============================================================================
// HELPER FUNCTIONS
// =============================================================================
// SafeGet returns a value with type checking.
func SafeGet[T any](cm ConfigManager, key string) (T, error) {
var zero T
value := cm.Get(key)
if value == nil {
return zero, fmt.Errorf("config not found: %s", key)
}
result, ok := value.(T)
if !ok {
return zero, fmt.Errorf("config type mismatch for key %s: expected %v, got %v", key, reflect.TypeOf(zero), reflect.TypeOf(value))
}
return result, nil
}
// MustGet returns a value or panics if not found.
func MustGet[T any](cm ConfigManager, key string) T {
value, err := SafeGet[T](cm, key)
if err != nil {
panic(err)
}
return value
}
// GetConfigManager resolves the config manager from the container
// Returns the config manager instance and an error if resolution fails.
func GetConfigManager(c Container) (ConfigManager, error) {
return config.GetConfigManager(c)
}
// GetConfy resolves the confy from the container
// Returns the confy instance and an error if resolution fails.
func GetConfy(c Container) (confy.Confy, error) {
return config.GetConfy(c)
}