Skip to content
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

Allow filter keys with a regex match #59

Merged
merged 6 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ This is a [JSONPath](http://goessner.net/articles/JsonPath/) implementation for
This implementation features all elements in the specification except the `()` operator (in the spcecification there is the `$..a[(@.length-1)]`, but this can be achieved with `$..a[-1]` and the latter is simpler).

On top of this it implements some extended features:
* Regex match comparisons (p.e. `$.store.book[?(@.author =~ /.*Tolkien/)]`)
* Regex match key (p.e. `$.*[?(/^bo{2}k$/)]` or `$[?(/a\wthors/)]`).
* Regex match value comparisons (p.e. `$.store.book[?(@.author =~ /.*Tolkien/)]`)
* For the child operator `[]` there is no need to surround child names with quotes (p.e. `$.[store][book, bicycle]`) except if the name of the field is a non-valid javascript variable name.
* `.length` can be used to get the length of a string, get the length of an array and to check if a node has children.

Expand Down Expand Up @@ -96,11 +97,11 @@ JsonPath Language
=================
This library implements the following specification:
```
var_name = [\w\_\$^\d][\w\-\$]*
var_name = [filterexpr][\w\_\$^\d][\w\-\$]*
number = ([0-9]+(\.[0-9]*) | ([0-9]*\.[0-9]+))
string = ('\''.*?'\'' | '"'.*?'"')
boolean = ('true' | 'false')
regpattern = '/'.*?'/'
regpattern = '/'.*?'/i?x?'
null = 'null'
index = -?[0-9]+

Expand All @@ -116,7 +117,7 @@ childfilter = '[' ('*' | namelist | indexlist | arrayslice | filterexpr) ']'
namelist = var_name (',' (var_name | '\'' .*? '\'' | '"' .*? '"'))*
indexlist = index (',' index)*
arrayslice = index? ':' index? ':' index?
filterexpr = '?(' ors ')'
filterexpr = '?(' ors ' | regpattern)'

ors = ands (' ' ( 'or' | '\|\|' ) ' ' ands)*
ands = expr (' ' ( 'and' | '&&' ) ' ' expr)*
Expand Down Expand Up @@ -211,6 +212,7 @@ JsonPath | Result
`$[store]` | The store.
`$['store']` | The store.
`$..book[*][title, 'category', "author"]` | title, category and author of all books.
See more examples in the `./tests/Galbar/JsonPath` folder.

Test
====
Expand Down
2 changes: 1 addition & 1 deletion src/Galbar/JsonPath/Language/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Regex

// Conditional expressions
const EXPR_STRING = '/^(?:\'(.*)\'|"(.*)")$/';
const EXPR_REGEX = '/^\/.*\/$/';
const EXPR_REGEX = '/^\/.*\/i?x?$/';
const BINOP_COMP = '/^(.+)\s*(==|!=|<=|>=|<|>|=\~)\s*(.+)$/';
const BINOP_OR = '/\s+(or|\|\|)\s+/';
const BINOP_AND = '/\s+(and|&&)\s+/';
Expand Down
9 changes: 7 additions & 2 deletions src/Galbar/JsonPath/Operation/SelectChildren.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ function($x) { return intval(trim($x)); },
) {
$hasDiverged = true;
$subexpr = substr($contents, 2, $contentsLen - 3);
foreach ($partial as &$child) {
if (Expression\BooleanExpression::evaluate($root, $child, $subexpr)) {
$is_regex = preg_match(Language\Regex::EXPR_REGEX, $subexpr);
foreach ($partial as $key => &$child) {
if ($is_regex) {
if (preg_match($subexpr, $key)) {
$result[] = &$child;
}
} else if (Expression\BooleanExpression::evaluate($root, $child, $subexpr)) {
$result[] = &$child;
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Galbar/JsonPath/JsonObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ public function testGetProvider()
),
"$..*[?(@.author =~ /.*Tolkien/)].title"
),
array(
array(
"The Lord of the Rings"
),
"$..*[?(@.author =~ /.*tolkien/i)].title"
),
array(
array(
"The Lord of the Rings"
),
"$..*[?(@.author =~ / J.\ R.\ R.\ Tolkien /x)].title"
),
array(
array(
"red"
Expand Down
Loading