Fix up the recursive descent

This commit is contained in:
2025-03-25 11:57:46 +01:00
parent 533a563dc5
commit 88887b9a12
2 changed files with 30 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ package jsonpath
import (
"fmt"
"log"
"strconv"
)
@@ -97,7 +98,7 @@ func readIndex(path string, start int) (string, int) {
func EvaluateJSONPath(data interface{}, steps []JSONStep) []interface{} {
current := []interface{}{data}
for _, step := range steps {
var next []interface{}
for _, node := range current {
@@ -105,7 +106,7 @@ func EvaluateJSONPath(data interface{}, steps []JSONStep) []interface{} {
}
current = next
}
return current
}
@@ -119,7 +120,10 @@ func evalStep(node interface{}, step JSONStep) []interface{} {
return evalWildcard(node)
case IndexStep:
return evalIndex(node, step.Index)
case RootStep:
return []interface{}{node}
default:
log.Println("Unknown step type:", step.Type)
return nil
}
}
@@ -141,12 +145,15 @@ func evalRecursiveDescent(node interface{}, targetKey string) []interface{} {
current := queue[0]
queue = queue[1:]
if m, ok := current.(map[string]interface{}); ok {
// Check if current level has target key
if targetKey == "*" {
results = append(results, current)
} else if m, ok := current.(map[string]interface{}); ok {
if val, exists := m[targetKey]; exists {
results = append(results, val)
}
// Add all children to queue
}
if m, ok := current.(map[string]interface{}); ok {
for _, v := range m {
queue = append(queue, v)
}