From f34630aaaeb77a7a3d024ca833112eed75db3f1e Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 28 Mar 2024 19:53:01 +0100 Subject: [PATCH] Add support for more types --- src/Internal/Driver/MysqlArray.php | 3 ++ src/Internal/Driver/PostgresArray.php | 4 ++- src/Internal/Driver/RedisArray.php | 4 +++ src/Internal/Serializer/BoolInt.php | 47 +++++++++++++++++++++++++ src/Internal/Serializer/BoolString.php | 46 ++++++++++++++++++++++++ src/Internal/Serializer/FloatString.php | 40 +++++++++++++++++++++ tests/OrmTest.php | 5 ++- 7 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 src/Internal/Serializer/BoolInt.php create mode 100644 src/Internal/Serializer/BoolString.php create mode 100644 src/Internal/Serializer/FloatString.php diff --git a/src/Internal/Driver/MysqlArray.php b/src/Internal/Driver/MysqlArray.php index 1705c3f..2cfb6ad 100644 --- a/src/Internal/Driver/MysqlArray.php +++ b/src/Internal/Driver/MysqlArray.php @@ -25,6 +25,8 @@ use AssertionError; use danog\AsyncOrm\Driver\Mysql; use danog\AsyncOrm\Driver\SqlArray; use danog\AsyncOrm\FieldConfig; +use danog\AsyncOrm\Internal\Serializer\BoolInt; +use danog\AsyncOrm\Internal\Serializer\BoolString; use danog\AsyncOrm\Internal\Serializer\Passthrough; use danog\AsyncOrm\KeyType; use danog\AsyncOrm\Serializer; @@ -60,6 +62,7 @@ final class MysqlArray extends SqlArray /** @var Serializer */ $serializer = match ($config->valueType) { ValueType::SCALAR, ValueType::OBJECT => $serializer, + ValueType::BOOL => new BoolInt, default => new Passthrough }; $settings = $config->settings; diff --git a/src/Internal/Driver/PostgresArray.php b/src/Internal/Driver/PostgresArray.php index e56fd6c..3d2f3e9 100644 --- a/src/Internal/Driver/PostgresArray.php +++ b/src/Internal/Driver/PostgresArray.php @@ -22,6 +22,8 @@ use Amp\Postgres\PostgresConnectionPool; use Amp\Sync\LocalKeyedMutex; use danog\AsyncOrm\Driver\SqlArray; use danog\AsyncOrm\FieldConfig; +use danog\AsyncOrm\Internal\Serializer\BoolInt; +use danog\AsyncOrm\Internal\Serializer\BoolString; use danog\AsyncOrm\Internal\Serializer\ByteaSerializer; use danog\AsyncOrm\Internal\Serializer\Passthrough; use danog\AsyncOrm\KeyType; @@ -92,7 +94,7 @@ class PostgresArray extends SqlArray ValueType::STRING => "VARCHAR(255)", ValueType::OBJECT => "MEDIUMBLOB", ValueType::FLOAT => "FLOAT(53)", - ValueType::BOOL => "BIT(1)", + ValueType::BOOL => "BOOLEAN", ValueType::SCALAR, ValueType::OBJECT => "BYTEA", }; /** @var Serializer */ diff --git a/src/Internal/Driver/RedisArray.php b/src/Internal/Driver/RedisArray.php index 89cae23..151b714 100644 --- a/src/Internal/Driver/RedisArray.php +++ b/src/Internal/Driver/RedisArray.php @@ -23,6 +23,8 @@ use Amp\Redis\RedisClient; use Amp\Sync\LocalKeyedMutex; use danog\AsyncOrm\Driver\DriverArray; use danog\AsyncOrm\FieldConfig; +use danog\AsyncOrm\Internal\Serializer\BoolString; +use danog\AsyncOrm\Internal\Serializer\FloatString; use danog\AsyncOrm\Internal\Serializer\IntString; use danog\AsyncOrm\Internal\Serializer\Passthrough; use danog\AsyncOrm\KeyType; @@ -59,6 +61,8 @@ final class RedisArray extends DriverArray /** @var Serializer */ $serializer = match ($config->valueType) { ValueType::INT => new IntString, + ValueType::FLOAT => new FloatString, + ValueType::BOOL => new BoolString, ValueType::SCALAR, ValueType::OBJECT => $serializer, default => new Passthrough }; diff --git a/src/Internal/Serializer/BoolInt.php b/src/Internal/Serializer/BoolInt.php new file mode 100644 index 0000000..1bba364 --- /dev/null +++ b/src/Internal/Serializer/BoolInt.php @@ -0,0 +1,47 @@ +. + * + * @author Daniil Gentili + * @author Alexander Pankratov + * @copyright 2016-2024 Daniil Gentili + * @copyright 2016-2024 Alexander Pankratov + * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 + * @link https://daniil.it/AsyncOrm AsyncOrm documentation + */ + +namespace danog\AsyncOrm\Internal\Serializer; + +use AssertionError; +use danog\AsyncOrm\Serializer; + +/** + * Bool casting serializer. + * + * @internal + * + * @implements Serializer + */ +final class BoolInt implements Serializer +{ + public function serialize(mixed $value): mixed + { + return match ($value) { + true => 1, + false => 0, + }; + } + public function deserialize(mixed $value): mixed + { + return match ($value) { + 1 => true, + 0 => false, + }; + } +} diff --git a/src/Internal/Serializer/BoolString.php b/src/Internal/Serializer/BoolString.php new file mode 100644 index 0000000..b623c67 --- /dev/null +++ b/src/Internal/Serializer/BoolString.php @@ -0,0 +1,46 @@ +. + * + * @author Daniil Gentili + * @author Alexander Pankratov + * @copyright 2016-2024 Daniil Gentili + * @copyright 2016-2024 Alexander Pankratov + * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 + * @link https://daniil.it/AsyncOrm AsyncOrm documentation + */ + +namespace danog\AsyncOrm\Internal\Serializer; + +use danog\AsyncOrm\Serializer; + +/** + * Bool casting serializer. + * + * @internal + * + * @implements Serializer + */ +final class BoolString implements Serializer +{ + public function serialize(mixed $value): mixed + { + return match ($value) { + true => '1', + false => '0', + }; + } + public function deserialize(mixed $value): mixed + { + return match ($value) { + '1' => true, + '0' => false, + }; + } +} diff --git a/src/Internal/Serializer/FloatString.php b/src/Internal/Serializer/FloatString.php new file mode 100644 index 0000000..c2d8299 --- /dev/null +++ b/src/Internal/Serializer/FloatString.php @@ -0,0 +1,40 @@ +. + * + * @author Daniil Gentili + * @author Alexander Pankratov + * @copyright 2016-2024 Daniil Gentili + * @copyright 2016-2024 Alexander Pankratov + * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 + * @link https://daniil.it/AsyncOrm AsyncOrm documentation + */ + +namespace danog\AsyncOrm\Internal\Serializer; + +use danog\AsyncOrm\Serializer; + +/** + * Float casting serializer. + * + * @internal + * + * @implements Serializer + */ +final class FloatString implements Serializer +{ + public function serialize(mixed $value): mixed + { + return (string) $value; + } + public function deserialize(mixed $value): mixed + { + return (float) $value; + } +} diff --git a/tests/OrmTest.php b/tests/OrmTest.php index 0ddb330..ecba101 100644 --- a/tests/OrmTest.php +++ b/tests/OrmTest.php @@ -109,11 +109,14 @@ final class OrmTest extends TestCase } } + private static int $cnt = 0; + #[DataProvider('provideSettingsKeysValues')] public function testBasic(Settings $settings, KeyType $keyType, string|int $key, ValueType $valueType, mixed $value): void { + $cnt = self::$cnt++; $field = new FieldConfig( - 'testBasic', + "testBasic_$cnt", $settings, $keyType, $valueType