Skip to content

Commit

Permalink
Merge branch 'master' of github.com:oliveagle/jsonpath
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveagle committed Jun 6, 2018
2 parents e5a552e + 1c146b9 commit 2e52cf6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,15 @@ func parse_token(token string) (op string, key string, args interface{}, err err
var frm interface{}
var to interface{}
if frm, err = strconv.Atoi(strings.Trim(tails[0], " ")); err != nil {
if strings.Trim(tails[0], " ") == "" {
err = nil
}
frm = nil
}
if to, err = strconv.Atoi(strings.Trim(tails[1], " ")); err != nil {
if strings.Trim(tails[1], " ") == "" {
err = nil
}
to = nil
}
args = [2]interface{}{frm, to}
Expand Down Expand Up @@ -391,6 +397,12 @@ func get_range(obj, frm, to interface{}) (interface{}, error) {
length := reflect.ValueOf(obj).Len()
_frm := 0
_to := length
if frm == nil {
frm = 0
}
if to == nil {
to = length - 1
}
if fv, ok := frm.(int); ok == true {
if fv < 0 {
_frm = length + fv
Expand Down
7 changes: 7 additions & 0 deletions jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ func Test_jsonpath_JsonPathLookup_1(t *testing.T) {
}
}

// full array
res, err = JsonPathLookup(json_data, "$.store.book[0:].price")
t.Log(err, res)
if res_v, ok := res.([]interface{}); ok != true || res_v[0].(float64) != 8.95 || res_v[1].(float64) != 12.99 || res_v[2].(float64) != 8.99 || res_v[3].(float64) != 22.99 {
t.Errorf("exp: [8.95, 12.99, 8.99, 22.99], got: %v", res)
}

// range
res, err = JsonPathLookup(json_data, "$.store.book[0:1].price")
t.Log(err, res)
Expand Down

0 comments on commit 2e52cf6

Please sign in to comment.