This commit is contained in:
Daniil Gentili 2024-03-28 19:25:49 +01:00
parent 9784a2e1cf
commit cc61cec062
3 changed files with 64 additions and 16 deletions

View File

@ -18,7 +18,7 @@
namespace danog\AsyncOrm\Driver; namespace danog\AsyncOrm\Driver;
use ArrayObject; use ArrayIterator;
use danog\AsyncOrm\DbArray; use danog\AsyncOrm\DbArray;
use danog\AsyncOrm\FieldConfig; use danog\AsyncOrm\FieldConfig;
use danog\AsyncOrm\Settings\Database\Memory; use danog\AsyncOrm\Settings\Database\Memory;
@ -78,7 +78,7 @@ final class MemoryArray extends DbArray
public function getIterator(): \Traversable public function getIterator(): \Traversable
{ {
return new ArrayObject($this); return new ArrayIterator($this->data);
} }
public function getArrayCopy(): array public function getArrayCopy(): array

View File

@ -25,6 +25,7 @@ use danog\AsyncOrm\Driver\DriverArray;
use danog\AsyncOrm\FieldConfig; use danog\AsyncOrm\FieldConfig;
use danog\AsyncOrm\Internal\Serializer\IntString; use danog\AsyncOrm\Internal\Serializer\IntString;
use danog\AsyncOrm\Internal\Serializer\Passthrough; use danog\AsyncOrm\Internal\Serializer\Passthrough;
use danog\AsyncOrm\KeyType;
use danog\AsyncOrm\Serializer; use danog\AsyncOrm\Serializer;
use danog\AsyncOrm\Settings\Redis; use danog\AsyncOrm\Settings\Redis;
use danog\AsyncOrm\ValueType; use danog\AsyncOrm\ValueType;
@ -48,6 +49,7 @@ final class RedisArray extends DriverArray
private static ?LocalKeyedMutex $mutex = null; private static ?LocalKeyedMutex $mutex = null;
private readonly RedisClient $db; private readonly RedisClient $db;
private readonly bool $castToInt;
/** /**
* @param Serializer<TValue> $serializer * @param Serializer<TValue> $serializer
@ -60,6 +62,7 @@ final class RedisArray extends DriverArray
ValueType::STRING => new Passthrough, ValueType::STRING => new Passthrough,
default => $serializer default => $serializer
}; };
$this->castToInt = $config->keyType === KeyType::INT;
parent::__construct($config, $serializer); parent::__construct($config, $serializer);
self::$mutex ??= new LocalKeyedMutex; self::$mutex ??= new LocalKeyedMutex;
@ -147,7 +150,11 @@ final class RedisArray extends DriverArray
$len = \strlen($this->rKey('')); $len = \strlen($this->rKey(''));
foreach ($request as $key) { foreach ($request as $key) {
yield \substr($key, $len) => $this->serializer->deserialize($this->db->get($key)); $sub = \substr($key, $len);
if ($this->castToInt) {
$sub = (int) $sub;
}
yield $sub => $this->serializer->deserialize($this->db->get($key));
} }
} }

View File

@ -146,7 +146,7 @@ final class OrmTest extends TestCase
public function testBasic(Settings $settings, KeyType $keyType, string|int $key): void public function testBasic(Settings $settings, KeyType $keyType, string|int $key): void
{ {
$field = new FieldConfig( $field = new FieldConfig(
__METHOD__, 'testBasic',
$settings, $settings,
$keyType, $keyType,
ValueType::INT ValueType::INT
@ -176,6 +176,7 @@ final class OrmTest extends TestCase
$orm = $field->build(); $orm = $field->build();
$orm[$key] = 124; $orm[$key] = 124;
$this->assertCount(1, $orm);
$this->assertCount(1, $orm); $this->assertCount(1, $orm);
$this->assertSame(124, $orm[$key]); $this->assertSame(124, $orm[$key]);
$this->assertTrue(isset($orm[$key])); $this->assertTrue(isset($orm[$key]));
@ -222,7 +223,7 @@ final class OrmTest extends TestCase
public function testMigration(Settings $settings): void public function testMigration(Settings $settings): void
{ {
$field = new FieldConfig( $field = new FieldConfig(
__METHOD__, 'testMigration',
$settings, $settings,
KeyType::STRING_OR_INT, KeyType::STRING_OR_INT,
ValueType::INT ValueType::INT
@ -232,13 +233,55 @@ final class OrmTest extends TestCase
$this->assertSame(123, $orm[321]); $this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321])); $this->assertTrue(isset($orm[321]));
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame($orm instanceof MemoryArray ? 321 : "321", $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
if ($orm instanceof MemoryArray) { if ($orm instanceof MemoryArray) {
return; return;
} }
$field = new FieldConfig( $field = new FieldConfig(
__METHOD__, 'testMigration',
$settings,
KeyType::INT,
ValueType::INT
);
$orm = $field->build();
$this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321]));
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame(321, $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
$field = new FieldConfig(
'testMigration',
$settings,
KeyType::STRING,
ValueType::INT
);
$orm = $field->build();
$this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321]));
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame('321', $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
$field = new FieldConfig(
'testMigration',
$settings, $settings,
KeyType::INT, KeyType::INT,
ValueType::INT ValueType::INT
@ -247,15 +290,13 @@ final class OrmTest extends TestCase
$this->assertSame(123, $orm[321]); $this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321])); $this->assertTrue(isset($orm[321]));
$field = new FieldConfig( $cnt = 0;
__METHOD__, foreach ($orm as $kk => $vv) {
$settings, $cnt++;
KeyType::STRING, $this->assertSame(321, $kk);
ValueType::INT $this->assertSame(123, $vv);
); }
$orm = $field->build(); $this->assertEquals(1, $cnt);
$this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321]));
} }
public static function provideSettingsKeys(): \Generator public static function provideSettingsKeys(): \Generator
@ -274,7 +315,7 @@ final class OrmTest extends TestCase
yield [ yield [
$settings, $settings,
KeyType::STRING, KeyType::STRING,
4321, '4321',
]; ];
yield [ yield [
$settings, $settings,