Skip to content

Commit d3515d1

Browse files
committedNov 6, 2023
Fix bug with sub-package inheritance
Subpackages were not correctly inheriting their parent package configuration.
1 parent e86d230 commit d3515d1

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed
 

‎pkg/config/config.go

+29-12
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,14 @@ func (c *Config) GetPackages(ctx context.Context) ([]string, error) {
195195
return packageList, nil
196196
}
197197

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.
198202
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+
199206
cfgMap, err := c.CfgAsMap(ctx)
200207
if err != nil {
201208
return nil, err
@@ -207,8 +214,10 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m
207214
}
208215
configAsMap, isMap := configUnmerged.(map[string]any)
209216
if isMap {
217+
log.Trace().Msg("package's value is a map, returning")
210218
return configAsMap, nil
211219
}
220+
log.Trace().Msg("package's value is not a map")
212221

213222
// Package is something other than map, so set its value to an
214223
// empty map.
@@ -248,7 +257,7 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con
248257
configSection, ok := configMap["config"]
249258
if !ok {
250259
log.Debug().Msg("config section not provided for package")
251-
configMap["config"] = deepCopyConfigMap(c._cfgAsMap)
260+
configMap["config"] = map[string]any{}
252261
c.pkgConfigCache[packageName] = pkgConfig
253262
return pkgConfig, nil
254263
}
@@ -445,6 +454,7 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
445454
log := zerolog.Ctx(ctx).With().
446455
Str("parent-package", parentPkgPath).
447456
Str("sub-package", subPkgPath).Logger()
457+
ctx = log.WithContext(ctx)
448458

449459
log.Debug().Msg("adding sub-package to config map")
450460
parentPkgConfig, err := c.getPackageConfigMap(ctx, parentPkgPath)
@@ -463,13 +473,14 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
463473
log.Debug().Msg("getting packages section")
464474
packagesSection := topLevelConfig["packages"].(map[string]any)
465475

466-
// Don't overwrite any config that already exists
467476
_, pkgExists := packagesSection[subPkgPath]
468477
if !pkgExists {
469478
log.Trace().Msg("sub-package doesn't exist in config")
479+
480+
// Copy the parent package directly into the subpackage config section
470481
packagesSection[subPkgPath] = map[string]any{}
471482
newPkgSection := packagesSection[subPkgPath].(map[string]any)
472-
newPkgSection["config"] = parentPkgConfig["config"]
483+
newPkgSection["config"] = deepCopyConfigMap(parentPkgConfig["config"].(map[string]any))
473484
} else {
474485
log.Trace().Msg("sub-package exists in config")
475486
// 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
481492
log.Err(err).Msg("could not get child package config")
482493
return fmt.Errorf("failed to get sub-package config: %w", err)
483494
}
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
488504
}
489505

490506
}
@@ -629,7 +645,6 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error {
629645
recursivePackages[pkg] = pkgConfig
630646
} else {
631647
pkgLog.Trace().Msg("package not marked as recursive")
632-
pkgLog.Trace().Msg(fmt.Sprintf("%+v", pkgConfig))
633648
}
634649
}
635650
if len(recursivePackages) == 0 {
@@ -711,13 +726,15 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
711726
}
712727
for _, pkgPath := range pkgs {
713728
pkgLog := log.With().Str("package-path", pkgPath).Logger()
729+
pkgCtx := pkgLog.WithContext(ctx)
730+
714731
pkgLog.Trace().Msg("merging for package")
715-
packageConfig, err := c.getPackageConfigMap(ctx, pkgPath)
732+
packageConfig, err := c.getPackageConfigMap(pkgCtx, pkgPath)
716733
if err != nil {
717734
pkgLog.Err(err).Msg("failed to get package config")
718735
return fmt.Errorf("failed to get package config: %w", err)
719736
}
720-
pkgLog.Trace().Msg("got package config map")
737+
pkgLog.Trace().Msgf("got package config map: %v", packageConfig)
721738

722739
configSectionUntyped, configExists := packageConfig["config"]
723740
if !configExists {
@@ -759,12 +776,12 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
759776
configSectionTyped[key] = value
760777
}
761778
}
762-
interfaces, err := c.getInterfacesForPackage(ctx, pkgPath)
779+
interfaces, err := c.getInterfacesForPackage(pkgCtx, pkgPath)
763780
if err != nil {
764781
return fmt.Errorf("failed to get interfaces for package: %w", err)
765782
}
766783
for _, interfaceName := range interfaces {
767-
interfacesSection, err := c.getInterfacesSection(ctx, pkgPath)
784+
interfacesSection, err := c.getInterfacesSection(pkgCtx, pkgPath)
768785
if err != nil {
769786
return err
770787
}

‎pkg/config/config_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,7 @@ with-expecter: false
12061206
}
12071207
for _, tt := range tests {
12081208
t.Run(tt.name, func(t *testing.T) {
1209+
12091210
ctx := context.Background()
12101211
tmpdir := pathlib.NewPath(t.TempDir())
12111212
cfg := tmpdir.Join("config.yaml")

0 commit comments

Comments
 (0)
Please sign in to comment.