Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Fixed reverse() and string slicing to operate on UTF-8 characters rather than bytes.
* Fixed slicing of array-like (ArrayAccess + Countable) values.
* Fixed equality and contains() to use JSON semantics, e.g. 1 == 1.0 is now true.
* Fixed multi-select hashes to end projections, so following tokens apply to the projected list.
* Fixed sort() and sort_by() to compare numbers numerically.
* Changed sort(), sort_by(), max(), min(), max_by() and min_by() to order strings by code point.
* Fixed max_by() and min_by() to error on mixed-type keys instead of returning arbitrary elements.
Expand Down
2 changes: 1 addition & 1 deletion src/CompilerRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class CompilerRuntime
{
const CACHE_VERSION = 3;
const CACHE_VERSION = 4;

private $parser;
private $compiler;
Expand Down
4 changes: 4 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ private function parseDot($bp)
if ($this->token['type'] == T::T_LBRACKET) {
$this->next();
return $this->parseMultiSelectList();
} elseif ($this->token['type'] == T::T_LBRACE) {
// Like the multi-select list above, a multi-select hash ends any
// projection: tokens that follow apply to the projected list.
return $this->nud_lbrace();
}

return $this->expr($bp);
Expand Down
37 changes: 36 additions & 1 deletion tests/CompilerRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testFunctionNameUsesVersionedSalt(): void
{
$this->assertSame(
'jmespath_' . md5(
'jmespath:' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . ':3:foo.bar'
'jmespath:' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . ':4:foo.bar'
),
CompilerRuntime::functionName('foo.bar')
);
Expand Down Expand Up @@ -69,6 +69,41 @@ public function testRuntimesUseJsonSemanticEqualityForNumbers(): void
}
}

public function testRuntimesStopProjectionsAtMultiSelectHashes(): void
{
$dir = $this->createTempDir();
$data = [
'people' => [
['age' => 20, 'name' => 'Bob'],
['age' => 25, 'name' => 'Fred'],
['age' => 30, 'name' => 'George'],
],
];

try {
foreach ([new AstRuntime(), new CompilerRuntime($dir)] as $runtime) {
$this->assertSame(
[['name' => 'Fred'], ['name' => 'George']],
$runtime('people[?age >= `25`].{name: name}', $data)
);
$this->assertSame(
['name' => 'Fred'],
$runtime('people[?age >= `25`].{name: name}[0]', $data)
);
$this->assertSame(
'Fred',
$runtime('people[?age >= `25`].{name: name}[0].name', $data)
);
$this->assertNull($runtime('people[?age >= `25`].{name: name}.name', $data));

// Multi-select lists already ended projections; pin the symmetry.
$this->assertSame(['Bob'], $runtime('people[*].[name][0]', $data));
}
} finally {
$this->removeTempDir($dir);
}
}

public function testCompiledRuntimeSortsNumerically(): void
{
$dir = $this->createTempDir();
Expand Down
Loading