Skip to content

Commit

Permalink
Add tests for json path and improve implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
stirante committed Aug 19, 2023
1 parent daf1ec1 commit 2309507
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
3 changes: 3 additions & 0 deletions jsonte/types/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (o JsonArray) Index(i JsonType) (JsonType, error) {
return Null, burrito.WrappedErrorf("Index out of bounds: %d", index)
}
}
if b, ok := i.(JsonPath); ok {
return b.Get(o)
}
return Null, burrito.WrappedErrorf("Index must be a number: %s", i.StringValue())
}

Expand Down
2 changes: 1 addition & 1 deletion jsonte/types/json_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func AsJsonPath(obj interface{}) JsonPath {

func ParseJsonPath(path string) (JsonPath, error) {
path = strings.TrimPrefix(path, "#")
if !strings.HasPrefix(path, "/") {
if !strings.HasPrefix(path, "/") && !strings.HasPrefix(path, "[") {
path = "/" + path
}
parts := make([]JsonType, 0)
Expand Down
14 changes: 14 additions & 0 deletions test/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ func assertSemver(t *testing.T, eval jsonte.Result, expected types.Semver) {
}
}

func assertJsonPath(t *testing.T, eval jsonte.Result, expected types.JsonPath) {
assertAction(t, eval, types.Value)
if eval.Value == nil {
t.Fatalf("Result is null")
}
if !types.IsJsonPath(eval.Value) {
t.Fatalf("Result is not a JSON Path (%s)", reflect.TypeOf(eval.Value).Name())
}
path := types.AsJsonPath(eval.Value)
if path.StringValue() != expected.StringValue() {
t.Fatalf("Result is not correct (expected %s, got %s)", expected.StringValue(), path.StringValue())
}
}

func assertBool(t *testing.T, eval jsonte.Result, expected bool) {
assertAction(t, eval, types.Value)
if eval.Value == nil {
Expand Down
57 changes: 57 additions & 0 deletions test/jsonpath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package test

import (
"github.com/MCDevKit/jsonte/jsonte/types"
"testing"
)

func TestJsonPathParsing(t *testing.T) {
eval := evaluate(t, `jsonpath('#/test/path[1]')`)
assertJsonPath(t, eval, types.JsonPath{
Path: []types.JsonType{
types.JsonString{Value: "test"},
types.JsonString{Value: "path"},
types.AsNumber(1),
},
})
}

func TestJsonPathParsingWithoutHash(t *testing.T) {
eval := evaluate(t, `jsonpath('/test/path[1]')`)
assertJsonPath(t, eval, types.JsonPath{
Path: []types.JsonType{
types.JsonString{Value: "test"},
types.JsonString{Value: "path"},
types.AsNumber(1),
},
})
}

func TestJsonPathParent(t *testing.T) {
eval := evaluate(t, `jsonpath('test/path[1]').parent()`)
assertJsonPath(t, eval, types.JsonPath{
Path: []types.JsonType{
types.JsonString{Value: "test"},
types.JsonString{Value: "path"},
},
})
}

func TestJsonPathParsingNumber(t *testing.T) {
eval := evaluate(t, `jsonpath('#[1]')`)
assertJsonPath(t, eval, types.JsonPath{
Path: []types.JsonType{
types.AsNumber(1),
},
})
}

func TestObjectAccessByJsonPath(t *testing.T) {
eval := evaluate(t, `{"test": {"path": [1, 2, 3]}}[jsonpath('#/test/path[1]')]`)
assertNumber(t, eval, 2)
}

func TestArrayAccessByJsonPath(t *testing.T) {
eval := evaluate(t, `[1, 2, 3][jsonpath('#[1]')]`)
assertNumber(t, eval, 2)
}

0 comments on commit 2309507

Please sign in to comment.