ref 10
This commit is contained in:
parent
b334b09efa
commit
41b82c71cd
71
config.go
71
config.go
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Parse the dot-notation path manually
|
||||
var start, i int
|
||||
var current any = c.data
|
||||
|
||||
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
|
||||
if !strings.Contains(key, ".") {
|
||||
if val, ok := c.data[key]; ok {
|
||||
return val, nil
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user