This commit is contained in:
Sky Johnson 2025-03-04 19:15:05 -06:00
parent b334b09efa
commit 41b82c71cd

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
"strings"
) )
// Config holds a single hierarchical structure like JSON and handles parsing // Config holds a single hierarchical structure like JSON and handles parsing
@ -52,52 +53,36 @@ func (c *Config) Get(key string) (any, error) {
return c.data, nil return c.data, nil
} }
// Parse the dot-notation path manually if !strings.Contains(key, ".") {
var start, i int if val, ok := c.data[key]; ok {
var current any = c.data return val, nil
for i = 0; i < len(key); i++ {
if key[i] == '.' || i == len(key)-1 {
end := i
if i == len(key)-1 && key[i] != '.' {
end = i + 1
}
part := key[start:end]
// Handle current node based on its type
switch node := current.(type) {
case map[string]any:
val, ok := node[part]
if !ok {
return nil, fmt.Errorf("key %s not found", part)
}
current = val
case []any:
index, err := strconv.Atoi(part)
if err != nil {
return nil, fmt.Errorf("invalid array index: %s", part)
}
if index < 0 || index >= len(node) {
return nil, fmt.Errorf("array index out of bounds: %d", index)
}
current = node[index]
default:
return nil, fmt.Errorf("cannot access %s in non-container value", part)
}
if i == len(key)-1 || (i < len(key)-1 && key[i] == '.' && end == i) {
if i == len(key)-1 {
return current, nil
}
}
start = i + 1
} }
return nil, fmt.Errorf("key %s not found", key)
} }
parts := strings.Split(key, ".")
current := any(c.data)
for _, part := range parts {
switch node := current.(type) {
case map[string]any:
var exists bool
current, exists = node[part]
if !exists {
return nil, fmt.Errorf("key %s not found", part)
}
case []any:
index, err := strconv.Atoi(part)
if err != nil {
return nil, fmt.Errorf("invalid array index: %s", part)
}
if index < 0 || index >= len(node) {
return nil, fmt.Errorf("array index out of bounds: %d", index)
}
current = node[index]
default:
return nil, fmt.Errorf("cannot access %s in non-container value", part)
}
}
return current, nil return current, nil
} }