-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[FrameworkBundle][JsonPath] Add support for custom JsonPath functions #61517
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
base: 7.4
Are you sure you want to change the base?
[FrameworkBundle][JsonPath] Add support for custom JsonPath functions #61517
Conversation
42a26ef
to
1927fa4
Compare
src/Symfony/Component/JsonPath/Functions/AbstractJsonPathFunction.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionArgumentTrait.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionInterface.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionInterface.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionsProvider.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionArgumentTrait.php
Outdated
Show resolved
Hide resolved
b5b5819
to
3147335
Compare
Thanks, I addressed all your comments. I introduced a locator approach which should address your concerns in b5b5819 |
question: Instead of introducting a new way to define function can't we use the expression language as has already been done for the components routing and security ? |
This was actually my first idea. However, the RFC defines that you can add custom functions, not necessarily custom expressions. The way it's done currently allows to easily plug on the existing logic. Introducing expression language would bring many challenges (for example, binding JSON data and the path to the expression in some way). I think it's a better choice to not add the dependency and keep the component as standalone as possible (and it's also easier I think). However, nothing would stop one from defining an |
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.
Partial review, I only now realize #61132 should be merged first 😬
src/Symfony/Component/JsonPath/Functions/AsJsonPathFunction.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/AsJsonPathFunction.php
Outdated
Show resolved
Hide resolved
323351e
to
27495a8
Compare
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionArgumentTrait.php
Outdated
Show resolved
Hide resolved
b66d9a0
to
cbf332a
Compare
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionsProvider.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionsProvider.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionsProvider.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionsProvider.php
Outdated
Show resolved
Hide resolved
@@ -26,9 +25,11 @@ final class JsonPathTokenizer | |||
private const BARE_LITERAL_REGEX = '(true|false|null|\d+(\.\d+)?([eE][+-]?\d+)?|\'[^\']*\'|"[^"]*")'; | |||
|
|||
/** | |||
* @param callable(string $functionName, list<string> $args): void|null $functionValidator |
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.
* @param callable(string $functionName, list<string> $args): void|null $functionValidator | |
* @param callable(string $functionName, list<string> $args): void $functionValidator |
voisd|null
does not make sense
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.
It's actually that the param is either a callable returning void, either null. I agree it looks ambiguous, but if I'm correct, CS fixer forces this order.
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionInterface.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/JsonPathFunctionsProviderInterface.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/JsonPath/Functions/AbstractJsonPathFunction.php
Outdated
Show resolved
Hide resolved
cbf332a
to
8ea0f8a
Compare
To match the RFC and implement custom functions, |
If we need to expose Nothing as a non-internal API, I suggest reverting the change that replaced the single-case enum with an |
7309112
to
4fcfad9
Compare
4fcfad9
to
93aa3e4
Compare
Requires #61132. Only second commit (1927fa4) should be reviewed.
This PR allows to create custom JsonPath functions thanks to the
#[AsJsonPathFunction]
attribute:Then, it's used like this:
All RFC built-in functions are converted to use the new
AbstractJsonPathFunction
to avoid special case handling in the crawler code. This uses the approach detailed here: #60624 (comment). What's also nice is that built-in functions now have their very own unit tests as well.