Skip to content

Commit

Permalink
#81 fix index corruption (#82)
Browse files Browse the repository at this point in the history
* #81 fix index corruption
  • Loading branch information
spyzhov authored Nov 25, 2024
1 parent b7c6942 commit 1f0ecf9
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 70 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
You can download `ajson` cli from the [release page](https://github.com/spyzhov/ajson/releases), or install from the source:

```shell script
go get github.com/spyzhov/ajson/cmd/ajson@v0.9.5
go get github.com/spyzhov/ajson/cmd/ajson@v0.9.6
```

Usage:
Expand Down Expand Up @@ -644,9 +644,9 @@ $ go test -bench=. -cpu=1 -benchmem
goos: linux
goarch: amd64
pkg: github.com/spyzhov/ajson
BenchmarkUnmarshal_AJSON 138032 8762 ns/op 5344 B/op 95 allocs/op
BenchmarkUnmarshal_JSON 117423 10502 ns/op 968 B/op 31 allocs/op
BenchmarkJSONPath_all_prices 80908 14394 ns/op 7128 B/op 153 allocs/op
BenchmarkUnmarshal_AJSON 121656 10060 ns/op 5712 B/op 118 allocs/op
BenchmarkUnmarshal_JSON 102674 11381 ns/op 960 B/op 32 allocs/op
BenchmarkJSONPath_all_prices 63314 16385 ns/op 7496 B/op 178 allocs/op
```

# License
Expand Down
2 changes: 1 addition & 1 deletion cmd/ajson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/spyzhov/ajson"
)

var version = "v0.9.5"
var version = "v0.9.6"

func usage() {
text := ``
Expand Down
13 changes: 12 additions & 1 deletion jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func eval(node *Node, expression rpn, cmd string) (result *Node, err error) {
return
}
if len(slice) > 1 { // array given
stack = append(stack, ArrayNode("", slice))
stack = append(stack, ArrayNode("", clone(slice)))
} else if len(slice) == 1 {
stack = append(stack, slice[0])
} else { // no data found
Expand Down Expand Up @@ -708,3 +708,14 @@ func getPositiveIndex(index int, count int) int {
}
return index
}

func clone(list []*Node) []*Node {
if list == nil {
return nil
}
result := make([]*Node, len(list))
for i, node := range list {
result[i] = node.Clone()
}
return result
}
53 changes: 53 additions & 0 deletions jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1664,3 +1664,56 @@ func TestEval_issue_69(t *testing.T) {
})
}
}

func TestEval_issue_81(t *testing.T) {
jb := []byte(`[{"a": 1, "b": 2, "c": 3}]`)
root, err := Unmarshal(jb)
if err != nil {
t.Log("Got error1:", err)
return
}
result, err := Eval(root, "$...")
if err != nil {
t.Errorf("Eval() error = %v", err)
return
}

arrs, err := result.GetArray()
if err != nil {
t.Errorf("result.GetArray() error = %v", err)
return
} else {
t.Log("Try:", arrs)
for i, arr := range arrs {
t.Logf("Try inner %d: %v", i, arr)
if arr.Type() == Array {
arr, err := arr.GetArray()
if err != nil {
t.Errorf("arr.GetArray() error = %v", err)
return
} else {
t.Log("Got inner:", arr)
}
}
}
}
}

func TestEval_issue_81_index_corruption_in_eval(t *testing.T) {
root, err := Unmarshal([]byte(`{"foo": [0, 1, 2, 3, 4]}`))
if err != nil {
t.Fatalf("Unmarshal() error = %v", err)
}
result, err := root.JSONPath(`$.foo[(last($.foo[1,2]))]`)
if err != nil {
t.Fatalf("JSONPath() error = %v", err)
}
if len(result) == 0 {
t.Fatalf("Result is nil")
}
for _, node := range result {
if node.Index() != int(node.MustNumeric()) {
t.Fatalf("Index is wrong for %v, index is %d", node, node.Index())
}
}
}
Loading

0 comments on commit 1f0ecf9

Please sign in to comment.