Skip to content

Commit

Permalink
Merge pull request #2 from tiopramayudi/fix-filter-in-nested-array
Browse files Browse the repository at this point in the history
Fix issue when filtering nested array
  • Loading branch information
tiopramayudi authored Apr 21, 2022
2 parents 37eac26 + 9346c2d commit bb91a19
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 138 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/jsonpath.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: ci
on:
push:
branches:
- master
tags:
- v*
pull_request:
jobs:
test:
runs-on: ubuntu-latest
name: Go ${{ matrix.go }} test
strategy:
matrix:
go: ["1.16", "1.17"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- run: go test -v ./...
57 changes: 15 additions & 42 deletions jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *Compiled) Lookup(obj interface{}) (interface{}, error) {
case "range":
if len(s.key) > 0 {
// no key `$[:1].test`
obj, err = get_key(obj, s.key)
obj, err = get_key_with_flattening(obj, s.key, true)
if err != nil {
return nil, err
}
Expand All @@ -127,7 +127,7 @@ func (c *Compiled) Lookup(obj interface{}) (interface{}, error) {
return nil, fmt.Errorf("range args length should be 2")
}
case "filter":
obj, err = get_key(obj, s.key)
obj, err = get_key_with_flattening(obj, s.key, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -325,7 +325,7 @@ func filter_get_from_explicit_path(obj interface{}, path string) (interface{}, e
return xobj, nil
}

func get_key(obj interface{}, key string) (interface{}, error) {
func get_key_with_flattening(obj interface{}, key string, isFlatten bool) (interface{}, error) {
if reflect.TypeOf(obj) == nil {
return nil, nil
}
Expand Down Expand Up @@ -354,6 +354,14 @@ func get_key(obj interface{}, key string) (interface{}, error) {
for i := 0; i < reflect.ValueOf(obj).Len(); i++ {
tmp, _ := get_idx(obj, i)
if v, err := get_key(tmp, key); err == nil {
keyVal := reflect.ValueOf(v)
// flatten the value is the result is array or slice
if keyVal.Kind() == reflect.Slice && isFlatten {
for j := 0; j < keyVal.Len(); j++ {
res = append(res, keyVal.Index(j).Interface())
}
continue
}
res = append(res, v)
}
}
Expand All @@ -363,6 +371,10 @@ func get_key(obj interface{}, key string) (interface{}, error) {
}
}

func get_key(obj interface{}, key string) (interface{}, error) {
return get_key_with_flattening(obj, key, false)
}

func get_idx(obj interface{}, idx int) (interface{}, error) {
switch reflect.TypeOf(obj).Kind() {
case reflect.Slice:
Expand Down Expand Up @@ -583,45 +595,6 @@ func parse_filter(filter string) (lp string, op string, rp string, err error) {
return lp, op, rp, err
}

func parse_filter_v1(filter string) (lp string, op string, rp string, err error) {
tmp := ""
istoken := false
for _, c := range filter {
if istoken == false && c != ' ' {
istoken = true
}
if istoken == true && c == ' ' {
istoken = false
}
if istoken == true {
tmp += string(c)
}
if istoken == false && tmp != "" {
if lp == "" {
lp = tmp[:]
tmp = ""
} else if op == "" {
op = tmp[:]
tmp = ""
} else if rp == "" {
rp = tmp[:]
tmp = ""
}
}
}
if tmp != "" && lp == "" && op == "" && rp == "" {
lp = tmp[:]
op = "exists"
rp = ""
err = nil
return
} else if tmp != "" && rp == "" {
rp = tmp[:]
tmp = ""
}
return lp, op, rp, err
}

func eval_reg_filter(obj, root interface{}, lp string, pat *regexp.Regexp) (res bool, err error) {
if pat == nil {
return false, errors.New("nil pat")
Expand Down
Loading

0 comments on commit bb91a19

Please sign in to comment.