add dot-accessor notation

This commit is contained in:
2026-03-20 05:54:35 -04:00
parent a81a2027ae
commit 6ec3d79700
2 changed files with 204 additions and 0 deletions

View File

@@ -8,17 +8,71 @@ import (
)
// resolve looks up a key in combinedConfig, falling back to envConfig.
// It supports dot notation (e.g., "services.mas.server") to traverse nested maps.
func (c *ConfigManager) resolve(key string) (ConfigMap, bool) {
lower := strings.ToLower(key)
// First, try direct lookup (for top-level keys or keys without dots)
if v, ok := c.combinedConfig[lower]; ok {
return v, true
}
if v, ok := c.envConfig[lower]; ok {
return v, true
}
// If key contains dots, try traversing nested maps
if strings.Contains(lower, ".") {
if v, ok := c.resolveNested(lower, c.combinedConfig); ok {
return v, true
}
if v, ok := c.resolveNested(lower, c.envConfig); ok {
return v, true
}
}
return ConfigMap{}, false
}
// resolveNested traverses nested maps using dot-separated key paths.
func (c *ConfigManager) resolveNested(key string, config map[string]ConfigMap) (ConfigMap, bool) {
parts := strings.Split(key, ".")
if len(parts) < 2 {
return ConfigMap{}, false
}
// Look up the first part in the config
firstPart := parts[0]
entry, ok := config[firstPart]
if !ok {
return ConfigMap{}, false
}
// Traverse the remaining parts through nested maps
current := entry.Value
for i := 1; i < len(parts); i++ {
m, ok := current.(map[string]any)
if !ok {
return ConfigMap{}, false
}
// Try case-insensitive lookup in the nested map
part := parts[i]
found := false
for k, v := range m {
if strings.EqualFold(k, part) {
current = v
found = true
break
}
}
if !found {
return ConfigMap{}, false
}
}
return ConfigMap{Key: key, Value: current}, true
}
func (c *ConfigManager) Get(key string) any {
c.mutex.RLock()
defer c.mutex.RUnlock()