Fix up parser

This commit is contained in:
2025-03-25 10:38:12 +01:00
parent 7fc2956b6d
commit 533a563dc5

View File

@@ -1,4 +1,4 @@
package main package jsonpath
import ( import (
"fmt" "fmt"
@@ -27,12 +27,14 @@ func ParseJSONPath(path string) ([]JSONStep, error) {
return nil, fmt.Errorf("path must start with $") return nil, fmt.Errorf("path must start with $")
} }
path = path[1:]
steps := []JSONStep{} steps := []JSONStep{}
i := 0 i := 0
for i < len(path) { for i < len(path) {
switch path[i] { switch path[i] {
case '$':
steps = append(steps, JSONStep{Type: RootStep})
i++
case '.': case '.':
i++ i++
if i < len(path) && path[i] == '.' { if i < len(path) && path[i] == '.' {
@@ -51,7 +53,6 @@ func ParseJSONPath(path string) ([]JSONStep, error) {
} }
i = nextPos i = nextPos
} }
case '[': case '[':
// Index step // Index step
i++ i++
@@ -66,7 +67,6 @@ func ParseJSONPath(path string) ([]JSONStep, error) {
steps = append(steps, JSONStep{Type: IndexStep, Index: index}) steps = append(steps, JSONStep{Type: IndexStep, Index: index})
} }
i = nextPos + 1 // Skip closing ] i = nextPos + 1 // Skip closing ]
default: default:
return nil, fmt.Errorf("unexpected character: %c", path[i]) return nil, fmt.Errorf("unexpected character: %c", path[i])
} }