mirror of
https://github.com/taigrr/jety.git
synced 2026-04-02 03:19:03 -07:00
- collapse() now applies env vars for keys present in fileConfig, not just defaultConfig. Previously, env vars couldn't override file values unless a default was also set for that key. - SetDefault no longer pollutes overrideConfig; it correctly resolves the value by checking override > env > file > default. - Remove unused explicitDefaults field and UseExplicitDefaults method. - Add IsSet, AllKeys, AllSettings methods + package-level wrappers. - Add missing package-level wrappers: Get, SetBool, SetString, SetConfigDir, WithEnvPrefix. - Add tests for all new methods and the env-over-file-without-default fix.
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package jety
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
func (c *ConfigManager) SetBool(key string, value bool) {
|
|
c.mutex.Lock()
|
|
defer c.mutex.Unlock()
|
|
lower := strings.ToLower(key)
|
|
c.overrideConfig[lower] = ConfigMap{Key: key, Value: value}
|
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
|
|
}
|
|
|
|
func (c *ConfigManager) SetString(key string, value string) {
|
|
c.mutex.Lock()
|
|
defer c.mutex.Unlock()
|
|
lower := strings.ToLower(key)
|
|
c.overrideConfig[lower] = ConfigMap{Key: key, Value: value}
|
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
|
|
}
|
|
|
|
func (c *ConfigManager) Set(key string, value any) {
|
|
c.mutex.Lock()
|
|
defer c.mutex.Unlock()
|
|
lower := strings.ToLower(key)
|
|
c.overrideConfig[lower] = ConfigMap{Key: key, Value: value}
|
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
|
|
}
|
|
|
|
func (c *ConfigManager) SetDefault(key string, value any) {
|
|
c.mutex.Lock()
|
|
defer c.mutex.Unlock()
|
|
lower := strings.ToLower(key)
|
|
c.defaultConfig[lower] = ConfigMap{Key: key, Value: value}
|
|
// Update combinedConfig respecting precedence: override > env > file > default
|
|
if v, ok := c.overrideConfig[lower]; ok {
|
|
c.combinedConfig[lower] = v
|
|
} else if v, ok := c.envConfig[lower]; ok {
|
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: v.Value}
|
|
} else if v, ok := c.fileConfig[lower]; ok {
|
|
c.combinedConfig[lower] = v
|
|
} else {
|
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
|
|
}
|
|
}
|