mirror of
https://github.com/taigrr/jety.git
synced 2026-04-02 03:19:03 -07:00
- Add Delete(key) to remove keys from all config layers - Add Sub(key) to get a scoped ConfigManager for nested sections - Update CodeQL workflow actions to v4/v5/v3 - Update govulncheck workflow checkout to v4 - Remove boilerplate comments from CodeQL workflow - Add comprehensive tests for both new methods - Update README with Sub() usage example and API docs
60 lines
1.8 KiB
Go
60 lines
1.8 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}
|
|
}
|
|
|
|
// Delete removes a key from all configuration layers (overrides, file,
|
|
// defaults) and rebuilds the combined configuration. Environment variables
|
|
// are not affected since they are loaded from the process environment.
|
|
func (c *ConfigManager) Delete(key string) {
|
|
c.mutex.Lock()
|
|
lower := strings.ToLower(key)
|
|
delete(c.overrideConfig, lower)
|
|
delete(c.fileConfig, lower)
|
|
delete(c.defaultConfig, lower)
|
|
delete(c.combinedConfig, lower)
|
|
c.mutex.Unlock()
|
|
}
|
|
|
|
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}
|
|
}
|
|
}
|