-
Notifications
You must be signed in to change notification settings - Fork 8
Cover expressions as function arguments #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3819,6 +3819,48 @@ | |
"selector": "$[-1:-10:-231584178474632390847141970017375815706539969331281128078915168015826259279872]", | ||
"invalid_selector": true | ||
}, | ||
{ | ||
"name": "functions, arguments, parenthesized expression", | ||
"selector": "$[?match((@.a), ('a.*'))]", | ||
"document": [ | ||
{ | ||
"a": "ab" | ||
}, | ||
{ | ||
"a": "ba" | ||
} | ||
], | ||
"result": [ | ||
{ | ||
"a": "ab" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "functions, arguments, function expression", | ||
"selector": "$.values[?match(@.a, value($..['regex']))]", | ||
"document": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless I've misunderstood (quite possible 😃),
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is good, but the path is invalid. It needs another
Tested at https://json-everything.net/json-path. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Looks like I have some issues with unbalanced bracket detection. |
||
"regex": "a.*", | ||
"values": [ | ||
{ | ||
"a": "ab" | ||
}, | ||
{ | ||
"a": "ba" | ||
} | ||
] | ||
}, | ||
"result": [ | ||
{ | ||
"a": "ab" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "functions, arguments, type mismatch, value type and logical type", | ||
"selector": "$[?length(@[email protected])>2]", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what this one is trying to do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intention was to test that a comparison expression can be a valid function argument, within the confines of the standard function extensions. I see that if If I'm not sure, and I can always define some mock functions that do accept a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We opted to separate the result of a boolean expression from the JSON literals For example, consider the complete expression I expect that this test case should generate an error. @glyn / @cabo can you confirm, please? What's the rule for syntactically correct but semantically incorrect paths? (I expect Section 2.1 has the requirements we're looking for.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, @gregsdennis , that makes sense. I've provisionally marked that test case as invalid and given it a more relevant name. Section 2.4.9 has some examples that cover of this kind of thing. I had missed that before. |
||
"invalid_selector": true | ||
}, | ||
{ | ||
"name": "functions, count, count function", | ||
"selector": "$[?count(@..*)>2]", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"tests": [ | ||
{ | ||
"name": "parenthesized expression", | ||
"selector": "$[?match((@.a), ('a.*'))]", | ||
"document" : [{"a": "ab"}, {"a": "ba"}], | ||
"result": [ | ||
{"a": "ab"} | ||
] | ||
}, | ||
{ | ||
"name": "function expression", | ||
"selector": "$.values[?match(@.a, value($..['regex']))]", | ||
"document" : {"regex": "a.*", "values": [{"a": "ab"}, {"a": "ba"}]}, | ||
"result": [ | ||
{"a": "ab"} | ||
] | ||
}, | ||
{ | ||
"name": "type mismatch, value type and logical type", | ||
"selector": "$[?length(@[email protected])>2]", | ||
"invalid_selector": true | ||
} | ||
] | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is correct. Here's how I see it. The parens around the arguments can only be used in a
logical-expr
, whose building blocks (apart from the parens,&&
,||
) arecomparison-expr
andtest-expr
. So@.a
and'a.*'
can only betest-expr
meaning they are ofLogicalType
. Butmatch()
only acceptsValueType
arguments, therefore the expression is not well-typed and the implementation must raise an error, according toSo, the expression should be treated as invalid.
P.S. So far there are no "standard" functions which accept
LogicalType
args. Which I think is good since boolean args may be considered a code smell.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see where you're going with this.I'm mobile right now, but I'd like to see how my implementations handles this. It can be tested at https://json-everything.net/json-path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gregsdennis

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One can argue that it could be better not to allow
logical-expr
inThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW my implementation did not allow parens in function args until now. This is a useful test, there's just no such standard function that can be used in it. In my implementation I introduced a non-standard logical function to have this case covered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, understood.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cabo does this mean that the check you showed has a bug, or am I interpreting this wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the problem is that a reasonable parser implementation will discard the () around
@.a
, so the fact that this was syntactically a logicaltype is not visible to the semantic processing.If I force this by doing one of
I get one of
Giving pure parentheses a type-coercing semantics is a flaw that overly complicated languages such as C++ have, but that I didn't know we had put into JSONPath. Now I have to think how to integrate this into jpt's parser/ast generator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced of this. Reasonability is subjective.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/reasonable parser/parser written by someone who has done expression language parsers before/
The AST gets a bit ugly when you need to include the parens as a node.
Of course, it can be done.