Skip to content

Commit c93bfcf

Browse files
committed
If JSONPath contains a wildcard for empty JSON, return a member not exist error.
update README.md : ErrorMemberNotExist is also throws in the case of arrays in JSON.
1 parent 5ce67ee commit c93bfcf

5 files changed

+15
-37
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ These error types define the corresponding symptom, as listed below:
8989

9090
#### Runtime errors from `Retrieve`, *`parser-functions`*
9191

92-
| Error type | Message format | Symptom |
93-
|------------------------|---------------------------------------------------|--------------------------------------------------------------------------------|
94-
| `ErrorMemberNotExist` | `member did not exist (path=%s)` | The object member specified in the JSONPath did not exist in the JSON object. |
95-
| `ErrorIndexOutOfRange` | `index out of range (path=%s)` | The array indexes specified in the JSONPath were out of range. |
96-
| `ErrorTypeUnmatched` | `type unmatched (expected=%s, found=%s, path=%s)` | The node type specified in the JSONPath did not exist in the JSON object. |
97-
| `ErrorNoneMatched` | `none matched (path=%s)` | The retrieving child paths specified in the JSONPath resulted in empty output. |
98-
| `ErrorFunctionFailed` | `function failed (function=%s, error=%s)` | The function specified in the JSONPath failed. |
92+
| Error type | Message format | Symptom |
93+
|------------------------|---------------------------------------------------|-------------------------------------------------------------------------------------|
94+
| `ErrorMemberNotExist` | `member did not exist (path=%s)` | The object/array member specified in the JSONPath did not exist in the JSON object. |
95+
| `ErrorIndexOutOfRange` | `index out of range (path=%s)` | The array indexes specified in the JSONPath were out of range. |
96+
| `ErrorTypeUnmatched` | `type unmatched (expected=%s, found=%s, path=%s)` | The node type specified in the JSONPath did not exist in the JSON object. |
97+
| `ErrorNoneMatched` | `none matched (path=%s)` | The retrieving child paths specified in the JSONPath resulted in empty output. |
98+
| `ErrorFunctionFailed` | `function failed (function=%s, error=%s)` | The function specified in the JSONPath failed. |
9999

100100
The type checking is convenient to recognize which error happened.
101101

syntax_node_identifier_child_multi.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func (i *syntaxChildMultiIdentifier) retrieve(
5757

5858
if len(*result) == 0 {
5959
switch len(childErrorMap) {
60+
case 0:
61+
return ErrorMemberNotExist{path: i.text}
6062
case 1:
6163
return lastError
6264
default:
@@ -72,7 +74,6 @@ func (i *syntaxChildMultiIdentifier) retrieveMap(
7274
childErrorMap map[error]struct{}) error {
7375

7476
var lastError error
75-
var partialFound bool
7677

7778
for _, identifier := range i.identifiers {
7879
var found bool
@@ -86,7 +87,6 @@ func (i *syntaxChildMultiIdentifier) retrieveMap(
8687
}
8788

8889
if found {
89-
partialFound = true
9090
if err := identifier.retrieve(root, srcMap, result); err != nil {
9191
childErrorMap[err] = struct{}{}
9292
lastError = err
@@ -95,11 +95,5 @@ func (i *syntaxChildMultiIdentifier) retrieveMap(
9595
}
9696
}
9797

98-
if !partialFound {
99-
err := ErrorMemberNotExist{path: i.text}
100-
childErrorMap[err] = struct{}{}
101-
lastError = err
102-
}
103-
10498
return lastError
10599
}

syntax_node_identifier_child_wildcard.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (i *syntaxChildWildcardIdentifier) retrieve(
2424
if len(*result) == 0 {
2525
switch len(childErrorMap) {
2626
case 0:
27-
return ErrorNoneMatched{path: i.text}
27+
return ErrorMemberNotExist{path: i.text}
2828
case 1:
2929
return lastError
3030
default:

syntax_node_qualifier_filter.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (f *syntaxFilterQualifier) retrieve(
2626
if len(*result) == 0 {
2727
switch len(childErrorMap) {
2828
case 0:
29-
return ErrorNoneMatched{path: f.text}
29+
return ErrorMemberNotExist{path: f.text}
3030
case 1:
3131
return lastError
3232
default:
@@ -42,7 +42,6 @@ func (f *syntaxFilterQualifier) retrieveMap(
4242
childErrorMap map[error]struct{}) error {
4343

4444
var lastError error
45-
var partialFound bool
4645

4746
index, keys := 0, make(sort.StringSlice, len(srcMap))
4847
for key := range srcMap {
@@ -67,20 +66,13 @@ func (f *syntaxFilterQualifier) retrieveMap(
6766
_, nodeNotFound = valueList[index].(struct{})
6867
}
6968
if !nodeNotFound {
70-
partialFound = true
7169
if err := f.retrieveMapNext(root, srcMap, keys[index], result); err != nil {
7270
childErrorMap[err] = struct{}{}
7371
lastError = err
7472
}
7573
}
7674
}
7775

78-
if !partialFound {
79-
err := ErrorMemberNotExist{path: f.text}
80-
childErrorMap[err] = struct{}{}
81-
lastError = err
82-
}
83-
8476
return lastError
8577
}
8678

@@ -89,7 +81,6 @@ func (f *syntaxFilterQualifier) retrieveList(
8981
childErrorMap map[error]struct{}) error {
9082

9183
var lastError error
92-
var partialFound bool
9384

9485
valueList := f.query.compute(root, srcList)
9586

@@ -101,19 +92,12 @@ func (f *syntaxFilterQualifier) retrieveList(
10192
_, nodeNotFound = valueList[index].(struct{})
10293
}
10394
if !nodeNotFound {
104-
partialFound = true
10595
if err := f.retrieveListNext(root, srcList, index, result); err != nil {
10696
childErrorMap[err] = struct{}{}
10797
lastError = err
10898
}
10999
}
110100
}
111101

112-
if !partialFound {
113-
err := ErrorMemberNotExist{path: f.text}
114-
childErrorMap[err] = struct{}{}
115-
lastError = err
116-
}
117-
118102
return lastError
119103
}

test_jsonpath_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,12 @@ func TestRetrieve_dotNotation_wildcard(t *testing.T) {
768768
{
769769
jsonpath: `$.*`,
770770
inputJSON: `{}`,
771-
expectedErr: ErrorNoneMatched{path: `.*`},
771+
expectedErr: ErrorMemberNotExist{path: `.*`},
772772
},
773773
{
774774
jsonpath: `$.*`,
775775
inputJSON: `[]`,
776-
expectedErr: ErrorNoneMatched{path: `.*`},
776+
expectedErr: ErrorMemberNotExist{path: `.*`},
777777
},
778778
},
779779
`recursive`: []TestCase{
@@ -1412,12 +1412,12 @@ func TestRetrieve_bracketNotation_wildcard(t *testing.T) {
14121412
{
14131413
jsonpath: `$[*]`,
14141414
inputJSON: `[]`,
1415-
expectedErr: ErrorNoneMatched{path: `[*]`},
1415+
expectedErr: ErrorMemberNotExist{path: `[*]`},
14161416
},
14171417
{
14181418
jsonpath: `$[*]`,
14191419
inputJSON: `{}`,
1420-
expectedErr: ErrorNoneMatched{path: `[*]`},
1420+
expectedErr: ErrorMemberNotExist{path: `[*]`},
14211421
},
14221422
},
14231423
`apply-to-value-group`: []TestCase{

0 commit comments

Comments
 (0)