Skip to content

Commit 1c146b9

Browse files
authored
Merge pull request #15 from creepfmd/colon-support
Colon support
2 parents 1338aad + 7ce34f6 commit 1c146b9

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

jsonpath.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,15 @@ func parse_token(token string) (op string, key string, args interface{}, err err
254254
var frm interface{}
255255
var to interface{}
256256
if frm, err = strconv.Atoi(strings.Trim(tails[0], " ")); err != nil {
257+
if strings.Trim(tails[0], " ") == "" {
258+
err = nil
259+
}
257260
frm = nil
258261
}
259262
if to, err = strconv.Atoi(strings.Trim(tails[1], " ")); err != nil {
263+
if strings.Trim(tails[1], " ") == "" {
264+
err = nil
265+
}
260266
to = nil
261267
}
262268
args = [2]interface{}{frm, to}
@@ -389,6 +395,12 @@ func get_range(obj, frm, to interface{}) (interface{}, error) {
389395
length := reflect.ValueOf(obj).Len()
390396
_frm := 0
391397
_to := length
398+
if frm == nil {
399+
frm = 0
400+
}
401+
if to == nil {
402+
to = length - 1
403+
}
392404
if fv, ok := frm.(int); ok == true {
393405
if fv < 0 {
394406
_frm = length + fv

jsonpath_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ func Test_jsonpath_JsonPathLookup_1(t *testing.T) {
9090
}
9191
}
9292

93+
// full array
94+
res, err = JsonPathLookup(json_data, "$.store.book[0:].price")
95+
t.Log(err, res)
96+
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 {
97+
t.Errorf("exp: [8.95, 12.99, 8.99, 22.99], got: %v", res)
98+
}
99+
93100
// range
94101
res, err = JsonPathLookup(json_data, "$.store.book[0:1].price")
95102
t.Log(err, res)

0 commit comments

Comments
 (0)