Skip to content

Commit 86137f4

Browse files
committed
chore: update readme with options
1 parent 7b1e2b3 commit 86137f4

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ This object is returned by the `Compile` function.
6767

6868
The JSONPath struct represents a reusable compiled JSONPath query which supports the `Query`, and `QueryString` functions as detailed above.
6969

70+
### Options
71+
72+
Part of the JSONPath object, Options allows you to specify what additional functionality, if any, that you want to enable while querying data.
73+
74+
You are able to enable index referencing support for maps for all tokens using `AllowMapReferenceByIndex` or use enable it for each token type individually.
75+
76+
You are able to enable index referencing support for strings for all tokens using `AllowStringReferenceByIndex` or use enable it for each token type individually.
77+
7078
## Supported Syntax
7179

7280
| syntax | name | example |
@@ -118,8 +126,6 @@ can also be used with the subscript syntax `$.store.book[*]`
118126

119127
allows for additional operators to be applied to the current object to retrieve a child element.
120128

121-
It is possible to use indexes to reference elements in a map, the order is determined by the keys in alphabetical order.
122-
123129
A negative value for an index is supported, resulting in the elements being counted in reverse, `-1` would represent the last item in the collection, `-2` the second last, and so on.
124130

125131
### Union
@@ -130,8 +136,6 @@ allows for a comma separated list of indices or keys to denote the elements to r
130136

131137
It is possible to use script expressions to define the union keys i.e. `$.store.book[0,(@.length-1)]` returns the first and last elements of the book collection.
132138

133-
It is possible to use indexes to reference elements in a map, the order is determined by the keys in alphabetical order.
134-
135139
A negative value for an index is supported, resulting in the elements being counted in reverse, `-1` would represent the last item in the collection, `-2` the second last, and so on.
136140

137141
### Range
@@ -185,11 +189,13 @@ the length token will allow you to return the length of an array, map, slice, or
185189

186190
If used with a map that has a key `length` it will return the corresponding value instead of the length of the map.
187191

188-
### Subscript, Union, and Range with strings
192+
### Subscript, Union, and Range with maps and strings
193+
194+
Using the Compile() function, and modifying the JSONPath Options, it is possible to use a map or a string in place of an array with the subscript `[1]` union `[1,2,3]` and range `[0:3]` operations.
189195

190-
It is possible to use a string in place of an array with the subscript `[1]` union `[1,2,3]` and range `[0:3]` operations, and instead of returning an array of characters instead will return a substring.
196+
For maps, the keys will be sorted into alphabetical order and they will be used to determine the index order. For example, if you had a map with strings `a` and `b`, regardless of the order, `a` would be the `0` index, and `b` the `1` index.
191197

192-
For example if you applied `[0:3]` to the string `string` it would return `str`.
198+
For strings instead of returning an array of characters instead will return a substring. For example if you applied `[0:3]` to the string `string` it would return `str`.
193199

194200
## Supported standard evaluation operations
195201

jsonpath_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,75 @@ func Test_Query(t *testing.T) {
780780
}
781781
}
782782

783+
func Test_JSONPath_String(t *testing.T) {
784+
785+
tests := []struct {
786+
input string
787+
expected string
788+
}{
789+
{
790+
input: "$.store.book[*].author",
791+
expected: "$['store']['book'][*]['author']",
792+
},
793+
{
794+
input: "$..author",
795+
expected: "$..['author']",
796+
},
797+
{
798+
input: "$.store.*",
799+
expected: "$['store'][*]",
800+
},
801+
{
802+
input: "$.store..price",
803+
expected: "$['store']..['price']",
804+
},
805+
{
806+
input: "$..book[2]",
807+
expected: "$..['book'][2]",
808+
},
809+
{
810+
input: "$..book[(@.length-1)]",
811+
expected: "$..['book'][(@.length-1)]",
812+
},
813+
{
814+
input: "$..book[-1:]",
815+
expected: "$..['book'][-1:]",
816+
},
817+
{
818+
input: "$..book[0,1]",
819+
expected: "$..['book'][0,1]",
820+
},
821+
{
822+
input: "$..book[:2]",
823+
expected: "$..['book'][:2]",
824+
},
825+
{
826+
input: "$..book[?(@.isbn)]",
827+
expected: "$..['book'][?(@.isbn)]",
828+
},
829+
{
830+
input: "$..book[?(@.price<10)]",
831+
expected: "$..['book'][?(@.price<10)]",
832+
},
833+
{
834+
input: "$..*",
835+
expected: "$..[*]",
836+
},
837+
{
838+
input: "$.store. book[0].author",
839+
expected: "$['store']['book'][0]['author']",
840+
},
841+
}
842+
843+
for idx, test := range tests {
844+
t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
845+
compiled, _ := Compile(test.input)
846+
assert.Equal(t, test.expected, compiled.String())
847+
})
848+
}
849+
850+
}
851+
783852
func Test_JSONPath_compile(t *testing.T) {
784853

785854
type expected struct {

token/token.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
"strings"
66
)
77

8+
/** Feature request
9+
support double quotes in keys?
10+
**/
11+
812
// Token represents a component of a JSON Path query
913
type Token interface {
1014
Apply(root, current interface{}, next []Token) (interface{}, error)

0 commit comments

Comments
 (0)