Skip to content

Commit 366a040

Browse files
committed
refactor: move query options to own package
1 parent 69298a0 commit 366a040

File tree

14 files changed

+55
-35
lines changed

14 files changed

+55
-35
lines changed

jsonpath.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77
"strings"
88

9+
"github.com/evilmonkeyinc/jsonpath/option"
910
"github.com/evilmonkeyinc/jsonpath/token"
1011
)
1112

@@ -40,7 +41,7 @@ func QueryString(queryPath string, jsonData string) (interface{}, error) {
4041
// JSONPath represents a compiled JSONPath query
4142
// and exposes functions to query JSON data and objects.
4243
type JSONPath struct {
43-
Options *token.Options
44+
Options *option.QueryOptions
4445
queryString string
4546
tokens []token.Token
4647
}

token/options.go renamed to option/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package token
1+
package option
22

3-
// Options represents optional functionality for the token parser that can be enabled or disabled.
3+
// QueryOptions represents optional functionality for the query functions that can be enabled or disabled.
44
//
55
// The default will be for all optional functionality to be disabled.
6-
type Options struct {
6+
type QueryOptions struct {
77
// AllowMapReferenceByIndex allow maps to be referenced by index in any token.
88
AllowMapReferenceByIndex bool
99
// AllowStringReferenceByIndex allow string characters to be referenced by index in any token.

script/script.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package script

token/expression.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import (
88
"regexp"
99
"strconv"
1010
"strings"
11+
12+
"github.com/evilmonkeyinc/jsonpath/option"
1113
)
1214

13-
func newExpressionToken(expression string, options *Options) *expressionToken {
15+
func newExpressionToken(expression string, options *option.QueryOptions) *expressionToken {
1416
return &expressionToken{expression: expression, options: options}
1517
}
1618

1719
type expressionToken struct {
1820
expression string
19-
options *Options
21+
options *option.QueryOptions
2022
}
2123

2224
func (token *expressionToken) String() string {
@@ -102,7 +104,7 @@ func getCurrentTokenIndex(expression string) int {
102104
/*
103105
1. regex
104106
*/
105-
func evaluateExpression(root, current interface{}, expression string, options *Options) (interface{}, error) {
107+
func evaluateExpression(root, current interface{}, expression string, options *option.QueryOptions) (interface{}, error) {
106108
if expression == "" {
107109
return nil, getInvalidExpressionEmptyError()
108110
}

token/expression_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/evilmonkeyinc/jsonpath/option"
78
"github.com/stretchr/testify/assert"
89
)
910

@@ -116,7 +117,7 @@ func Test_evaluateExpression(t *testing.T) {
116117
type input struct {
117118
root, current interface{}
118119
expression string
119-
options *Options
120+
options *option.QueryOptions
120121
}
121122

122123
type expected struct {

token/filter.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"fmt"
55
"reflect"
66
"strings"
7+
8+
"github.com/evilmonkeyinc/jsonpath/option"
79
)
810

9-
func newFilterToken(expression string, options *Options) *filterToken {
11+
func newFilterToken(expression string, options *option.QueryOptions) *filterToken {
1012
return &filterToken{expression: expression, options: options}
1113
}
1214

1315
type filterToken struct {
1416
expression string
15-
options *Options
17+
options *option.QueryOptions
1618
}
1719

1820
func (token *filterToken) String() string {

token/index.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package token
33
import (
44
"fmt"
55
"reflect"
6+
7+
"github.com/evilmonkeyinc/jsonpath/option"
68
)
79

8-
func newIndexToken(index int64, options *Options) *indexToken {
10+
func newIndexToken(index int64, options *option.QueryOptions) *indexToken {
911
allowMap := false
1012
allowString := false
1113

token/index_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/evilmonkeyinc/jsonpath/option"
78
"github.com/stretchr/testify/assert"
89
)
910

@@ -15,7 +16,7 @@ func Test_newIndexToken(t *testing.T) {
1516

1617
type input struct {
1718
index int64
18-
options *Options
19+
options *option.QueryOptions
1920
}
2021

2122
type expected *indexToken
@@ -38,7 +39,7 @@ func Test_newIndexToken(t *testing.T) {
3839
{
3940
input: input{
4041
index: 0,
41-
options: &Options{},
42+
options: &option.QueryOptions{},
4243
},
4344
expected: &indexToken{
4445
index: 0,
@@ -49,7 +50,7 @@ func Test_newIndexToken(t *testing.T) {
4950
{
5051
input: input{
5152
index: 0,
52-
options: &Options{
53+
options: &option.QueryOptions{
5354
AllowMapReferenceByIndex: false,
5455
AllowStringReferenceByIndex: false,
5556

@@ -66,7 +67,7 @@ func Test_newIndexToken(t *testing.T) {
6667
{
6768
input: input{
6869
index: 0,
69-
options: &Options{
70+
options: &option.QueryOptions{
7071
AllowMapReferenceByIndex: true,
7172
AllowStringReferenceByIndex: true,
7273

@@ -83,7 +84,7 @@ func Test_newIndexToken(t *testing.T) {
8384
{
8485
input: input{
8586
index: 0,
86-
options: &Options{
87+
options: &option.QueryOptions{
8788
AllowMapReferenceByIndex: true,
8889
AllowStringReferenceByIndex: true,
8990

@@ -100,7 +101,7 @@ func Test_newIndexToken(t *testing.T) {
100101
{
101102
input: input{
102103
index: 0,
103-
options: &Options{
104+
options: &option.QueryOptions{
104105
AllowMapReferenceByIndex: false,
105106
AllowStringReferenceByIndex: false,
106107

token/range.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package token
33
import (
44
"fmt"
55
"reflect"
6+
7+
"github.com/evilmonkeyinc/jsonpath/option"
68
)
79

8-
func newRangeToken(from, to, step interface{}, options *Options) *rangeToken {
10+
func newRangeToken(from, to, step interface{}, options *option.QueryOptions) *rangeToken {
911
allowMap := false
1012
allowString := false
1113

token/range_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/evilmonkeyinc/jsonpath/option"
78
"github.com/stretchr/testify/assert"
89
)
910

@@ -15,7 +16,7 @@ func Test_newRangeToken(t *testing.T) {
1516

1617
type input struct {
1718
to, from, step interface{}
18-
options *Options
19+
options *option.QueryOptions
1920
}
2021

2122
type expected *rangeToken
@@ -35,7 +36,7 @@ func Test_newRangeToken(t *testing.T) {
3536
},
3637
{
3738
input: input{
38-
options: &Options{},
39+
options: &option.QueryOptions{},
3940
},
4041
expected: &rangeToken{
4142
allowMap: false,
@@ -44,7 +45,7 @@ func Test_newRangeToken(t *testing.T) {
4445
},
4546
{
4647
input: input{
47-
options: &Options{
48+
options: &option.QueryOptions{
4849
AllowMapReferenceByIndex: false,
4950
AllowStringReferenceByIndex: false,
5051

@@ -59,7 +60,7 @@ func Test_newRangeToken(t *testing.T) {
5960
},
6061
{
6162
input: input{
62-
options: &Options{
63+
options: &option.QueryOptions{
6364
AllowMapReferenceByIndex: true,
6465
AllowStringReferenceByIndex: true,
6566

@@ -74,7 +75,7 @@ func Test_newRangeToken(t *testing.T) {
7475
},
7576
{
7677
input: input{
77-
options: &Options{
78+
options: &option.QueryOptions{
7879
AllowMapReferenceByIndex: true,
7980
AllowStringReferenceByIndex: true,
8081

@@ -89,7 +90,7 @@ func Test_newRangeToken(t *testing.T) {
8990
},
9091
{
9192
input: input{
92-
options: &Options{
93+
options: &option.QueryOptions{
9394
AllowMapReferenceByIndex: false,
9495
AllowStringReferenceByIndex: false,
9596

0 commit comments

Comments
 (0)