mirror of
https://github.com/taigrr/jety.git
synced 2026-04-02 03:19:03 -07:00
The documented precedence is Set > env > file > defaults, but collapse() was using maps.Copy(ccm, mapConfig) which let file values (and Set values, stored in the same map) unconditionally overwrite env values. Split mapConfig into fileConfig (from ReadInConfig) and overrideConfig (from Set/SetString/SetBool). collapse() now applies layers in correct order: defaults, then file, then env (for known keys), then overrides. Added TestPrecedenceChain to verify the full layering.
47 lines
1.3 KiB
Go
47 lines
1.3 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}
|
|
if _, ok := c.overrideConfig[lower]; !ok {
|
|
if envVal, ok := c.envConfig[lower]; ok {
|
|
c.overrideConfig[lower] = ConfigMap{Key: key, Value: envVal.Value}
|
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: envVal.Value}
|
|
} else {
|
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
|
|
}
|
|
} else {
|
|
c.combinedConfig[lower] = c.overrideConfig[lower]
|
|
}
|
|
}
|