ref 11
This commit is contained in:
parent
80500963b4
commit
3481962080
39
config.go
39
config.go
@ -4,7 +4,6 @@ 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
|
||||||
@ -53,23 +52,28 @@ func (c *Config) Get(key string) (any, error) {
|
|||||||
return c.data, nil
|
return c.data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(key, ".") {
|
// Parse the dot-notation path manually
|
||||||
if val, ok := c.data[key]; ok {
|
var start, i int
|
||||||
return val, nil
|
var current any = c.data
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("key %s not found", key)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(key, ".")
|
part := key[start:end]
|
||||||
current := any(c.data)
|
|
||||||
for _, part := range parts {
|
// Handle current node based on its type
|
||||||
switch node := current.(type) {
|
switch node := current.(type) {
|
||||||
case map[string]any:
|
case map[string]any:
|
||||||
var exists bool
|
val, ok := node[part]
|
||||||
current, exists = node[part]
|
if !ok {
|
||||||
if !exists {
|
|
||||||
return nil, fmt.Errorf("key %s not found", part)
|
return nil, fmt.Errorf("key %s not found", part)
|
||||||
}
|
}
|
||||||
|
current = val
|
||||||
|
|
||||||
case []any:
|
case []any:
|
||||||
index, err := strconv.Atoi(part)
|
index, err := strconv.Atoi(part)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -79,10 +83,21 @@ func (c *Config) Get(key string) (any, error) {
|
|||||||
return nil, fmt.Errorf("array index out of bounds: %d", index)
|
return nil, fmt.Errorf("array index out of bounds: %d", index)
|
||||||
}
|
}
|
||||||
current = node[index]
|
current = node[index]
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("cannot access %s in non-container value", part)
|
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 current, nil
|
return current, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user