Skip to content

Bh/quoted col fix #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/oliveagle/jsonpath

go 1.15

require github.com/stretchr/testify v1.7.0
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23 changes: 23 additions & 0 deletions jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,27 @@ func tokenize(query string) ([]string, error) {
// token_start := false
// token_end := false
token := ""
quoteChar := rune(0)

// fmt.Println("-------------------------------------------------- start")
for idx, x := range query {
if quoteChar != 0 {
if x == quoteChar {
quoteChar = 0
} else {
token += string(x)
}

continue
} else if x == '"' {
if token == "." {
token = ""
}

quoteChar = x
continue
}

token += string(x)
// //fmt.Printf("idx: %d, x: %s, token: %s, tokens: %v\n", idx, string(x), token, tokens)
if idx == 0 {
Expand Down Expand Up @@ -193,6 +211,11 @@ func tokenize(query string) ([]string, error) {
}
}
}

if quoteChar != 0 {
token = string(quoteChar) + token
}

if len(token) > 0 {
if token[0] == '.' {
token = token[1:]
Expand Down
122 changes: 41 additions & 81 deletions jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"reflect"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
)

var json_data interface{}
Expand Down Expand Up @@ -68,6 +70,12 @@ func Test_jsonpath_JsonPathLookup_1(t *testing.T) {
t.Errorf("$.store.book[0].price should be 8.95")
}

// quoted - single index
res, _ = JsonPathLookup(json_data, `$."store"."book"[0]."price"`)
if res_v, ok := res.(float64); ok != true || res_v != 8.95 {
t.Errorf(`$."store"."book"[0]."price" should be 8.95`)
}

// nagtive single index
res, _ = JsonPathLookup(json_data, "$.store.book[-1].isbn")
if res_v, ok := res.(string); ok != true || res_v != "0-395-19395-8" {
Expand Down Expand Up @@ -153,90 +161,42 @@ func Test_jsonpath_authors_of_all_books(t *testing.T) {
t.Log(res, expected)
}

var token_cases = []map[string]interface{}{
map[string]interface{}{
"query": "$..author",
"tokens": []string{"$", "*", "author"},
},
map[string]interface{}{
"query": "$.store.*",
"tokens": []string{"$", "store", "*"},
},
map[string]interface{}{
"query": "$.store..price",
"tokens": []string{"$", "store", "*", "price"},
},
map[string]interface{}{
"query": "$.store.book[*].author",
"tokens": []string{"$", "store", "book[*]", "author"},
},
map[string]interface{}{
"query": "$..book[2]",
"tokens": []string{"$", "*", "book[2]"},
},
map[string]interface{}{
"query": "$..book[(@.length-1)]",
"tokens": []string{"$", "*", "book[(@.length-1)]"},
},
map[string]interface{}{
"query": "$..book[0,1]",
"tokens": []string{"$", "*", "book[0,1]"},
},
map[string]interface{}{
"query": "$..book[:2]",
"tokens": []string{"$", "*", "book[:2]"},
},
map[string]interface{}{
"query": "$..book[?(@.isbn)]",
"tokens": []string{"$", "*", "book[?(@.isbn)]"},
},
map[string]interface{}{
"query": "$.store.book[?(@.price < 10)]",
"tokens": []string{"$", "store", "book[?(@.price < 10)]"},
},
map[string]interface{}{
"query": "$..book[?(@.price <= $.expensive)]",
"tokens": []string{"$", "*", "book[?(@.price <= $.expensive)]"},
},
map[string]interface{}{
"query": "$..book[?(@.author =~ /.*REES/i)]",
"tokens": []string{"$", "*", "book[?(@.author =~ /.*REES/i)]"},
},
map[string]interface{}{
"query": "$..book[?(@.author =~ /.*REES\\]/i)]",
"tokens": []string{"$", "*", "book[?(@.author =~ /.*REES\\]/i)]"},
},
map[string]interface{}{
"query": "$..*",
"tokens": []string{"$", "*"},
},
map[string]interface{}{
"query": "$....author",
"tokens": []string{"$", "*", "author"},
},
var token_cases = []struct {
query string
expected []string
}{
{"$..author", []string{"$", "*", "author"}},
{"$.store.*", []string{"$", "store", "*"}},
{"$.store..price", []string{"$", "store", "*", "price"}},
{"$.store.book[*].author", []string{"$", "store", "book[*]", "author"}},
{"$..book[2]", []string{"$", "*", "book[2]"}},
{"$..book[(@.length-1)]", []string{"$", "*", "book[(@.length-1)]"}},
{"$..book[0,1]", []string{"$", "*", "book[0,1]"}},
{"$..book[:2]", []string{"$", "*", "book[:2]"}},
{"$..book[?(@.isbn)]", []string{"$", "*", "book[?(@.isbn)]"}},
{"$.store.book[?(@.price < 10)]", []string{"$", "store", "book[?(@.price < 10)]"}},
{"$..book[?(@.price <= $.expensive)]", []string{"$", "*", "book[?(@.price <= $.expensive)]"}},
{"$..book[?(@.author =~ /.*REES/i)]", []string{"$", "*", "book[?(@.author =~ /.*REES/i)]"}},
{"$..book[?(@.author =~ /.*REES\\]/i)]", []string{"$", "*", "book[?(@.author =~ /.*REES\\]/i)]"}},
{"$..*", []string{"$", "*"}},
{"$....author", []string{"$", "*", "author"}},
{`$."col"`, []string{"$", "col"}},
{`$."col.with.dots"."sub.with.dots"`, []string{"$", "col.with.dots", "sub.with.dots"}},
{`$."unterminated`, []string{"$", `"unterminated`}},
{`$."col with spaces"."sub with spaces"`, []string{"$", "col with spaces", "sub with spaces"}},
}

func Test_jsonpath_tokenize(t *testing.T) {
for idx, tcase := range token_cases {
t.Logf("idx[%d], tcase: %v", idx, tcase)
query := tcase["query"].(string)
expected_tokens := tcase["tokens"].([]string)
tokens, err := tokenize(query)
t.Log(err, tokens, expected_tokens)
if len(tokens) != len(expected_tokens) {
t.Errorf("different length: (got)%v, (expected)%v", len(tokens), len(expected_tokens))
continue
}
for i := 0; i < len(expected_tokens); i++ {
if tokens[i] != expected_tokens[i] {
t.Errorf("not expected: [%d], (got)%v != (expected)%v", i, tokens[i], expected_tokens[i])
}
}
for _, tcase := range token_cases {
t.Run(tcase.query, func(t *testing.T) {
tokens, err := tokenize(tcase.query)
assert.NoError(t, err)
assert.Equal(t, tcase.expected, tokens)
})
}
}

var parse_token_cases = []map[string]interface{}{

map[string]interface{}{
"token": "$",
"op": "root",
Expand Down Expand Up @@ -1179,13 +1139,13 @@ func Test_jsonpath_rootnode_is_array_range(t *testing.T) {
t.Logf("idx: %v, v: %v", idx, v)
}
if len(ares) != 2 {
t.Fatal("len is not 2. got: %v", len(ares))
t.Fatalf("len is not 2. got: %v", len(ares))
}
if ares[0].(float64) != 12.34 {
t.Fatal("idx: 0, should be 12.34. got: %v", ares[0])
t.Fatalf("idx: 0, should be 12.34. got: %v", ares[0])
}
if ares[1].(float64) != 13.34 {
t.Fatal("idx: 0, should be 12.34. got: %v", ares[1])
t.Fatalf("idx: 0, should be 12.34. got: %v", ares[1])
}
}

Expand Down Expand Up @@ -1232,7 +1192,7 @@ func Test_jsonpath_rootnode_is_nested_array_range(t *testing.T) {
t.Logf("idx: %v, v: %v", idx, v)
}
if len(ares) != 2 {
t.Fatal("len is not 2. got: %v", len(ares))
t.Fatalf("len is not 2. got: %v", len(ares))
}

//FIXME: `$[:1].[0].test` got wrong result
Expand Down