Skip to content

Commit 272f1b3

Browse files
Merge pull request #2 from evilmonkeyinc/fix/query-primitives
feat: support query string and json primitives
2 parents 3e20670 + e60e930 commit 272f1b3

File tree

6 files changed

+102
-23
lines changed

6 files changed

+102
-23
lines changed

.github/workflows/release-build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
matrix:
1111
goos: [linux, windows, darwin]
1212
goarch: ["386", amd64, arm64]
13-
goversion: ["1.16", "1.17"]
13+
goversion: ["1.17"]
1414
exclude:
1515
- goarch: "386"
1616
goos: darwin

jsonpath.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jsonpath
33
import (
44
"encoding/json"
55
"fmt"
6+
"strconv"
67
"strings"
78

89
"github.com/evilmonkeyinc/jsonpath/token"
@@ -95,19 +96,42 @@ func (query *JSONPath) Query(root interface{}) (interface{}, error) {
9596
// QueryString will return the result of the JSONPath query applied against the specified JSON data.
9697
func (query *JSONPath) QueryString(jsonData string) (interface{}, error) {
9798
jsonData = strings.TrimSpace(jsonData)
99+
if jsonData == "" {
100+
return nil, getInvalidJSONData(errDataIsUnexpectedTypeOrNil)
101+
}
98102

99103
var root interface{}
104+
100105
if strings.HasPrefix(jsonData, "{") && strings.HasSuffix(jsonData, "}") {
106+
// object
101107
root = make(map[string]interface{})
108+
if err := json.Unmarshal([]byte(jsonData), &root); err != nil {
109+
return nil, getInvalidJSONData(err)
110+
}
102111
} else if strings.HasPrefix(jsonData, "[") && strings.HasSuffix(jsonData, "]") {
112+
// array
103113
root = make([]interface{}, 0)
114+
if err := json.Unmarshal([]byte(jsonData), &root); err != nil {
115+
return nil, getInvalidJSONData(err)
116+
}
117+
} else if len(jsonData) > 2 && strings.HasPrefix(jsonData, "\"") && strings.HasPrefix(jsonData, "\"") {
118+
// string
119+
root = jsonData[1 : len(jsonData)-1]
120+
} else if strings.ToLower(jsonData) == "true" {
121+
// bool true
122+
root = true
123+
} else if strings.ToLower(jsonData) == "false" {
124+
// bool false
125+
root = false
126+
} else if val, err := strconv.ParseInt(jsonData, 10, 64); err == nil {
127+
// integer
128+
root = val
129+
} else if val, err := strconv.ParseFloat(jsonData, 64); err == nil {
130+
// float
131+
root = val
104132
} else {
105133
return nil, getInvalidJSONData(errDataIsUnexpectedTypeOrNil)
106134
}
107135

108-
if err := json.Unmarshal([]byte(jsonData), &root); err != nil {
109-
return nil, getInvalidJSONData(err)
110-
}
111-
112136
return query.Query(root)
113137
}

jsonpath_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ func Test_JSONPath_QueryString(t *testing.T) {
900900
sampleQuery, _ := Compile("$.expensive")
901901
altSampleQuery, _ := Compile("$..author")
902902
lengthQuery, _ := Compile("$.length")
903+
rootQuery, _ := Compile("$")
903904

904905
type input struct {
905906
jsonPath *JSONPath
@@ -923,6 +924,60 @@ func Test_JSONPath_QueryString(t *testing.T) {
923924
err: "invalid data. unexpected type or nil",
924925
},
925926
},
927+
{
928+
input: input{
929+
jsonPath: rootQuery,
930+
jsonData: "42",
931+
},
932+
expected: expected{
933+
value: int64(42),
934+
},
935+
},
936+
{
937+
input: input{
938+
jsonPath: rootQuery,
939+
jsonData: "3.14",
940+
},
941+
expected: expected{
942+
value: float64(3.14),
943+
},
944+
},
945+
{
946+
input: input{
947+
jsonPath: rootQuery,
948+
jsonData: "true",
949+
},
950+
expected: expected{
951+
value: true,
952+
},
953+
},
954+
{
955+
input: input{
956+
jsonPath: rootQuery,
957+
jsonData: "false",
958+
},
959+
expected: expected{
960+
value: false,
961+
},
962+
},
963+
{
964+
input: input{
965+
jsonPath: rootQuery,
966+
jsonData: "not a json string",
967+
},
968+
expected: expected{
969+
err: "invalid data. unexpected type or nil",
970+
},
971+
},
972+
{
973+
input: input{
974+
jsonPath: rootQuery,
975+
jsonData: `"json string"`,
976+
},
977+
expected: expected{
978+
value: "json string",
979+
},
980+
},
926981
{
927982
input: input{
928983
jsonPath: &JSONPath{

test/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ This implementation would be closer to the `Scalar consensus` as it does not alw
154154
|`$.*.*`|`[[1, 2, 3], [4, 5, 6]]`|`[1 2 3 4 5 6]`|`[[1 2 3] [4 5 6]]`|:no_entry:|
155155
|`$..*`|`{ "key": "value", "another key": { "complex": "string", "primitives": [0, 1] } }`|`[string value 0 1 [0 1] map[complex:string primitives:[0 1]]]`|`[string value 0 1 [0 1] map[complex:string primitives:[0 1]]]`|:white_check_mark:|
156156
|`$..*`|`[ 40, null, 42 ]`|`[40 42 <nil>]`|`[40 42 <nil>]`|:white_check_mark:|
157-
|`$..*`|`42`|`nil`|`nil`|:white_check_mark:|
157+
|`$..*`|`42`|`[]`|`[]`|:white_check_mark:|
158158
|`$a`|`{"a": 1, "$a": 2}`|`nil`|`nil`|:white_check_mark:|
159159
|`.key`|`{ "key": "value" }`|`nil`|`nil`|:white_check_mark:|
160160
|`key`|`{ "key": "value" }`|`nil`|`nil`|:white_check_mark:|
@@ -256,9 +256,9 @@ This implementation would be closer to the `Scalar consensus` as it does not alw
256256
|`$..`|`[{"a": {"b": "c"}}, [0, 1]]`|none|`[[map[a:map[b:c]] [0 1]] map[a:map[b:c]] map[b:c] c [0 1] 0 1]`|:question:|
257257
|`$.key..`|`{"some key": "value", "key": {"complex": "string", "primitives": [0, 1]}}`|none|`[map[complex:string primitives:[0 1]] [0 1] 0 1 string]`|:question:|
258258
|`$`|`{ "key": "value", "another key": { "complex": [ "a", 1 ] } }`|`map[another key:map[complex:[a 1]] key:value]`|`map[another key:map[complex:[a 1]] key:value]`|:white_check_mark:|
259-
|`$`|`42`|`42`|`nil`|:no_entry:|
260-
|`$`|`false`|`false`|`nil`|:no_entry:|
261-
|`$`|`true`|`false`|`nil`|:no_entry:|
259+
|`$`|`42`|`42`|`42`|:white_check_mark:|
260+
|`$`|`false`|`false`|`false`|:white_check_mark:|
261+
|`$`|`true`|`true`|`true`|:white_check_mark:|
262262
|`$[(@.length-1)]`|`["first", "second", "third", "forth", "fifth"]`|`nil`|`fifth`|:no_entry:|
263263

264264

test/dot_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ func Test_Dot(t *testing.T) {
342342
{
343343
query: `$..*`,
344344
data: `42`,
345-
expected: nil,
346-
consensus: nil,
347-
expectedError: "invalid data. unexpected type or nil",
345+
expected: []interface{}{},
346+
consensus: []interface{}{},
347+
expectedError: "",
348348
},
349349
{
350350
query: `$a`,

test/misc_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,25 @@ func Test_Misc(t *testing.T) {
4848
expectedError: "",
4949
},
5050
{
51-
query: `$`, // TODO : should we support more than [] and {}
51+
query: `$`,
5252
data: `42`,
53-
expected: nil,
54-
consensus: float64(42),
55-
expectedError: "invalid data. unexpected type or nil",
53+
expected: int64(42),
54+
consensus: int64(42),
55+
expectedError: "",
5656
},
5757
{
58-
query: `$`, // TODO : should we support more than [] and {}
58+
query: `$`,
5959
data: `false`,
60-
expected: nil,
60+
expected: false,
6161
consensus: false,
62-
expectedError: "invalid data. unexpected type or nil",
62+
expectedError: "",
6363
},
6464
{
65-
query: `$`, // TODO : should we support more than [] and {}
65+
query: `$`,
6666
data: `true`,
67-
expected: nil,
68-
consensus: false,
69-
expectedError: "invalid data. unexpected type or nil",
67+
expected: true,
68+
consensus: true,
69+
expectedError: "",
7070
},
7171
{
7272
query: `$[(@.length-1)]`,

0 commit comments

Comments
 (0)