-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.go
More file actions
623 lines (530 loc) · 16.4 KB
/
loader.go
File metadata and controls
623 lines (530 loc) · 16.4 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package rigging
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"time"
)
// Transformer mutates a typed config after binding/defaults/conversion and before validation.
// Use for canonicalization or deriving fields from already-typed values.
type Transformer[T any] interface {
Transform(ctx context.Context, cfg *T) error
}
// TransformerFunc is a function adapter for Transformer interface.
type TransformerFunc[T any] func(ctx context.Context, cfg *T) error
func (f TransformerFunc[T]) Transform(ctx context.Context, cfg *T) error {
return f(ctx, cfg)
}
// Loader loads and validates configuration from multiple sources.
// Sources are processed in order (later override earlier). Supports transformers, tag-based validation, and custom validation.
// Thread-safe for reads, not for concurrent configuration changes.
type Loader[T any] struct {
sources []Source
transformers []Transformer[T]
validators []Validator[T]
strict bool // Fail on unknown keys (default: true)
}
// NewLoader creates a Loader with no sources, transformers, or validators and strict mode enabled.
func NewLoader[T any]() *Loader[T] {
return &Loader[T]{
sources: make([]Source, 0),
transformers: make([]Transformer[T], 0),
validators: make([]Validator[T], 0),
strict: true, // Default to strict mode
}
}
// WithSource adds a source. Sources are processed in order (later override earlier).
func (l *Loader[T]) WithSource(src Source) *Loader[T] {
l.sources = append(l.sources, src)
return l
}
// WithTransformer adds a typed transform (executed after binding and before tag-based validation).
func (l *Loader[T]) WithTransformer(t Transformer[T]) *Loader[T] {
l.transformers = append(l.transformers, t)
return l
}
// WithTransformerFunc adds a typed transform function (executed after binding and before tag-based validation).
// This is a convenience wrapper around WithTransformer(TransformerFunc[T](fn)).
func (l *Loader[T]) WithTransformerFunc(fn func(context.Context, *T) error) *Loader[T] {
return l.WithTransformer(TransformerFunc[T](fn))
}
// WithValidator adds a custom validator (executed after transformers and tag-based validation).
func (l *Loader[T]) WithValidator(v Validator[T]) *Loader[T] {
l.validators = append(l.validators, v)
return l
}
// Strict controls whether unknown keys cause errors. Default: true.
func (l *Loader[T]) Strict(strict bool) *Loader[T] {
l.strict = strict
return l
}
// Load loads, merges, binds, and validates configuration from all sources.
// Returns populated config or ValidationError with all field errors.
func (l *Loader[T]) Load(ctx context.Context) (*T, error) {
cfg, _, err := l.loadInternal(ctx, true)
return cfg, err
}
// LoadWithProvenance loads configuration and returns field provenance explicitly.
// Unlike Load, it does not store provenance in the global provenance map.
func (l *Loader[T]) LoadWithProvenance(ctx context.Context) (*T, *Provenance, error) {
return l.loadInternal(ctx, false)
}
func (l *Loader[T]) loadInternal(ctx context.Context, store bool) (*T, *Provenance, error) {
rootType := configType[T]()
// Step 1: Validate the schema before any source I/O.
schemaErrors := getSchemaValidationErrors(rootType)
if len(schemaErrors) > 0 {
return nil, nil, &ValidationError{FieldErrors: schemaErrors}
}
// Step 2: Load from all sources and merge
mergedData := make(map[string]mergedEntry)
for _, source := range l.sources {
var data map[string]any
var originalKeys map[string]string
var err error
// Check if source implements SourceWithKeys for better provenance
if sourceWithKeys, ok := source.(SourceWithKeys); ok {
data, originalKeys, err = sourceWithKeys.LoadWithKeys(ctx)
} else {
data, err = source.Load(ctx)
originalKeys = nil
}
if err != nil {
return nil, nil, fmt.Errorf("load source %s: %w", source.Name(), err)
}
// Merge data into mergedData map
// Later sources override earlier ones
for key, value := range data {
// Normalize key to lowercase dot-separated path
normalizedKey := strings.ToLower(key)
// Determine source key for provenance
sourceKey := source.Name()
if originalKeys != nil {
if origKey, ok := originalKeys[normalizedKey]; ok {
// For env vars, use the full variable name (e.g., "env:APP_DATABASE__PASSWORD")
// For files, just use the filename (e.g., "file:config.yaml")
if strings.HasPrefix(source.Name(), "env") {
sourceKey = "env:" + origKey
}
// For files, sourceKey remains just source.Name() (e.g., "file:config.yaml")
}
}
mergedData[normalizedKey] = mergedEntry{
value: value,
sourceName: source.Name(),
sourceKey: sourceKey,
}
}
}
// Step 3: In strict mode, detect unknown keys
if l.strict {
validKeys := collectValidKeys(rootType, "")
dynamicMapKeyPatterns := collectDynamicMapKeyPatterns(rootType, "")
// Check for unknown keys
var unknownKeyErrors []FieldError
for key := range mergedData {
if !validKeys[key] && !matchesDynamicMapKeyPattern(key, dynamicMapKeyPatterns) {
unknownKeyErrors = append(unknownKeyErrors, FieldError{
FieldPath: key,
Code: ErrCodeUnknownKey,
Message: "unknown configuration key (strict mode)",
})
}
}
if len(unknownKeyErrors) > 0 {
return nil, nil, &ValidationError{FieldErrors: unknownKeyErrors}
}
}
// Step 4: Create zero instance of T
cfg := new(T)
cfgValue := reflect.ValueOf(cfg).Elem()
// Step 5: Bind struct fields from merged data
var provenanceFields []FieldProvenance
presentFields := make(map[string]bool)
bindErrors := bindStructWithPresence(cfgValue, mergedData, &provenanceFields, presentFields, "", "")
allErrors := append([]FieldError{}, bindErrors...)
// Step 6: Run typed transforms after successful binding so transformers operate on converted values.
if len(bindErrors) == 0 {
for i, transformer := range l.transformers {
err := transformer.Transform(ctx, cfg)
if err != nil {
if valErr, ok := err.(*ValidationError); ok {
allErrors = append(allErrors, valErr.FieldErrors...)
} else {
return nil, nil, fmt.Errorf("transformer %d failed: %w", i, err)
}
}
}
}
// Step 7: Validate struct (tag-based validation)
// Required checks use presence data captured before conversion.
validationErrors := validateStructWithPresence(cfgValue, presentFields)
allErrors = append(allErrors, validationErrors...)
// Step 8: Run custom validators
for i, validator := range l.validators {
err := validator.Validate(ctx, cfg)
if err != nil {
// Check if it's a ValidationError
if valErr, ok := err.(*ValidationError); ok {
allErrors = append(allErrors, valErr.FieldErrors...)
} else {
// Wrap other errors as validation errors
return nil, nil, fmt.Errorf("validator %d failed: %w", i, err)
}
}
}
// Step 9: Return error if any validation failed
if len(allErrors) > 0 {
return nil, nil, &ValidationError{FieldErrors: allErrors}
}
// Step 10: Build provenance for the config instance
prov := &Provenance{Fields: provenanceFields}
if store {
storeProvenance(cfg, prov)
}
// Step 11: Return the loaded configuration
return cfg, prov, nil
}
// Watch monitors sources for changes and auto-reloads configuration.
// Returns: snapshots channel, errors channel, initial load error.
// Changes are debounced (100ms). Built-in sources don't support watching yet.
func (l *Loader[T]) Watch(ctx context.Context) (<-chan Snapshot[T], <-chan error, error) {
// Load initial configuration
initialCfg, err := l.Load(ctx)
if err != nil {
return nil, nil, fmt.Errorf("initial load failed: %w", err)
}
// Create channels for snapshots and errors
snapshotCh := make(chan Snapshot[T])
errorCh := make(chan error)
// Start watch goroutine
go l.watchLoop(ctx, initialCfg, snapshotCh, errorCh)
return snapshotCh, errorCh, nil
}
// collectValidKeys recursively collects all valid configuration keys from a struct type.
// It returns a map of valid keys for use in strict mode validation.
func collectValidKeys(t reflect.Type, prefix string) map[string]bool {
validKeys := make(map[string]bool)
// Dereference pointer types
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
// Only process struct types
if t.Kind() != reflect.Struct {
return validKeys
}
// Walk through cached exported field metadata.
for _, meta := range getStructFieldMeta(t) {
field := meta.field
tagCfg := meta.tagCfg
// Determine key path
keyPath := determineKeyPath(field.Name, tagCfg, prefix)
// Add this key as valid
validKeys[keyPath] = true
// Handle nested structs
fieldType := field.Type
// Check if it's an Optional[T] type
if isOptionalType(fieldType) {
// For Optional[T], check the inner type
innerType := fieldType.Field(0).Type
if innerType.Kind() == reflect.Struct {
// Recursively collect keys from nested struct
nestedKeys := collectValidKeys(innerType, keyPath)
for k := range nestedKeys {
validKeys[k] = true
}
}
} else if fieldType.Kind() == reflect.Struct {
// Skip time.Time and time.Duration (they're structs but treated as primitives)
if fieldType.PkgPath() == "time" {
continue
}
// Determine prefix for nested struct
nestedPrefix := keyPath
if tagCfg.prefix != "" {
nestedPrefix = tagCfg.prefix
}
// Recursively collect keys from nested struct
nestedKeys := collectValidKeys(fieldType, nestedPrefix)
for k := range nestedKeys {
validKeys[k] = true
}
}
}
return validKeys
}
func collectDynamicMapKeyPatterns(t reflect.Type, prefix string) []string {
patternSet := make(map[string]struct{})
var walk func(reflect.Type, string)
walk = func(currType reflect.Type, currPrefix string) {
if currType.Kind() == reflect.Ptr {
currType = currType.Elem()
}
if currType.Kind() != reflect.Struct {
return
}
for _, meta := range getStructFieldMeta(currType) {
field := meta.field
tagCfg := meta.tagCfg
keyPath := determineKeyPath(field.Name, tagCfg, currPrefix)
fieldType := field.Type
unwrappedType := fieldType
if isOptionalType(unwrappedType) {
unwrappedType = unwrappedType.Field(0).Type
}
if unwrappedType.Kind() == reflect.Map && unwrappedType.Key().Kind() == reflect.String {
for _, pattern := range collectMapValueKeyPatterns(keyPath, unwrappedType.Elem()) {
patternSet[pattern] = struct{}{}
}
}
if isOptionalType(fieldType) {
innerType := fieldType.Field(0).Type
if innerType.Kind() == reflect.Struct {
walk(innerType, keyPath)
}
continue
}
if fieldType.Kind() != reflect.Struct || fieldType.PkgPath() == "time" {
continue
}
nestedPrefix := keyPath
if tagCfg.prefix != "" {
nestedPrefix = tagCfg.prefix
}
walk(fieldType, nestedPrefix)
}
}
walk(t, prefix)
patterns := make([]string, 0, len(patternSet))
for pattern := range patternSet {
patterns = append(patterns, pattern)
}
return patterns
}
func collectMapValueKeyPatterns(baseKey string, valueType reflect.Type) []string {
if valueType.Kind() == reflect.Ptr {
valueType = valueType.Elem()
}
if isOptionalType(valueType) {
valueType = valueType.Field(0).Type
}
if valueType.Kind() == reflect.Struct && valueType.PkgPath() != "time" {
patterns := make([]string, 0)
for key := range collectValidKeys(valueType, baseKey+".*") {
patterns = append(patterns, key)
}
return patterns
}
return []string{baseKey + ".*"}
}
func matchesDynamicMapKeyPattern(key string, patterns []string) bool {
for _, pattern := range patterns {
if matchDotKeyPattern(pattern, key) {
return true
}
}
return false
}
func matchDotKeyPattern(pattern string, key string) bool {
patternParts := strings.Split(pattern, ".")
keyParts := strings.Split(key, ".")
if len(patternParts) != len(keyParts) {
return false
}
for i := range patternParts {
if patternParts[i] == "*" {
continue
}
if patternParts[i] != keyParts[i] {
return false
}
}
return true
}
// watchLoop is the main goroutine that monitors sources for changes and reloads configuration.
// It handles debouncing, thread-safe snapshot emission, and cleanup.
func (l *Loader[T]) watchLoop(ctx context.Context, initialCfg *T, snapshotCh chan<- Snapshot[T], errorCh chan<- error) {
defer close(snapshotCh)
defer close(errorCh)
// Emit initial snapshot
currentVersion := int64(1)
currentCfg := initialCfg
snapshotCh <- Snapshot[T]{
Config: initialCfg,
Version: currentVersion,
LoadedAt: time.Now(),
Source: "initial",
}
// Start watching all sources
changeChannels := make([]<-chan ChangeEvent, 0, len(l.sources))
cancelFuncs := make([]context.CancelFunc, 0, len(l.sources))
for _, source := range l.sources {
// Create a child context for this source watcher
sourceCtx, cancel := context.WithCancel(ctx)
cancelFuncs = append(cancelFuncs, cancel)
// Try to watch this source
changeCh, err := source.Watch(sourceCtx)
if err != nil {
// If watch is not supported, skip this source
if errors.Is(err, ErrWatchNotSupported) {
cancel() // Clean up the context
continue
}
// For other errors, send to error channel and skip
select {
case errorCh <- fmt.Errorf("watch source %s: %w", source.Name(), err):
case <-ctx.Done():
cancel()
return
}
cancel()
continue
}
changeChannels = append(changeChannels, changeCh)
}
defer func() {
for _, cancel := range cancelFuncs {
cancel()
}
}()
// If no sources support watching, we're done
if len(changeChannels) == 0 {
return
}
// Create a debounce timer
var debounceTimer *time.Timer
var debounceCh <-chan time.Time
pendingReload := false
pendingCause := ""
const debounceDelay = 100 * time.Millisecond
defer func() {
if debounceTimer == nil {
return
}
if !debounceTimer.Stop() {
select {
case <-debounceTimer.C:
default:
}
}
}()
// Merge all change channels into one
mergedChanges := make(chan ChangeEvent)
go func() {
defer close(mergedChanges)
for {
// Use reflection to select from multiple channels
cases := make([]reflect.SelectCase, len(changeChannels)+1)
// Add context.Done case
cases[0] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ctx.Done()),
}
// Add all change channels
for i, ch := range changeChannels {
cases[i+1] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ch),
}
}
// Wait for any channel to receive
chosen, value, ok := reflect.Select(cases)
// Check if context was cancelled
if chosen == 0 {
return
}
// Check if channel was closed
if !ok {
// Remove this channel from the list
changeChannels = append(changeChannels[:chosen-1], changeChannels[chosen:]...)
// If all channels are closed, exit
if len(changeChannels) == 0 {
return
}
continue
}
// Extract the ChangeEvent
event, ok := value.Interface().(ChangeEvent)
if !ok {
continue
}
// Send to merged channel
select {
case mergedChanges <- event:
case <-ctx.Done():
return
}
}
}()
// Main watch loop
for {
if mergedChanges == nil && !pendingReload {
return
}
select {
case <-ctx.Done():
return
case event, ok := <-mergedChanges:
if !ok {
// All change channels closed; process any pending debounced reload first.
mergedChanges = nil
if !pendingReload {
return
}
continue
}
// Debounce: reset timer on each event
pendingCause = event.Cause
pendingReload = true
if debounceTimer == nil {
debounceTimer = time.NewTimer(debounceDelay)
debounceCh = debounceTimer.C
continue
}
if !debounceTimer.Stop() {
select {
case <-debounceTimer.C:
default:
}
}
debounceTimer.Reset(debounceDelay)
debounceCh = debounceTimer.C
case <-debounceCh:
debounceCh = nil
if !pendingReload {
continue
}
// Reload configuration from the latest debounced event.
cause := pendingCause
pendingReload = false
newCfg, err := l.Load(ctx)
if err != nil {
// Send error, keep previous config.
select {
case errorCh <- fmt.Errorf("reload failed: %w", err):
case <-ctx.Done():
return
}
continue
}
// Emit snapshot first; only then release the superseded provenance.
snapshot := Snapshot[T]{
Config: newCfg,
Version: currentVersion + 1,
LoadedAt: time.Now(),
Source: cause,
}
select {
case snapshotCh <- snapshot:
currentVersion++
ReleaseProvenance(currentCfg)
currentCfg = newCfg
case <-ctx.Done():
ReleaseProvenance(newCfg)
return
}
}
}
}