-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_provider.go
More file actions
1227 lines (1074 loc) · 40.5 KB
/
config_provider.go
File metadata and controls
1227 lines (1074 loc) · 40.5 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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package modular
import (
"fmt"
"reflect"
"sort"
"sync"
"sync/atomic"
)
const mainConfigSection = "_main"
// LoadAppConfigFunc is the function type for loading application configuration.
// This function is responsible for loading configuration data into the application
// using the registered config feeders and config sections.
//
// The default implementation can be replaced for testing or custom configuration scenarios.
type LoadAppConfigFunc func(*StdApplication) error
// AppConfigLoader is the default implementation that can be replaced in tests.
// This variable allows the configuration loading strategy to be customized,
// which is particularly useful for testing scenarios where you want to
// control how configuration is loaded.
//
// Example of replacing for tests:
//
// oldLoader := modular.AppConfigLoader
// defer func() { modular.AppConfigLoader = oldLoader }()
// modular.AppConfigLoader = func(app *StdApplication) error {
// // Custom test configuration loading
// return nil
// }
var AppConfigLoader LoadAppConfigFunc = loadAppConfig
// ConfigProvider defines the interface for providing configuration objects.
// Configuration providers encapsulate configuration data and make it available
// to modules and the application framework.
//
// The framework supports multiple configuration sources (files, environment variables,
// command-line flags) and formats (JSON, YAML, TOML) through different providers.
type ConfigProvider interface {
// GetConfig returns the configuration object.
// The returned value should be a pointer to a struct that represents
// the configuration schema. Modules typically type-assert this to
// their expected configuration type.
//
// Example:
// cfg := provider.GetConfig().(*MyModuleConfig)
GetConfig() any
}
// StdConfigProvider provides a standard implementation of ConfigProvider.
// It wraps a configuration struct and makes it available through the ConfigProvider interface.
//
// IMPORTANT THREAD SAFETY WARNING:
// StdConfigProvider returns the SAME reference on every GetConfig() call.
// This means:
// - Multiple modules/goroutines will share the same configuration object
// - Modifications by any consumer affect ALL other consumers
// - NOT safe for concurrent modification
// - NOT suitable for multi-tenant applications with per-tenant config isolation
//
// For safer alternatives, see:
// - NewIsolatedConfigProvider: Returns deep copies (test isolation)
// - NewImmutableConfigProvider: Thread-safe immutable config (production)
// - NewCopyOnWriteConfigProvider: Copy-on-write for defensive mutations
//
// Best practices:
// - Use StdConfigProvider only when you need shared mutable config
// - Modules should NOT modify configs in-place
// - Tests should use IsolatedConfigProvider to prevent pollution
type StdConfigProvider struct {
cfg any
}
// GetConfig returns the configuration object.
// WARNING: The returned value is the exact object reference that was passed to NewStdConfigProvider.
// Any modifications to this object will affect all other consumers of this config provider.
func (s *StdConfigProvider) GetConfig() any {
return s.cfg
}
// NewStdConfigProvider creates a new standard configuration provider.
// The cfg parameter should be a pointer to a struct that defines the
// configuration schema for your module.
//
// WARNING: This provider returns the SAME reference on every GetConfig() call.
// For test isolation or thread-safe scenarios, use NewIsolatedConfigProvider or
// NewImmutableConfigProvider instead.
//
// Example:
//
// type MyConfig struct {
// Host string `json:"host" default:"localhost"`
// Port int `json:"port" default:"8080"`
// }
//
// cfg := &MyConfig{}
// provider := modular.NewStdConfigProvider(cfg)
func NewStdConfigProvider(cfg any) *StdConfigProvider {
return &StdConfigProvider{cfg: cfg}
}
// IsolatedConfigProvider provides complete configuration isolation by returning
// a deep copy on every GetConfig() call. This ensures that each consumer receives
// its own independent copy of the configuration, preventing any possibility of
// shared state or mutation pollution.
//
// Use cases:
// - Test isolation: Prevents config pollution between test runs
// - Multi-tenant applications: Each tenant gets isolated config
// - Defensive programming: Modules can modify their config without side effects
//
// Performance considerations:
// - Deep copy on EVERY GetConfig() call (expensive)
// - Best suited for scenarios where isolation is more important than performance
// - For production workloads, consider ImmutableConfigProvider instead
//
// Example:
//
// cfg := &MyConfig{Host: "localhost", Port: 8080}
// provider := modular.NewIsolatedConfigProvider(cfg)
// // Each call returns a completely independent copy
// copy1 := provider.GetConfig().(*MyConfig)
// copy2 := provider.GetConfig().(*MyConfig)
// copy1.Port = 9090 // Does NOT affect copy2
type IsolatedConfigProvider struct {
cfg any
}
// GetConfig returns a deep copy of the configuration object.
// Each call creates a new independent copy, ensuring complete isolation.
// Returns nil if deep copying fails to maintain isolation guarantees.
func (p *IsolatedConfigProvider) GetConfig() any {
copied, err := DeepCopyConfig(p.cfg)
if err != nil {
// Return nil to prevent shared state pollution and maintain isolation guarantees
return nil
}
return copied
}
// NewIsolatedConfigProvider creates a configuration provider that returns
// deep copies on every GetConfig() call, ensuring complete isolation between
// consumers.
//
// This is the recommended provider for test scenarios where config isolation
// is critical to prevent test pollution.
//
// Example:
//
// cfg := &MyConfig{Host: "localhost"}
// provider := modular.NewIsolatedConfigProvider(cfg)
func NewIsolatedConfigProvider(cfg any) *IsolatedConfigProvider {
return &IsolatedConfigProvider{cfg: cfg}
}
// ImmutableConfigProvider provides thread-safe access to configuration using
// atomic operations. The configuration is stored in an atomic.Value, allowing
// concurrent reads without locks while supporting atomic updates.
//
// Use cases:
// - Production applications with concurrent access
// - High-performance read-heavy workloads
// - Configuration hot-reloading with atomic swaps
// - Multi-tenant applications with shared config
//
// Thread safety:
// - GetConfig() is lock-free and safe for concurrent reads
// - Multiple goroutines can read simultaneously without contention
// - Updates via UpdateConfig() are atomic
//
// Performance:
// - Excellent for read-heavy workloads (no locks, no copies)
// - Near-zero overhead for reads
// - Best choice for production concurrent scenarios
//
// Example:
//
// cfg := &MyConfig{Host: "localhost", Port: 8080}
// provider := modular.NewImmutableConfigProvider(cfg)
// // Thread-safe reads from multiple goroutines
// config := provider.GetConfig().(*MyConfig)
// // Atomic update
// newCfg := &MyConfig{Host: "example.com", Port: 443}
// provider.UpdateConfig(newCfg)
type ImmutableConfigProvider struct {
cfg atomic.Value
}
// GetConfig returns the current configuration object in a thread-safe manner.
// This operation is lock-free and safe for concurrent access from multiple goroutines.
func (p *ImmutableConfigProvider) GetConfig() any {
return p.cfg.Load()
}
// UpdateConfig atomically replaces the configuration with a new value.
// This operation is thread-safe and all subsequent GetConfig() calls will
// return the new configuration.
//
// This is useful for configuration hot-reloading scenarios where you want to
// update the config without restarting the application.
func (p *ImmutableConfigProvider) UpdateConfig(cfg any) {
p.cfg.Store(cfg)
}
// NewImmutableConfigProvider creates a thread-safe configuration provider
// using atomic operations. This is the recommended provider for production
// applications with concurrent access patterns.
//
// Example:
//
// cfg := &MyConfig{Host: "localhost", Port: 8080}
// provider := modular.NewImmutableConfigProvider(cfg)
func NewImmutableConfigProvider(cfg any) *ImmutableConfigProvider {
provider := &ImmutableConfigProvider{}
provider.cfg.Store(cfg)
return provider
}
// CopyOnWriteConfigProvider provides a configuration provider with explicit
// copy-on-write semantics. It returns the original configuration for reads,
// but provides a GetMutableConfig() method that returns an isolated deep copy
// for scenarios where modifications are needed.
//
// Use cases:
// - Modules that need to apply defensive modifications
// - Scenarios where you want to modify config without affecting others
// - When you need explicit control over when copies are made
//
// Thread safety:
// - GetConfig() uses RLock for safe concurrent reads
// - GetMutableConfig() uses Lock and creates isolated copies
// - Safe for concurrent access with proper synchronization
//
// Performance:
// - Good: Only copies when explicitly requested via GetMutableConfig()
// - Read-heavy workloads perform well (RLock is cheap)
// - Better than IsolatedConfigProvider (which copies on every read)
//
// Example:
//
// cfg := &MyConfig{Host: "localhost", Port: 8080}
// provider := modular.NewCopyOnWriteConfigProvider(cfg)
//
// // Read-only access (no copy)
// readCfg := provider.GetConfig().(*MyConfig)
//
// // Need to modify? Get a mutable copy
// mutableCfg, err := provider.GetMutableConfig()
// if err == nil {
// cfg := mutableCfg.(*MyConfig)
// cfg.Port = 9090 // Safe to modify, won't affect others
// }
type CopyOnWriteConfigProvider struct {
cfg any
mu sync.RWMutex
}
// GetConfig returns the original configuration object for read-only access.
// This method uses RLock for safe concurrent reads without creating copies.
func (p *CopyOnWriteConfigProvider) GetConfig() any {
p.mu.RLock()
defer p.mu.RUnlock()
return p.cfg
}
// GetMutableConfig returns a deep copy of the configuration for modification.
// The returned copy is completely isolated from the original and other consumers.
//
// This method should be used when you need to make modifications to the config
// without affecting other consumers. The copy is created using DeepCopyConfig.
//
// Returns an error if deep copying fails.
func (p *CopyOnWriteConfigProvider) GetMutableConfig() (any, error) {
p.mu.RLock()
defer p.mu.RUnlock()
return DeepCopyConfig(p.cfg)
}
// UpdateOriginal atomically replaces the original configuration with a new value.
// This allows implementing config hot-reload scenarios where you want to update
// the base configuration that GetConfig() returns.
func (p *CopyOnWriteConfigProvider) UpdateOriginal(cfg any) {
p.mu.Lock()
defer p.mu.Unlock()
p.cfg = cfg
}
// NewCopyOnWriteConfigProvider creates a configuration provider with
// copy-on-write semantics. Use GetConfig() for read-only access and
// GetMutableConfig() when you need an isolated copy for modification.
//
// Example:
//
// cfg := &MyConfig{Host: "localhost", Port: 8080}
// provider := modular.NewCopyOnWriteConfigProvider(cfg)
func NewCopyOnWriteConfigProvider(cfg any) *CopyOnWriteConfigProvider {
return &CopyOnWriteConfigProvider{cfg: cfg}
}
// Config represents a configuration builder that can combine multiple feeders and structures.
// It provides functionality for the modular framework to coordinate configuration loading.
//
// The Config builder allows you to:
// - Add multiple configuration sources (files, environment, etc.)
// - Combine configuration from different feeders
// - Apply configuration to multiple struct targets
// - Track which structs have been configured
// - Enable verbose debugging for configuration processing
// - Track field-level population details
type Config struct {
// Feeders contains all the registered configuration feeders
Feeders []Feeder
// StructKeys maps struct identifiers to their configuration objects.
// Used internally to track which configuration structures have been processed.
StructKeys map[string]any
// VerboseDebug enables detailed logging during configuration processing
VerboseDebug bool
// Logger is used for verbose debug logging
Logger Logger
// FieldTracker tracks which fields are populated by which feeders
FieldTracker FieldTracker
}
// NewConfig creates a new configuration builder.
// The returned Config can be used to set up complex configuration scenarios
// involving multiple sources and target structures.
//
// Example:
//
// cfg := modular.NewConfig()
// cfg.AddFeeder(modular.ConfigFeeders[0]) // Add file feeder
// cfg.AddStruct(&myConfig) // Add target struct
// err := cfg.Feed() // Load configuration
func NewConfig() *Config {
return &Config{
Feeders: make([]Feeder, 0),
StructKeys: make(map[string]any),
VerboseDebug: false,
Logger: nil,
FieldTracker: NewDefaultFieldTracker(),
}
}
// SetVerboseDebug enables or disables verbose debug logging
func (c *Config) SetVerboseDebug(enabled bool, logger Logger) *Config {
c.VerboseDebug = enabled
c.Logger = logger
// Set logger on field tracker
if c.FieldTracker != nil {
c.FieldTracker.SetLogger(logger)
}
// Apply verbose debugging to any verbose-aware feeders
for _, feeder := range c.Feeders {
if verboseFeeder, ok := feeder.(VerboseAwareFeeder); ok {
verboseFeeder.SetVerboseDebug(enabled, logger)
}
}
return c
}
// AddFeeder adds a configuration feeder to support verbose debugging and field tracking
func (c *Config) AddFeeder(feeder Feeder) *Config {
c.Feeders = append(c.Feeders, feeder)
// If verbose debugging is enabled, apply it to this feeder
if c.VerboseDebug && c.Logger != nil {
if verboseFeeder, ok := feeder.(VerboseAwareFeeder); ok {
verboseFeeder.SetVerboseDebug(true, c.Logger)
}
}
// If field tracking is enabled, apply it to this feeder
if c.FieldTracker != nil {
// Check for main package FieldTrackingFeeder interface
if trackingFeeder, ok := feeder.(FieldTrackingFeeder); ok {
trackingFeeder.SetFieldTracker(c.FieldTracker)
} else {
// Check for feeders package interface compatibility
// Use reflection to check if the feeder has a SetFieldTracker method
feederValue := reflect.ValueOf(feeder)
setFieldTrackerMethod := feederValue.MethodByName("SetFieldTracker")
if setFieldTrackerMethod.IsValid() {
// Create a bridge adapter and call SetFieldTracker
bridge := NewFieldTrackerBridge(c.FieldTracker)
args := []reflect.Value{reflect.ValueOf(bridge)}
setFieldTrackerMethod.Call(args)
}
}
}
return c
}
// AddStructKey adds a structure with a key to the configuration
func (c *Config) AddStructKey(key string, target any) *Config {
c.StructKeys[key] = target
return c
}
// SetFieldTracker sets the field tracker for capturing field population details
func (c *Config) SetFieldTracker(tracker FieldTracker) *Config {
c.FieldTracker = tracker
if c.Logger != nil {
c.FieldTracker.SetLogger(c.Logger)
}
// Apply field tracking to any tracking-aware feeders
for _, feeder := range c.Feeders {
if trackingFeeder, ok := feeder.(FieldTrackingFeeder); ok {
trackingFeeder.SetFieldTracker(tracker)
}
}
return c
}
// FeedWithModuleContext feeds a single configuration structure with module context information
// This allows module-aware feeders to customize their behavior based on the module name
func (c *Config) FeedWithModuleContext(target any, moduleName string) error {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Starting module-aware config feed", "targetType", reflect.TypeOf(target), "moduleName", moduleName, "feedersCount", len(c.Feeders))
}
for i, f := range c.Feeders {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Applying feeder with module context", "feederIndex", i, "feederType", fmt.Sprintf("%T", f), "moduleName", moduleName)
}
// Try module-aware feeder first if available
if maf, ok := f.(ModuleAwareFeeder); ok {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Using ModuleAwareFeeder", "feederType", fmt.Sprintf("%T", f), "moduleName", moduleName)
}
if err := maf.FeedWithModuleContext(target, moduleName); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("ModuleAwareFeeder failed", "feederType", fmt.Sprintf("%T", f), "error", err)
}
return fmt.Errorf("config feeder error: %w: %w", ErrConfigFeederError, err)
}
} else {
// Fall back to regular Feed method for non-module-aware feeders
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Using regular Feed method", "feederType", fmt.Sprintf("%T", f))
}
if err := f.Feed(target); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Regular Feed method failed", "feederType", fmt.Sprintf("%T", f), "error", err)
}
return fmt.Errorf("config feeder error: %w: %w", ErrConfigFeederError, err)
}
}
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Feeder applied successfully", "feederType", fmt.Sprintf("%T", f))
}
}
// Apply defaults and validate config
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Validating config", "moduleName", moduleName)
}
if err := ValidateConfig(target); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Config validation failed", "moduleName", moduleName, "error", err)
}
return fmt.Errorf("config validation error for %s: %w", moduleName, err)
}
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Config validation succeeded", "moduleName", moduleName)
}
// Call Setup if implemented
if setupable, ok := target.(ConfigSetup); ok {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Calling Setup for config", "moduleName", moduleName)
}
if err := setupable.Setup(); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Config setup failed", "moduleName", moduleName, "error", err)
}
return fmt.Errorf("%w for %s: %w", ErrConfigSetupError, moduleName, err)
}
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Config setup succeeded", "moduleName", moduleName)
}
}
return nil
}
// Feed with validation applies defaults and validates configs after feeding
func (c *Config) Feed() error {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Starting config feed process", "structKeysCount", len(c.StructKeys), "feedersCount", len(c.Feeders))
}
// Sort feeders by priority (ascending order, so higher priority applies last)
sortedFeeders := c.sortFeedersByPriority()
// If we have struct keys, feed them directly with field tracking
if len(c.StructKeys) > 0 {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Using enhanced feeding process with field tracking")
}
// Feed each struct key with each feeder
for key, target := range c.StructKeys {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Processing struct key", "key", key, "targetType", reflect.TypeOf(target))
}
for i, f := range sortedFeeders {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Applying feeder to struct", "key", key, "feederIndex", i, "feederType", fmt.Sprintf("%T", f))
}
// Try module-aware feeder first if this is a section config (not main config)
if key != mainConfigSection {
if maf, ok := f.(ModuleAwareFeeder); ok {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Using ModuleAwareFeeder for section", "key", key, "feederType", fmt.Sprintf("%T", f))
}
if err := maf.FeedWithModuleContext(target, key); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("ModuleAwareFeeder Feed method failed", "key", key, "feederType", fmt.Sprintf("%T", f), "error", err)
}
return fmt.Errorf("config feeder error: %w: %w", ErrConfigFeederError, err)
}
} else {
// Fall back to regular Feed method for non-module-aware feeders
if err := f.Feed(target); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Regular Feed method failed", "key", key, "feederType", fmt.Sprintf("%T", f), "error", err)
}
return fmt.Errorf("config feeder error: %w: %w", ErrConfigFeederError, err)
}
}
} else {
// Use regular Feed method for main config
if err := f.Feed(target); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Feeder Feed method failed", "key", key, "feederType", fmt.Sprintf("%T", f), "error", err)
}
return fmt.Errorf("config feeder error: %w: %w", ErrConfigFeederError, err)
}
}
// Also try ComplexFeeder if available (for instance-aware feeders)
if cf, ok := f.(ComplexFeeder); ok {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Applying ComplexFeeder FeedKey", "key", key, "feederType", fmt.Sprintf("%T", f))
}
if err := cf.FeedKey(key, target); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("ComplexFeeder FeedKey failed", "key", key, "feederType", fmt.Sprintf("%T", f), "error", err)
}
return fmt.Errorf("config feeder error: %w: %w", ErrConfigFeederError, err)
}
}
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Feeder applied successfully", "key", key, "feederType", fmt.Sprintf("%T", f))
}
}
// Apply defaults and validate config
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Validating config for struct key", "key", key)
}
if err := ValidateConfig(target); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Config validation failed", "key", key, "error", err)
}
return fmt.Errorf("config validation error for %s: %w", key, err)
}
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Config validation succeeded", "key", key)
}
// Call Setup if implemented
if setupable, ok := target.(ConfigSetup); ok {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Calling Setup for config", "key", key)
}
if err := setupable.Setup(); err != nil {
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Config setup failed", "key", key, "error", err)
}
return fmt.Errorf("%w for %s: %w", ErrConfigSetupError, key, err)
}
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Config setup succeeded", "key", key)
}
}
}
} else {
// No struct keys configured - this means no explicit structures were added
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("No struct keys configured - skipping feed process")
}
}
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Config feed process completed successfully")
}
return nil
}
// sortFeedersByPriority sorts feeders by priority in ascending order.
// Higher priority values are applied later, allowing them to override lower priority feeders.
// Feeders without priority (not implementing PrioritizedFeeder) default to priority 0.
// When priorities are equal, original order is preserved (stable sort).
func (c *Config) sortFeedersByPriority() []Feeder {
// Create a copy of feeders to avoid modifying the original slice
sortedFeeders := make([]Feeder, len(c.Feeders))
copy(sortedFeeders, c.Feeders)
// Sort by priority (ascending, so highest priority applies last)
sort.SliceStable(sortedFeeders, func(i, j int) bool {
priI := 0
if pf, ok := sortedFeeders[i].(PrioritizedFeeder); ok {
priI = pf.Priority()
}
priJ := 0
if pf, ok := sortedFeeders[j].(PrioritizedFeeder); ok {
priJ = pf.Priority()
}
return priI < priJ
})
if c.VerboseDebug && c.Logger != nil {
c.Logger.Debug("Feeders sorted by priority")
for i, f := range sortedFeeders {
pri := 0
if pf, ok := f.(PrioritizedFeeder); ok {
pri = pf.Priority()
}
c.Logger.Debug("Feeder order", "index", i, "type", fmt.Sprintf("%T", f), "priority", pri)
}
}
return sortedFeeders
}
// ConfigSetup is an interface that configs can implement
// to perform additional setup after being populated by feeders
type ConfigSetup interface {
Setup() error
}
func loadAppConfig(app *StdApplication) error {
// Guard against nil application
if app == nil {
return ErrApplicationNil
}
if app.IsVerboseConfig() {
app.logger.Debug("Starting configuration loading process")
}
// Auto-detect base config structure if not explicitly configured
if !IsBaseConfigEnabled() {
if DetectBaseConfigStructure() {
if app.IsVerboseConfig() {
app.logger.Debug("Auto-detected base configuration structure",
"configDir", BaseConfigSettings.ConfigDir,
"environment", BaseConfigSettings.Environment)
}
}
}
// Prepare config feeders - include base config feeder if enabled.
// Priority / order:
// 1. Base config feeder (if enabled)
// 2. Per-app feeders (if explicitly provided via SetConfigFeeders)
// 3. Global ConfigFeeders fallback (if no per-app feeders provided)
var effectiveFeeders []Feeder
// Start capacity estimation (base + either per-app or global)
baseCount := 0
if IsBaseConfigEnabled() && GetBaseConfigFeeder() != nil {
baseCount = 1
}
if app.configFeeders != nil {
effectiveFeeders = make([]Feeder, 0, baseCount+len(app.configFeeders))
} else {
effectiveFeeders = make([]Feeder, 0, baseCount+len(ConfigFeeders))
}
// Add base config feeder first if enabled (so it gets processed first)
if IsBaseConfigEnabled() {
if baseFeeder := GetBaseConfigFeeder(); baseFeeder != nil {
effectiveFeeders = append(effectiveFeeders, baseFeeder)
if app.IsVerboseConfig() {
app.logger.Debug("Added base config feeder",
"configDir", BaseConfigSettings.ConfigDir,
"environment", BaseConfigSettings.Environment)
}
}
}
// Append per-app feeders if provided; else fall back to global
if app.configFeeders != nil {
effectiveFeeders = append(effectiveFeeders, app.configFeeders...)
} else {
effectiveFeeders = append(effectiveFeeders, ConfigFeeders...)
}
// Skip if no feeders are defined
if len(effectiveFeeders) == 0 {
app.logger.Info("No config feeders defined, skipping config loading")
return nil
}
if app.IsVerboseConfig() {
app.logger.Debug("Configuration feeders available", "count", len(effectiveFeeders))
for i, feeder := range effectiveFeeders {
app.logger.Debug("Config feeder registered", "index", i, "type", fmt.Sprintf("%T", feeder))
}
}
// Build the configuration
cfgBuilder := NewConfig()
if app.IsVerboseConfig() {
cfgBuilder.SetVerboseDebug(true, app.logger)
}
for _, feeder := range effectiveFeeders {
cfgBuilder.AddFeeder(feeder)
if app.IsVerboseConfig() {
app.logger.Debug("Added config feeder to builder", "type", fmt.Sprintf("%T", feeder))
}
}
// Process configs
tempConfigs, hasConfigs := processConfigs(app, cfgBuilder)
// If no valid configs found, return early
if !hasConfigs {
app.logger.Info("No valid configs found, skipping config loading")
return nil
}
if app.IsVerboseConfig() {
app.logger.Debug("Configuration structures prepared for feeding", "count", len(tempConfigs))
}
// Feed all configs at once
if err := cfgBuilder.Feed(); err != nil {
if app.IsVerboseConfig() {
app.logger.Debug("Configuration feeding failed", "error", err)
}
return err
}
// Apply instance-aware feeding for supported configurations AFTER regular feeding
if err := applyInstanceAwareFeeding(app, tempConfigs); err != nil {
if app.IsVerboseConfig() {
app.logger.Debug("Instance-aware feeding failed", "error", err)
}
return err
}
if app.IsVerboseConfig() {
app.logger.Debug("Configuration feeding completed successfully")
}
// Apply updated configs
applyConfigUpdates(app, tempConfigs)
if app.IsVerboseConfig() {
app.logger.Debug("Configuration loading process completed")
}
return nil
}
// processConfigs handles the collection and preparation of configs
func processConfigs(app *StdApplication, cfgBuilder *Config) (map[string]configInfo, bool) {
tempConfigs := make(map[string]configInfo)
hasConfigs := false
if app.IsVerboseConfig() {
app.logger.Debug("Processing configuration sections")
}
// Process main app config if provided
if processedMain := processMainConfig(app, cfgBuilder, tempConfigs); processedMain {
hasConfigs = true
}
// Process registered sections
if processedSections := processSectionConfigs(app, cfgBuilder, tempConfigs); processedSections {
hasConfigs = true
}
if app.IsVerboseConfig() {
app.logger.Debug("Configuration processing completed", "totalConfigs", len(tempConfigs), "hasValidConfigs", hasConfigs)
}
return tempConfigs, hasConfigs
}
// processMainConfig handles the main application config
func processMainConfig(app *StdApplication, cfgBuilder *Config, tempConfigs map[string]configInfo) bool {
if app.cfgProvider == nil {
if app.IsVerboseConfig() {
app.logger.Debug("Main config provider is nil, skipping main config")
}
return false
}
mainCfg := app.cfgProvider.GetConfig()
if mainCfg == nil {
app.logger.Warn("Main config is nil, skipping main config loading")
return false
}
if app.IsVerboseConfig() {
app.logger.Debug("Processing main configuration", "configType", reflect.TypeOf(mainCfg), "section", mainConfigSection)
}
tempMainCfg, mainCfgInfo, err := createTempConfig(mainCfg)
if err != nil {
app.logger.Warn("Failed to create temp config, skipping main config", "error", err)
return false
}
cfgBuilder.AddStructKey(mainConfigSection, tempMainCfg)
tempConfigs[mainConfigSection] = mainCfgInfo
app.logger.Debug("Added main config for loading", "type", reflect.TypeOf(mainCfg))
if app.IsVerboseConfig() {
app.logger.Debug("Main configuration prepared for feeding", "section", mainConfigSection)
}
return true
}
// processSectionConfigs handles the section configs
func processSectionConfigs(app *StdApplication, cfgBuilder *Config, tempConfigs map[string]configInfo) bool {
hasValidSections := false
if app.IsVerboseConfig() {
app.logger.Debug("Processing configuration sections", "totalSections", len(app.cfgSections))
}
for sectionKey, provider := range app.cfgSections {
if app.IsVerboseConfig() {
app.logger.Debug("Processing configuration section", "section", sectionKey, "providerType", fmt.Sprintf("%T", provider))
}
if provider == nil {
app.logger.Warn("Skipping nil config provider", "section", sectionKey)
continue
}
sectionCfg := provider.GetConfig()
if sectionCfg == nil {
app.logger.Warn("Skipping section with nil config", "section", sectionKey)
continue
}
if app.IsVerboseConfig() {
app.logger.Debug("Section config retrieved", "section", sectionKey, "configType", reflect.TypeOf(sectionCfg))
}
tempSectionCfg, sectionInfo, err := createTempConfig(sectionCfg)
if err != nil {
app.logger.Warn("Failed to create temp config for section, skipping",
"section", sectionKey, "error", err)
continue
}
cfgBuilder.AddStructKey(sectionKey, tempSectionCfg)
tempConfigs[sectionKey] = sectionInfo
hasValidSections = true
app.logger.Debug("Added section config for loading",
"section", sectionKey, "type", reflect.TypeOf(sectionCfg))
if app.IsVerboseConfig() {
app.logger.Debug("Section configuration prepared for feeding", "section", sectionKey)
}
}
if app.IsVerboseConfig() {
app.logger.Debug("Section configuration processing completed", "validSections", hasValidSections)
}
return hasValidSections
}
// applyConfigUpdates applies updates to all configs
func applyConfigUpdates(app *StdApplication, tempConfigs map[string]configInfo) {
// Update main config if it exists
if mainInfo, exists := tempConfigs[mainConfigSection]; exists {
updateConfig(app, mainInfo)
app.logger.Debug("Updated main config")
}
// Update section configs
for sectionKey, info := range tempConfigs {
if sectionKey == mainConfigSection {
continue
}
updateSectionConfig(app, sectionKey, info)
app.logger.Debug("Updated section config", "section", sectionKey)
}
}
// applyInstanceAwareFeeding applies instance-aware feeding to configurations that support it
func applyInstanceAwareFeeding(app *StdApplication, tempConfigs map[string]configInfo) error {
if app.IsVerboseConfig() {
app.logger.Debug("Starting instance-aware feeding process")
}
// Check each section for instance-aware config support
for sectionKey := range tempConfigs {
if sectionKey == mainConfigSection {
continue // Skip main config section for now
}
// Get the original provider to check if it's instance-aware
provider, exists := app.cfgSections[sectionKey]
if !exists {
continue
}
// Check if the provider is instance-aware
iaProvider, isInstanceAware := provider.(*InstanceAwareConfigProvider)
if !isInstanceAware {
if app.IsVerboseConfig() {
app.logger.Debug("Section provider is not instance-aware, skipping", "section", sectionKey)
}
continue
}
if app.IsVerboseConfig() {
app.logger.Debug("Processing instance-aware section", "section", sectionKey)
}
// Get the config from the temporary config that was just fed with YAML/ENV data
configInfo := tempConfigs[sectionKey]
var tempConfig any
if configInfo.isPtr {
tempConfig = configInfo.tempVal.Interface()
} else {
tempConfig = configInfo.tempVal.Elem().Interface()
}
// Check if it supports instance configurations
instanceSupport, supportsInstances := tempConfig.(InstanceAwareConfigSupport)
if !supportsInstances {
if app.IsVerboseConfig() {
app.logger.Debug("Config does not support instances, skipping", "section", sectionKey)
}
continue
}
// Get the instance configurations
instances := instanceSupport.GetInstanceConfigs()
if len(instances) == 0 {
if app.IsVerboseConfig() {
app.logger.Debug("No instances found for section", "section", sectionKey)
}
continue
}
if app.IsVerboseConfig() {
app.logger.Debug("Found instances for section", "section", sectionKey, "instanceCount", len(instances))
}
// Get the prefix function
prefixFunc := iaProvider.GetInstancePrefixFunc()
if prefixFunc == nil {
app.logger.Warn("Instance-aware provider missing prefix function", "section", sectionKey)
continue
}
// Create instance-aware feeder
instanceFeeder := NewInstanceAwareEnvFeeder(prefixFunc)
// Apply verbose debug if enabled
if app.IsVerboseConfig() {
if verboseFeeder, ok := instanceFeeder.(VerboseAwareFeeder); ok {
verboseFeeder.SetVerboseDebug(true, app.logger)
}
}