fix!: SetEnvPrefix now re-reads env vars with prefix stripping

SetEnvPrefix was broken: it set the prefix string but never re-read
environment variables. This meant collapse() and SetDefault() couldn't
match prefixed env vars to their unprefixed config keys, so defaults
always won over env vars.

The fix makes SetEnvPrefix behave identically to WithEnvPrefix: it
re-reads os.Environ(), strips the prefix from matching keys, and
stores the stripped keys in envConfig. The envPrefix field is removed
entirely since keys are always pre-stripped.

BREAKING CHANGE: SetEnvPrefix now filters env vars to only those
matching the prefix (previously all env vars were accessible).
This matches the documented and expected behavior.
This commit is contained in:
2026-03-01 20:42:30 +00:00
parent 94b97a5825
commit 9ff1fdc5ee
3 changed files with 112 additions and 21 deletions

View File

@@ -12,7 +12,7 @@ func (c *ConfigManager) Get(key string) any {
defer c.mutex.RUnlock()
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(c.envPrefix+key)]
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return nil
}
@@ -25,7 +25,7 @@ func (c *ConfigManager) GetBool(key string) bool {
defer c.mutex.RUnlock()
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(c.envPrefix+key)]
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return false
}
@@ -56,7 +56,7 @@ func (c *ConfigManager) GetDuration(key string) time.Duration {
defer c.mutex.RUnlock()
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(c.envPrefix+key)]
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return 0
}
@@ -91,7 +91,7 @@ func (c *ConfigManager) GetString(key string) string {
defer c.mutex.RUnlock()
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(c.envPrefix+key)]
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return ""
}
@@ -110,7 +110,7 @@ func (c *ConfigManager) GetStringMap(key string) map[string]any {
defer c.mutex.RUnlock()
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(c.envPrefix+key)]
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return nil
}
@@ -128,7 +128,7 @@ func (c *ConfigManager) GetStringSlice(key string) []string {
defer c.mutex.RUnlock()
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(c.envPrefix+key)]
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return nil
}
@@ -157,7 +157,7 @@ func (c *ConfigManager) GetInt(key string) int {
defer c.mutex.RUnlock()
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(c.envPrefix+key)]
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return 0
}
@@ -189,7 +189,7 @@ func (c *ConfigManager) GetIntSlice(key string) []int {
defer c.mutex.RUnlock()
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(c.envPrefix+key)]
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return nil
}