@@ -195,7 +195,14 @@ func (c *Config) GetPackages(ctx context.Context) ([]string, error) {
195
195
return packageList , nil
196
196
}
197
197
198
+ // getPackageConfigMap returns the map for the particular package, which includes
199
+ // (but is not limited to) both the `configs` section and the `interfaces` section.
200
+ // Note this does NOT return the `configs` section for the package. It returns the
201
+ // entire mapping for the package.
198
202
func (c * Config ) getPackageConfigMap (ctx context.Context , packageName string ) (map [string ]any , error ) {
203
+ log := zerolog .Ctx (ctx )
204
+ log .Trace ().Msg ("getting package config map" )
205
+
199
206
cfgMap , err := c .CfgAsMap (ctx )
200
207
if err != nil {
201
208
return nil , err
@@ -207,8 +214,10 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m
207
214
}
208
215
configAsMap , isMap := configUnmerged .(map [string ]any )
209
216
if isMap {
217
+ log .Trace ().Msg ("package's value is a map, returning" )
210
218
return configAsMap , nil
211
219
}
220
+ log .Trace ().Msg ("package's value is not a map" )
212
221
213
222
// Package is something other than map, so set its value to an
214
223
// empty map.
@@ -248,7 +257,7 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con
248
257
configSection , ok := configMap ["config" ]
249
258
if ! ok {
250
259
log .Debug ().Msg ("config section not provided for package" )
251
- configMap ["config" ] = deepCopyConfigMap ( c . _cfgAsMap )
260
+ configMap ["config" ] = map [ string ] any {}
252
261
c .pkgConfigCache [packageName ] = pkgConfig
253
262
return pkgConfig , nil
254
263
}
@@ -445,6 +454,7 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
445
454
log := zerolog .Ctx (ctx ).With ().
446
455
Str ("parent-package" , parentPkgPath ).
447
456
Str ("sub-package" , subPkgPath ).Logger ()
457
+ ctx = log .WithContext (ctx )
448
458
449
459
log .Debug ().Msg ("adding sub-package to config map" )
450
460
parentPkgConfig , err := c .getPackageConfigMap (ctx , parentPkgPath )
@@ -463,13 +473,14 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
463
473
log .Debug ().Msg ("getting packages section" )
464
474
packagesSection := topLevelConfig ["packages" ].(map [string ]any )
465
475
466
- // Don't overwrite any config that already exists
467
476
_ , pkgExists := packagesSection [subPkgPath ]
468
477
if ! pkgExists {
469
478
log .Trace ().Msg ("sub-package doesn't exist in config" )
479
+
480
+ // Copy the parent package directly into the subpackage config section
470
481
packagesSection [subPkgPath ] = map [string ]any {}
471
482
newPkgSection := packagesSection [subPkgPath ].(map [string ]any )
472
- newPkgSection ["config" ] = parentPkgConfig ["config" ]
483
+ newPkgSection ["config" ] = deepCopyConfigMap ( parentPkgConfig ["config" ].( map [ string ] any ))
473
484
} else {
474
485
log .Trace ().Msg ("sub-package exists in config" )
475
486
// The sub-package exists in config. Check if it has its
@@ -481,10 +492,15 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
481
492
log .Err (err ).Msg ("could not get child package config" )
482
493
return fmt .Errorf ("failed to get sub-package config: %w" , err )
483
494
}
484
-
485
- for key , val := range parentPkgConfig {
486
- if _ , keyInSubPkg := subPkgConfig [key ]; ! keyInSubPkg {
487
- subPkgConfig [key ] = val
495
+ log .Trace ().Msgf ("sub-package config: %v" , subPkgConfig )
496
+ log .Trace ().Msgf ("parent-package config: %v" , parentPkgConfig )
497
+
498
+ // Merge the parent config with the sub-package config.
499
+ parentConfigSection := parentPkgConfig ["config" ].(map [string ]any )
500
+ subPkgConfigSection := subPkgConfig ["config" ].(map [string ]any )
501
+ for key , val := range parentConfigSection {
502
+ if _ , keyInSubPkg := subPkgConfigSection [key ]; ! keyInSubPkg {
503
+ subPkgConfigSection [key ] = val
488
504
}
489
505
490
506
}
@@ -629,7 +645,6 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error {
629
645
recursivePackages [pkg ] = pkgConfig
630
646
} else {
631
647
pkgLog .Trace ().Msg ("package not marked as recursive" )
632
- pkgLog .Trace ().Msg (fmt .Sprintf ("%+v" , pkgConfig ))
633
648
}
634
649
}
635
650
if len (recursivePackages ) == 0 {
@@ -711,13 +726,15 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
711
726
}
712
727
for _ , pkgPath := range pkgs {
713
728
pkgLog := log .With ().Str ("package-path" , pkgPath ).Logger ()
729
+ pkgCtx := pkgLog .WithContext (ctx )
730
+
714
731
pkgLog .Trace ().Msg ("merging for package" )
715
- packageConfig , err := c .getPackageConfigMap (ctx , pkgPath )
732
+ packageConfig , err := c .getPackageConfigMap (pkgCtx , pkgPath )
716
733
if err != nil {
717
734
pkgLog .Err (err ).Msg ("failed to get package config" )
718
735
return fmt .Errorf ("failed to get package config: %w" , err )
719
736
}
720
- pkgLog .Trace ().Msg ("got package config map" )
737
+ pkgLog .Trace ().Msgf ("got package config map: %v" , packageConfig )
721
738
722
739
configSectionUntyped , configExists := packageConfig ["config" ]
723
740
if ! configExists {
@@ -759,12 +776,12 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
759
776
configSectionTyped [key ] = value
760
777
}
761
778
}
762
- interfaces , err := c .getInterfacesForPackage (ctx , pkgPath )
779
+ interfaces , err := c .getInterfacesForPackage (pkgCtx , pkgPath )
763
780
if err != nil {
764
781
return fmt .Errorf ("failed to get interfaces for package: %w" , err )
765
782
}
766
783
for _ , interfaceName := range interfaces {
767
- interfacesSection , err := c .getInterfacesSection (ctx , pkgPath )
784
+ interfacesSection , err := c .getInterfacesSection (pkgCtx , pkgPath )
768
785
if err != nil {
769
786
return err
770
787
}
0 commit comments