Skip to content

Commit 2ce109a

Browse files
author
pentium10
committed
fix(datastore): infer TYPE_ID only for valid signed 64-bit integers
1 parent 8bb6105 commit 2ce109a

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎Datastore/src/Key.php‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,46 @@ private function determineIdentifierType($identifier, ?string $identifierType):
477477
));
478478
}
479479

480-
if (is_numeric($identifier)) {
480+
if ($this->isInt64($identifier)) {
481481
return self::TYPE_ID;
482482
}
483483

484484
return self::TYPE_NAME;
485485
}
486486

487+
/**
488+
* Check whether the given identifier can be represented as a signed 64-bit integer.
489+
*
490+
* @param mixed $identifier
491+
* @return bool
492+
*/
493+
private function isInt64($identifier): bool
494+
{
495+
if (is_int($identifier)) {
496+
return true;
497+
}
498+
499+
if (!is_string($identifier) || !preg_match('/^-?\d+$/', $identifier)) {
500+
return false;
501+
}
502+
503+
$negative = $identifier[0] === '-';
504+
$digits = ltrim($negative ? substr($identifier, 1) : $identifier, '0');
505+
if ($digits === '') {
506+
return true;
507+
}
508+
509+
$limit = $negative ? '9223372036854775808' : '9223372036854775807';
510+
$len = strlen($digits);
511+
$limitLen = strlen($limit);
512+
513+
if ($len !== $limitLen) {
514+
return $len < $limitLen;
515+
}
516+
517+
return strcmp($digits, $limit) <= 0;
518+
}
519+
487520
/**
488521
* Normalize the internal representation of a path
489522
*

‎Datastore/tests/Unit/KeyTest.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,36 @@ public function testPathElementForceType()
151151
$this->assertEquals(['kind' => 'Robots', 'id' => '1000'], $key->keyObject()['path'][1]);
152152
}
153153

154+
/**
155+
* @dataProvider inferredIdentifierTypeProvider
156+
*/
157+
public function testInferredIdentifierType($identifier, string $expectedType)
158+
{
159+
$key = new Key('foo');
160+
$key->pathElement('Robots', $identifier);
161+
162+
$this->assertEquals($expectedType, $key->pathEndIdentifierType());
163+
$this->assertEquals(['kind' => 'Robots', $expectedType => $identifier], $key->pathEnd());
164+
}
165+
166+
public function inferredIdentifierTypeProvider(): array
167+
{
168+
return [
169+
'int' => [1000, Key::TYPE_ID],
170+
'negative int' => [-42, Key::TYPE_ID],
171+
'numeric string' => ['1000', Key::TYPE_ID],
172+
'zero string' => ['0', Key::TYPE_ID],
173+
'int64 max string' => ['9223372036854775807', Key::TYPE_ID],
174+
'int64 min string' => ['-9223372036854775808', Key::TYPE_ID],
175+
'int64 max plus one' => ['9223372036854775808', Key::TYPE_NAME],
176+
'int64 min minus one' => ['-9223372036854775809', Key::TYPE_NAME],
177+
'32-digit numeric md5' => ['13975337727204308302793171870910', Key::TYPE_NAME],
178+
'scientific notation md5' => ['0e462097431906509019562988736854', Key::TYPE_NAME],
179+
'float string' => ['12.34', Key::TYPE_NAME],
180+
'alphanumeric string' => ['abc123', Key::TYPE_NAME],
181+
];
182+
}
183+
154184
public function testPathElementInvalidIdentifierType()
155185
{
156186
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)