instancier type le plus vite possible

This commit is contained in:
Jephté Clain 2025-03-06 15:32:49 +04:00
parent e1b4ef4cc3
commit fc523bfb6c
4 changed files with 22 additions and 17 deletions

View File

@ -1,9 +1,6 @@
# nulib\schema # nulib\schema
* tenir compte de la valeur par défaut, qui *doit* être du bon type * tenir compte de la valeur par défaut, qui *doit* être du bon type
* type vaut soit une instance de IType, soit une tableau de types
* dans la définition, `[type]` est remplacé par l'instance de IType lors de sa
résolution? --> NON, sauf si c'est un type union
* newInput dans Schema * newInput dans Schema
* ScalarSchema::from_property() * ScalarSchema::from_property()

View File

@ -175,7 +175,7 @@ class ScalarSchema extends Schema {
$params = $name; $params = $name;
$name = $key; $name = $key;
} }
$definition["type"] = [types::get($nullable, $name, $params, $definition)]; $definition["type"] = types::get($nullable, $name, $params, $definition);
} }
} }

View File

@ -81,8 +81,8 @@ class ScalarWrapper extends Wrapper {
if (!$input->isPresent($destKey)) return $result->setMissing($schema); if (!$input->isPresent($destKey)) return $result->setMissing($schema);
$schemaTypes = $schema->type; $schemaTypes = $schema->type;
if (count($schemaTypes) == 1 && $schemaTypes[0] instanceof IType) { if ($schemaTypes instanceof IType) {
$type = $schemaTypes[0]; $type = $schemaTypes;
} else { } else {
# type union # type union
$haveType = false; $haveType = false;

View File

@ -3,6 +3,7 @@ namespace nur\sery\wip\schema\_scalar;
use nulib\tests\TestCase; use nulib\tests\TestCase;
use nur\sery\wip\schema\SchemaException; use nur\sery\wip\schema\SchemaException;
use nur\sery\wip\schema\types\IType;
class ScalarSchemaTest extends TestCase { class ScalarSchemaTest extends TestCase {
const NULL_SCHEMA = [ const NULL_SCHEMA = [
@ -31,6 +32,13 @@ class ScalarSchemaTest extends TestCase {
return array_merge(self::NULL_SCHEMA, $schema); return array_merge(self::NULL_SCHEMA, $schema);
} }
static function sanitize(array $schema): array {
if ($schema["type"] instanceof IType) {
$schema["type"] = [$schema["type"]->getClass()];
}
return $schema;
}
function testNormalize() { function testNormalize() {
self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize(null)); self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize(null));
self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize([])); self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize([]));
@ -43,22 +51,22 @@ class ScalarSchemaTest extends TestCase {
}); });
$string = self::schema(["type" => ["string"], "nullable" => false]); $string = self::schema(["type" => ["string"], "nullable" => false]);
self::assertSame($string, ScalarSchema::normalize("string")); self::assertSame($string, self::sanitize(ScalarSchema::normalize("string")));
self::assertSame($string, ScalarSchema::normalize(["string"])); self::assertSame($string, self::sanitize(ScalarSchema::normalize(["string"])));
$nstring = self::schema(["type" => ["string"]]); $nstring = self::schema(["type" => ["string"]]);
self::assertSame($nstring, ScalarSchema::normalize(["?string"])); self::assertSame($nstring, self::sanitize(ScalarSchema::normalize(["?string"])));
self::assertSame($nstring, ScalarSchema::normalize(["?string|null"])); self::assertSame($nstring, self::sanitize(ScalarSchema::normalize(["?string|null"])));
self::assertSame($nstring, ScalarSchema::normalize(["string|null"])); self::assertSame($nstring, self::sanitize(ScalarSchema::normalize(["string|null"])));
self::assertSame($nstring, ScalarSchema::normalize([["?string", "null"]])); self::assertSame($nstring, self::sanitize(ScalarSchema::normalize([["?string", "null"]])));
self::assertSame($nstring, ScalarSchema::normalize([["string", "null"]])); self::assertSame($nstring, self::sanitize(ScalarSchema::normalize([["string", "null"]])));
self::assertSame($nstring, ScalarSchema::normalize([["string", null]])); self::assertSame($nstring, self::sanitize(ScalarSchema::normalize([["string", null]])));
$key = self::schema(["type" => ["string", "int"], "nullable" => false]); $key = self::schema(["type" => ["string", "int"], "nullable" => false]);
self::assertSame($key, ScalarSchema::normalize("string|int")); self::assertSame($key, self::sanitize(ScalarSchema::normalize("string|int")));
$nkey = self::schema(["type" => ["string", "int"], "nullable" => true]); $nkey = self::schema(["type" => ["string", "int"], "nullable" => true]);
self::assertSame($nkey, ScalarSchema::normalize("?string|int")); self::assertSame($nkey, self::sanitize(ScalarSchema::normalize("?string|int")));
self::assertSame($nkey, ScalarSchema::normalize("string|?int")); self::assertSame($nkey, self::sanitize(ScalarSchema::normalize("string|?int")));
} }
} }