From c98dbcdddba7de31b4768474d3aa17d6a25303bf Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 11 Feb 2024 18:14:47 +0100 Subject: [PATCH] use sequences instead of sets --- src/Adapter/Filesystem/Decode.php | 15 +++---- src/Adapter/SQL/Decode.php | 3 +- src/Adapter/SQL/Encode.php | 31 ++++++------- src/Adapter/SQL/MainTable.php | 27 ++++++------ src/Adapter/SQL/Update.php | 20 ++++----- src/Definition/Aggregate.php | 50 ++++++++++----------- src/Definition/Aggregate/Parsing.php | 51 ++++++++++----------- src/Raw/Aggregate.php | 66 ++++++++++++++-------------- src/Raw/Diff.php | 66 ++++++++++++++-------------- src/Repository/Denormalize.php | 12 ++--- src/Repository/Diff.php | 12 ++--- src/Repository/Extract.php | 14 +++--- src/Repository/Normalize.php | 12 ++--- 13 files changed, 177 insertions(+), 202 deletions(-) diff --git a/src/Adapter/Filesystem/Decode.php b/src/Adapter/Filesystem/Decode.php index 9d2b30b..03cb22c 100644 --- a/src/Adapter/Filesystem/Decode.php +++ b/src/Adapter/Filesystem/Decode.php @@ -17,6 +17,7 @@ use Innmind\Immutable\{ Maybe, Set, + Sequence, Predicate\Instance, }; @@ -67,8 +68,7 @@ public function __invoke(Aggregate\Id $id = null): callable ->map(static fn($file) => Aggregate\Property::of( $file->name()->toString(), Json::decode($file->content()->toString()), - )) - ->toSet(), + )), ), $directory ->get(Name::of('entities')) @@ -89,8 +89,7 @@ public function __invoke(Aggregate\Id $id = null): callable )) ->toSet(), ), - ) - ->toSet(), + ), ), $directory ->get(Name::of('optionals')) @@ -116,8 +115,7 @@ public function __invoke(Aggregate\Id $id = null): callable ->toSet(), ), ), - ) - ->toSet(), + ), ), $directory ->get(Name::of('collections')) @@ -141,10 +139,9 @@ public function __invoke(Aggregate\Id $id = null): callable ), ), ), - ) - ->toSet(), + ), ), - )->map(static fn(Set $properties, Set $entities, Set $optionals, Set $collections) => Aggregate::of( + )->map(static fn(Sequence $properties, Sequence $entities, Sequence $optionals, Sequence $collections) => Aggregate::of( $id($directory), $properties, $entities, diff --git a/src/Adapter/SQL/Decode.php b/src/Adapter/SQL/Decode.php index 284f686..6f499ab 100644 --- a/src/Adapter/SQL/Decode.php +++ b/src/Adapter/SQL/Decode.php @@ -77,8 +77,7 @@ public function __invoke(Aggregate\Id $id = null): callable ->map(static fn($value) => Aggregate\Property::of( Str::of($value->column()->toString())->drop(7)->toString(), $value->value(), - )) - ->toSet(), + )), $this ->mainTable ->entities() diff --git a/src/Adapter/SQL/Encode.php b/src/Adapter/SQL/Encode.php index 75f614a..75c0b35 100644 --- a/src/Adapter/SQL/Encode.php +++ b/src/Adapter/SQL/Encode.php @@ -47,12 +47,10 @@ public function __invoke(Aggregate $data): Sequence $optionals = $this->optionals($data); $collections = $this->collections($data); - return Sequence::of( - $main, - ...$entities->toList(), - ...$optionals->toList(), - ...$collections->toList(), - ); + return Sequence::of($main) + ->append($entities) + ->append($optionals) + ->append($collections); } /** @@ -73,9 +71,9 @@ public static function of( } /** - * @return Set + * @return Sequence */ - private function entities(Aggregate $data): Set + private function entities(Aggregate $data): Sequence { return $data ->entities() @@ -86,15 +84,14 @@ private function entities(Aggregate $data): Set ->map( static fn($table) => $table->insert($data->id(), $entity->properties()), ) - ->toSequence() - ->toSet(), + ->toSequence(), ); } /** - * @return Set + * @return Sequence */ - private function optionals(Aggregate $data): Set + private function optionals(Aggregate $data): Sequence { return $data ->optionals() @@ -107,8 +104,7 @@ private function optionals(Aggregate $data): Set static fn($properties) => $table->insert($data->id(), $properties), ), ) - ->toSequence() - ->toSet(), + ->toSequence(), ); } @@ -121,9 +117,9 @@ private function main(Aggregate $data): Query } /** - * @return Set + * @return Sequence */ - private function collections(Aggregate $data): Set + private function collections(Aggregate $data): Sequence { return $data ->collections() @@ -137,8 +133,7 @@ private function collections(Aggregate $data): Set $collection->entities(), ), ) - ->toSequence() - ->toSet(), + ->toSequence(), ); } } diff --git a/src/Adapter/SQL/MainTable.php b/src/Adapter/SQL/MainTable.php index 0559d10..7fc9139 100644 --- a/src/Adapter/SQL/MainTable.php +++ b/src/Adapter/SQL/MainTable.php @@ -34,6 +34,7 @@ Map, Maybe, Set, + Sequence, }; /** @@ -161,9 +162,9 @@ public function primaryKey(): Table\Column } /** - * @return Set + * @return Sequence */ - public function columnsDefinition(MapType $mapType): Set + public function columnsDefinition(MapType $mapType): Sequence { return $this ->definition @@ -212,11 +213,11 @@ public function count(Specification $specification = null): Select /** * @internal * - * @param Set $properties + * @param Sequence $properties */ public function insert( Id $id, - Set $properties, + Sequence $properties, ): Query { $table = $this->name->name(); @@ -276,11 +277,11 @@ public function delete(): Delete } /** - * @return Set + * @return Sequence */ - public function entities(): Set + public function entities(): Sequence { - return $this->entities->values()->toSet(); + return $this->entities->values(); } /** @@ -294,11 +295,11 @@ public function entity(string $name): Maybe } /** - * @return Set + * @return Sequence */ - public function optionals(): Set + public function optionals(): Sequence { - return $this->optionals->values()->toSet(); + return $this->optionals->values(); } /** @@ -312,11 +313,11 @@ public function optional(string $name): Maybe } /** - * @return Set + * @return Sequence */ - public function collections(): Set + public function collections(): Sequence { - return $this->collections->values()->toSet(); + return $this->collections->values(); } /** diff --git a/src/Adapter/SQL/Update.php b/src/Adapter/SQL/Update.php index d890e1b..8da0519 100644 --- a/src/Adapter/SQL/Update.php +++ b/src/Adapter/SQL/Update.php @@ -45,8 +45,7 @@ public function __invoke(Diff $data): Sequence ) ->toSequence(), ) - ->toSequence() - ->toSet(), + ->toSequence(), ); $optionals = $data ->optionals() @@ -58,8 +57,7 @@ public function __invoke(Diff $data): Sequence $data->id(), $optional, )) - ->toSequence() - ->toSet(), + ->toSequence(), ); $collections = $data ->collections() @@ -74,16 +72,14 @@ public function __invoke(Diff $data): Sequence static fn($entity) => $entity->reference(), ), )) - ->toSequence() - ->toSet(), + ->toSequence(), ); - return Sequence::of( - $main, - ...$entities->toList(), - ...$optionals->toList(), - ...$collections->toList(), - )->flatMap(static fn($queries) => $queries); + return Sequence::of($main) + ->append($entities) + ->append($optionals) + ->append($collections) + ->flatMap(static fn($queries) => $queries); } /** diff --git a/src/Definition/Aggregate.php b/src/Definition/Aggregate.php index 341ee13..e8c30c5 100644 --- a/src/Definition/Aggregate.php +++ b/src/Definition/Aggregate.php @@ -7,7 +7,7 @@ use Innmind\Reflection\ReflectionClass; use Innmind\Immutable\{ Str, - Set, + Sequence, Monoid\Concat, }; @@ -21,30 +21,30 @@ final class Aggregate private string $class; /** @var Aggregate\Identity */ private Aggregate\Identity $id; - /** @var Set> */ - private Set $properties; - /** @var Set */ - private Set $entities; - /** @var Set */ - private Set $optionals; - /** @var Set */ - private Set $collections; + /** @var Sequence> */ + private Sequence $properties; + /** @var Sequence */ + private Sequence $entities; + /** @var Sequence */ + private Sequence $optionals; + /** @var Sequence */ + private Sequence $collections; /** * @param class-string $class * @param Aggregate\Identity $id - * @param Set> $properties - * @param Set $entities - * @param Set $optionals - * @param Set $collections + * @param Sequence> $properties + * @param Sequence $entities + * @param Sequence $optionals + * @param Sequence $collections */ private function __construct( string $class, Aggregate\Identity $id, - Set $properties, - Set $entities, - Set $optionals, - Set $collections, + Sequence $properties, + Sequence $entities, + Sequence $optionals, + Sequence $collections, ) { $this->class = $class; $this->id = $id; @@ -116,33 +116,33 @@ public function id(): Aggregate\Identity } /** - * @return Set> + * @return Sequence> */ - public function properties(): Set + public function properties(): Sequence { return $this->properties; } /** - * @return Set + * @return Sequence */ - public function entities(): Set + public function entities(): Sequence { return $this->entities; } /** - * @return Set + * @return Sequence */ - public function optionals(): Set + public function optionals(): Sequence { return $this->optionals; } /** - * @return Set + * @return Sequence */ - public function collections(): Set + public function collections(): Sequence { return $this->collections; } diff --git a/src/Definition/Aggregate/Parsing.php b/src/Definition/Aggregate/Parsing.php index 46e0571..a56a9c1 100644 --- a/src/Definition/Aggregate/Parsing.php +++ b/src/Definition/Aggregate/Parsing.php @@ -16,6 +16,7 @@ use Innmind\Immutable\{ Maybe, Set, + Sequence, Predicate\Instance, }; @@ -29,30 +30,30 @@ final class Parsing private string $class; /** @var Maybe> */ private Maybe $id; - /** @var Set> */ - private Set $properties; - /** @var Set */ - private Set $entities; - /** @var Set */ - private Set $optionals; - /** @var Set */ - private Set $collections; + /** @var Sequence> */ + private Sequence $properties; + /** @var Sequence */ + private Sequence $entities; + /** @var Sequence */ + private Sequence $optionals; + /** @var Sequence */ + private Sequence $collections; /** * @param class-string $class * @param Maybe> $id - * @param Set> $properties - * @param Set $entities - * @param Set $optionals - * @param Set $collections + * @param Sequence> $properties + * @param Sequence $entities + * @param Sequence $optionals + * @param Sequence $collections */ private function __construct( string $class, Maybe $id, - Set $properties, - Set $entities, - Set $optionals, - Set $collections, + Sequence $properties, + Sequence $entities, + Sequence $optionals, + Sequence $collections, ) { $this->class = $class; $this->id = $id; @@ -75,7 +76,7 @@ public static function of(string $class): self /** @var Maybe> */ $id = Maybe::nothing(); - return new self($class, $id, Set::of(), Set::of(), Set::of(), Set::of()); + return new self($class, $id, Sequence::of(), Sequence::of(), Sequence::of(), Sequence::of()); } /** @@ -143,33 +144,33 @@ public function id(): Maybe } /** - * @return Set> + * @return Sequence> */ - public function properties(): Set + public function properties(): Sequence { return $this->properties; } /** - * @return Set + * @return Sequence */ - public function entities(): Set + public function entities(): Sequence { return $this->entities; } /** - * @return Set + * @return Sequence */ - public function optionals(): Set + public function optionals(): Sequence { return $this->optionals; } /** - * @return Set + * @return Sequence */ - public function collections(): Set + public function collections(): Sequence { return $this->collections; } diff --git a/src/Raw/Aggregate.php b/src/Raw/Aggregate.php index b5207c5..6134e88 100644 --- a/src/Raw/Aggregate.php +++ b/src/Raw/Aggregate.php @@ -3,7 +3,7 @@ namespace Formal\ORM\Raw; -use Innmind\Immutable\Set; +use Innmind\Immutable\Sequence; /** * @psalm-immutable @@ -11,27 +11,27 @@ final class Aggregate { private Aggregate\Id $id; - /** @var Set */ - private Set $properties; - /** @var Set */ - private Set $entities; - /** @var Set */ - private Set $optionals; - /** @var Set */ - private Set $collections; + /** @var Sequence */ + private Sequence $properties; + /** @var Sequence */ + private Sequence $entities; + /** @var Sequence */ + private Sequence $optionals; + /** @var Sequence */ + private Sequence $collections; /** - * @param Set $properties - * @param Set $entities - * @param Set $optionals - * @param Set $collections + * @param Sequence $properties + * @param Sequence $entities + * @param Sequence $optionals + * @param Sequence $collections */ private function __construct( Aggregate\Id $id, - Set $properties, - Set $entities, - Set $optionals, - Set $collections, + Sequence $properties, + Sequence $entities, + Sequence $optionals, + Sequence $collections, ) { $this->id = $id; $this->properties = $properties; @@ -43,17 +43,17 @@ private function __construct( /** * @psalm-pure * - * @param Set $properties - * @param Set $entities - * @param Set $optionals - * @param Set $collections + * @param Sequence $properties + * @param Sequence $entities + * @param Sequence $optionals + * @param Sequence $collections */ public static function of( Aggregate\Id $id, - Set $properties, - Set $entities, - Set $optionals, - Set $collections, + Sequence $properties, + Sequence $entities, + Sequence $optionals, + Sequence $collections, ): self { return new self($id, $properties, $entities, $optionals, $collections); } @@ -64,33 +64,33 @@ public function id(): Aggregate\Id } /** - * @return Set + * @return Sequence */ - public function properties(): Set + public function properties(): Sequence { return $this->properties; } /** - * @return Set + * @return Sequence */ - public function entities(): Set + public function entities(): Sequence { return $this->entities; } /** - * @return Set + * @return Sequence */ - public function optionals(): Set + public function optionals(): Sequence { return $this->optionals; } /** - * @return Set + * @return Sequence */ - public function collections(): Set + public function collections(): Sequence { return $this->collections; } diff --git a/src/Raw/Diff.php b/src/Raw/Diff.php index 1821ad0..21a48b5 100644 --- a/src/Raw/Diff.php +++ b/src/Raw/Diff.php @@ -3,7 +3,7 @@ namespace Formal\ORM\Raw; -use Innmind\Immutable\Set; +use Innmind\Immutable\Sequence; /** * @psalm-immutable @@ -11,27 +11,27 @@ final class Diff { private Aggregate\Id $id; - /** @var Set */ - private Set $properties; - /** @var Set */ - private Set $entities; - /** @var Set */ - private Set $optionals; - /** @var Set */ - private Set $collections; + /** @var Sequence */ + private Sequence $properties; + /** @var Sequence */ + private Sequence $entities; + /** @var Sequence */ + private Sequence $optionals; + /** @var Sequence */ + private Sequence $collections; /** - * @param Set $properties - * @param Set $entities - * @param Set $optionals - * @param Set $collections + * @param Sequence $properties + * @param Sequence $entities + * @param Sequence $optionals + * @param Sequence $collections */ private function __construct( Aggregate\Id $id, - Set $properties, - Set $entities, - Set $optionals, - Set $collections, + Sequence $properties, + Sequence $entities, + Sequence $optionals, + Sequence $collections, ) { $this->id = $id; $this->properties = $properties; @@ -44,17 +44,17 @@ private function __construct( * @internal * @psalm-pure * - * @param Set $properties - * @param Set $entities - * @param Set $optionals - * @param Set $collections + * @param Sequence $properties + * @param Sequence $entities + * @param Sequence $optionals + * @param Sequence $collections */ public static function of( Aggregate\Id $id, - Set $properties, - Set $entities, - Set $optionals, - Set $collections, + Sequence $properties, + Sequence $entities, + Sequence $optionals, + Sequence $collections, ): self { return new self($id, $properties, $entities, $optionals, $collections); } @@ -65,33 +65,33 @@ public function id(): Aggregate\Id } /** - * @return Set + * @return Sequence */ - public function properties(): Set + public function properties(): Sequence { return $this->properties; } /** - * @return Set + * @return Sequence */ - public function entities(): Set + public function entities(): Sequence { return $this->entities; } /** - * @return Set + * @return Sequence */ - public function optionals(): Set + public function optionals(): Sequence { return $this->optionals; } /** - * @return Set + * @return Sequence */ - public function collections(): Set + public function collections(): Sequence { return $this->collections; } diff --git a/src/Repository/Denormalize.php b/src/Repository/Denormalize.php index d2de6f2..a6a2cb5 100644 --- a/src/Repository/Denormalize.php +++ b/src/Repository/Denormalize.php @@ -130,8 +130,7 @@ private function properties(Id $id, Aggregate $data): Map ->get($property->name()) ->map(static fn($definition): mixed => $definition->type()->denormalize($property->value())) ->map(static fn($value) => [$property->name(), $value]) - ->toSequence() - ->toSet(), + ->toSequence(), ) ->toList(), ...$data @@ -142,8 +141,7 @@ private function properties(Id $id, Aggregate $data): Map ->get($entity->name()) ->map(static fn($denormalize): object => $denormalize($entity)) ->map(static fn($value) => [$entity->name(), $value]) - ->toSequence() - ->toSet(), + ->toSequence(), ) ->toList(), ...$data @@ -154,8 +152,7 @@ private function properties(Id $id, Aggregate $data): Map ->get($optional->name()) ->map(static fn($denormalize): Maybe => $denormalize($optional)) ->map(static fn($value) => [$optional->name(), $value]) - ->toSequence() - ->toSet(), + ->toSequence(), ) ->toList(), ...$data @@ -166,8 +163,7 @@ private function properties(Id $id, Aggregate $data): Map ->get($collection->name()) ->map(static fn($denormalize): Set => $denormalize($id, $collection)) ->map(static fn($value) => [$collection->name(), $value]) - ->toSequence() - ->toSet(), + ->toSequence(), ) ->toList(), ); diff --git a/src/Repository/Diff.php b/src/Repository/Diff.php index d37234e..c7807e2 100644 --- a/src/Repository/Diff.php +++ b/src/Repository/Diff.php @@ -120,8 +120,7 @@ public function __invoke(Denormalized $then, Denormalized $now): Raw\Diff $property->name(), $property->type()->normalize($value->now()), )) - ->values() - ->toSet(); + ->values(); /** @psalm-suppress MixedArgument */ $entities = $diff ->flatMap( @@ -137,8 +136,7 @@ public function __invoke(Denormalized $then, Denormalized $now): Raw\Diff static fn() => Map::of(), ), ) - ->values() - ->toSet(); + ->values(); /** @psalm-suppress MixedArgument */ $optionals = $diff ->flatMap( @@ -154,8 +152,7 @@ public function __invoke(Denormalized $then, Denormalized $now): Raw\Diff static fn() => Map::of(), ), ) - ->values() - ->toSet(); + ->values(); /** @psalm-suppress MixedArgument */ $collections = $diff ->flatMap( @@ -173,8 +170,7 @@ public function __invoke(Denormalized $then, Denormalized $now): Raw\Diff static fn() => Map::of(), ), ) - ->values() - ->toSet(); + ->values(); return Raw\Diff::of( $normalizedId, diff --git a/src/Repository/Extract.php b/src/Repository/Extract.php index f12c769..31f45ae 100644 --- a/src/Repository/Extract.php +++ b/src/Repository/Extract.php @@ -9,10 +9,7 @@ Id, }; use Innmind\Reflection; -use Innmind\Immutable\{ - Set, - Map, -}; +use Innmind\Immutable\Set; /** * @internal @@ -38,21 +35,22 @@ private function __construct(Definition $definition) $this->allProperties = $definition ->properties() ->map(static fn($property) => $property->name()) - ->merge( + ->append( $definition ->entities() ->map(static fn($entity) => $entity->name()), ) - ->merge( + ->append( $definition ->optionals() ->map(static fn($optional) => $optional->name()), ) - ->merge( + ->append( $definition ->collections() ->map(static fn($collection) => $collection->name()), - ); + ) + ->toSet(); /** * @psalm-suppress InvalidArgument * @var \Closure(T): Id diff --git a/src/Repository/Normalize.php b/src/Repository/Normalize.php index 628e55a..2c60703 100644 --- a/src/Repository/Normalize.php +++ b/src/Repository/Normalize.php @@ -88,8 +88,7 @@ public function __invoke(Denormalized $denormalized): Aggregate $property->name(), $property->type()->normalize($value), )) - ->toSequence() - ->toSet(), + ->toSequence(), ), $this ->definition @@ -103,8 +102,7 @@ public function __invoke(Denormalized $denormalized): Aggregate ->get($entity->name()) ->map($normalize), ) - ->toSequence() - ->toSet(), + ->toSequence(), ), $this ->definition @@ -118,8 +116,7 @@ public function __invoke(Denormalized $denormalized): Aggregate ->get($optional->name()) ->map($normalize), ) - ->toSequence() - ->toSet(), + ->toSequence(), ), $this ->definition @@ -133,8 +130,7 @@ public function __invoke(Denormalized $denormalized): Aggregate ->get($collection->name()) ->map(static fn($object) => $normalize($denormalized->id(), $object)), ) - ->toSequence() - ->toSet(), + ->toSequence(), ), ); }