Skip to content

Commit

Permalink
#36 rename deReference function to ApplyJSONPath (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
spyzhov committed Oct 28, 2021
1 parent 891ea8c commit 32110c0
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v1.2.1
uses: golangci/golangci-lint-action@v2.5.2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.26
version: v1.42.1

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ go:
- 1.12.x
- 1.13.x
- 1.14.x
- 1.15.x
- 1.16.x

env:
- GO111MODULE=on
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
You can download `ajson` cli from the [release page](https://github.com/spyzhov/ajson/releases), or install from the source:

```shell script
go get github.com/spyzhov/ajson/cmd/ajson@v0.4.2
go get github.com/spyzhov/ajson/cmd/ajson@v0.5.0
```

Usage:
Expand Down
12 changes: 9 additions & 3 deletions jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func JSONPath(data []byte, path string) (result []*Node, err error) {
if err != nil {
return nil, err
}
return deReference(node, commands)
return ApplyJSONPath(node, commands)
}

// Paths returns calculated paths of underlying nodes
Expand Down Expand Up @@ -307,7 +307,13 @@ func ParseJSONPath(path string) (result []string, err error) {
return
}

func deReference(node *Node, commands []string) (result []*Node, err error) {
// ApplyJSONPath function applies commands sequence parse from JSONPath.
// Example:
//
// commands := []string{"$", "store", "book", "?(@.price < 10)", "title"}
// result, _ := ApplyJSONPath(node, commands)
//
func ApplyJSONPath(node *Node, commands []string) (result []*Node, err error) {
result = make([]*Node, 0)
var (
temporary []*Node
Expand Down Expand Up @@ -615,7 +621,7 @@ func eval(node *Node, expression rpn, cmd string) (result *Node, err error) {
if err != nil {
return
}
slice, err = deReference(node, commands)
slice, err = ApplyJSONPath(node, commands)
if err != nil {
return
}
Expand Down
95 changes: 95 additions & 0 deletions jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ajson
import (
"fmt"
"reflect"
"strconv"
"strings"
"testing"
)
Expand Down Expand Up @@ -1405,3 +1406,97 @@ func TestJSONPath_special_requests(t *testing.T) {
})
}
}

func TestApplyJSONPath(t *testing.T) {
node1 := NumericNode("", 1.)
node2 := NumericNode("", 2.)
cpy := func(n Node) *Node {
return &n
}
array := ArrayNode("", []*Node{cpy(*node1), cpy(*node2)})

type args struct {
node *Node
commands []string
}
tests := []struct {
name string
args args
wantResult []*Node
wantErr bool
}{
{
name: "nil",
args: args{
node: nil,
commands: nil,
},
wantResult: make([]*Node, 0),
wantErr: false,
},
{
name: "root",
args: args{
node: node1,
commands: []string{"$"},
},
wantResult: []*Node{node1},
wantErr: false,
},
{
name: "second",
args: args{
node: array,
commands: []string{"$", "1"},
},
wantResult: []*Node{array.children["1"]},
wantErr: false,
},
{
name: "both",
args: args{
node: array,
commands: []string{"$", "1,0"},
},
wantResult: []*Node{array.children["1"], array.children["0"]},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotResult, err := ApplyJSONPath(tt.args.node, tt.args.commands)
if (err != nil) != tt.wantErr {
t.Errorf("ApplyJSONPath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("ApplyJSONPath() gotResult = %v, want %v", gotResult, tt.wantResult)
}
})
}
}

func ExampleApplyJSONPath() {
json := `[
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
]`
node := Must(Unmarshal([]byte(json)))
for i := 0; i < 10; i++ {
key1 := strconv.Itoa(i)
key2 := strconv.Itoa(4 - i)
nodes, _ := ApplyJSONPath(node, []string{"$", key1, key2})
fmt.Printf("%s", nodes)
}

// Output:
// [4][3][2][1][0][9][8][7][6][5]
}
2 changes: 1 addition & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ func (n *Node) JSONPath(path string) (result []*Node, err error) {
if err != nil {
return nil, err
}
return deReference(n, commands)
return ApplyJSONPath(n, commands)
}

// root returns the root node
Expand Down

0 comments on commit 32110c0

Please sign in to comment.