Skip to content

Commit

Permalink
fix point in time type format being non standard
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Aug 1, 2024
1 parent 7ed19f4 commit 842e5a8
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 21 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

### Added

- `Formal\ORM\Definition\Type\PointInTimeType::new()`

### Deprecated

- `Formal\ORM\Definition\Type\PointInTimeType::of()` as it uses a non standard string format. Use `::new()` instead, but don't forget to migrate your data.

### Fixed

- Psalm was complaining of a missing argument when using `PointInTimeType::of()`
Expand Down
10 changes: 7 additions & 3 deletions benchmark/fill_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
Adapter,
};
use Innmind\OperatingSystem\Factory;
use Innmind\TimeContinuum\PointInTime;
use Innmind\Url\Url;
use Innmind\BlackBox\{
Set,
Random,
};
use Innmind\Immutable\Either;
use Fixtures\Formal\ORM\User;
use Fixtures\Innmind\TimeContinuum\Earth\PointInTime;
use Fixtures\Innmind\TimeContinuum\Earth\PointInTime as FPointInTime;

$os = Factory::build();
$connection = $os->remote()->sql(Url::of("mysql://root:root@127.0.0.1:3306/example"));
$aggregates = Aggregates::of(Types::of(
Type\PointInTimeType::of($os->clock()),
Type\Support::class(
PointInTime::class,
Type\PointInTimeType::new($os->clock()),
),
));

$_ = Adapter\SQL\ShowCreateTable::of($aggregates)(User::class)->foreach($connection);
Expand All @@ -33,7 +37,7 @@

$users = Set\Composite::immutable(
User::new(...),
PointInTime::any(),
FPointInTime::any(),
Set\Strings::madeOf(Set\Chars::alphanumerical())->between(0, 250),
);
$users = Set\Randomize::of($users)->take(100_000)->values(Random::default);
Expand Down
6 changes: 5 additions & 1 deletion benchmark/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
use Innmind\OperatingSystem\Factory;
use Innmind\Url\Url;
use Fixtures\Formal\ORM\User;
use Innmind\TimeContinuum\PointInTime;

$os = Factory::build();
$connection = $os->remote()->sql(Url::of("mysql://root:root@127.0.0.1:3306/example"));
$aggregates = Aggregates::of(Types::of(
Type\PointInTimeType::of($os->clock()),
Type\Support::class(
PointInTime::class,
Type\PointInTimeType::new($os->clock()),
),
));

$manager = Manager::sql($connection, $aggregates);
Expand Down
11 changes: 8 additions & 3 deletions documentation/mapping/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ By default Formal also supports:
Manager,
Definition\Aggregates,
Definition\Types,
Definition\Type\PointInTime,
}
Definition\Type\Support,
Definition\Type\PointInTimeType,
};
use Innmind\TimeContinuum\PointInTime;

$orm = Manager::of(
/* any adapter (1) */,
Aggregates::of(
Types::of(
PointInTimeType::of($os->clock()),
Support::class(
PointInTime::class,
PointInTimeType::new($os->clock()),
),
),
),
);
Expand Down
10 changes: 8 additions & 2 deletions proofs/adapter/elasticsearch/mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@
Definition\Type,
Definition\Types,
};
use Innmind\TimeContinuum\Earth\Clock;
use Innmind\TimeContinuum\{
Earth\Clock,
PointInTime,
};
use Fixtures\Formal\ORM\User;

return static function() {
yield test(
'Define Elasticsearch index mapping for the User fixture',
static function($assert) {
$aggregates = Aggregates::of(Types::of(
Type\PointInTimeType::of(new Clock),
Type\Support::class(
PointInTime::class,
Type\PointInTimeType::new(new Clock),
),
));

$mapping = Mapping::new()($aggregates->get(User::class));
Expand Down
15 changes: 12 additions & 3 deletions proofs/adapter/sql/showCreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
Definition\Types,
};
use Formal\AccessLayer\Driver;
use Innmind\TimeContinuum\Earth\Clock;
use Innmind\TimeContinuum\{
Earth\Clock,
PointInTime,
};
use Fixtures\Formal\ORM\User;

return static function() {
Expand All @@ -18,7 +21,10 @@
static function($assert) {
$show = ShowCreateTable::of(
Aggregates::of(Types::of(
Type\PointInTimeType::of(new Clock),
Type\Support::class(
PointInTime::class,
Type\PointInTimeType::new(new Clock),
),
)),
);

Expand Down Expand Up @@ -53,7 +59,10 @@ static function($assert) {
static function($assert) {
$show = ShowCreateTable::of(
Aggregates::of(Types::of(
Type\PointInTimeType::of(new Clock),
Type\Support::class(
PointInTime::class,
Type\PointInTimeType::new(new Clock),
),
))->mapName(static fn($string) => match ($string) {
User::class => 'some_user',
}),
Expand Down
20 changes: 16 additions & 4 deletions proofs/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
};
use Innmind\OperatingSystem\Factory;
use Innmind\Filesystem\Adapter\InMemory;
use Innmind\TimeContinuum\Earth\Clock;
use Innmind\TimeContinuum\{
Earth\Clock,
PointInTime,
};
use Innmind\Url\Url;
use Innmind\Immutable\Either;
use Innmind\BlackBox\Set;
Expand Down Expand Up @@ -82,7 +85,10 @@ static function($assert) {
Set\Call::of(static fn() => Manager::filesystem(
InMemory::emulateFilesystem(),
Aggregates::of(Types::of(
Type\PointInTimeType::of(new Clock),
Type\Support::class(
PointInTime::class,
Type\PointInTimeType::new(new Clock),
),
SortableType::of(...),
)),
)),
Expand All @@ -94,7 +100,10 @@ static function($assert) {
Set\Call::of(static fn() => Manager::filesystem(
InMemory::emulateFilesystem(),
Aggregates::of(Types::of(
Type\PointInTimeType::of(new Clock),
Type\Support::class(
PointInTime::class,
Type\PointInTimeType::new(new Clock),
),
SortableType::of(...),
)),
)),
Expand All @@ -105,7 +114,10 @@ static function($assert) {

$os = Factory::build();
$aggregates = Aggregates::of(Types::of(
Type\PointInTimeType::of($os->clock()),
Type\Support::class(
PointInTime::class,
Type\PointInTimeType::new($os->clock()),
),
SortableType::of(...),
));

Expand Down
19 changes: 15 additions & 4 deletions src/Definition/Type/PointInTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,38 @@
final class PointInTimeType implements Type
{
private Clock $clock;
private Format $format;

private function __construct(Clock $clock)
private function __construct(Clock $clock, Format $format)
{
$this->clock = $clock;
$this->format = $format;
}

/**
* @psalm-pure
*/
public static function new(Clock $clock): self
{
return new self($clock, Format::new());

Check failure on line 42 in src/Definition/Type/PointInTimeType.php

View workflow job for this annotation

GitHub Actions / Psalm (8.2, lowest)

ImpureMethodCall

src/Definition/Type/PointInTimeType.php:42:41: ImpureMethodCall: Cannot call an impure method from a pure context (see https://psalm.dev/203)

Check failure on line 42 in src/Definition/Type/PointInTimeType.php

View workflow job for this annotation

GitHub Actions / Psalm (8.2, highest)

ImpureMethodCall

src/Definition/Type/PointInTimeType.php:42:41: ImpureMethodCall: Cannot call an impure method from a pure context (see https://psalm.dev/203)

Check failure on line 42 in src/Definition/Type/PointInTimeType.php

View workflow job for this annotation

GitHub Actions / Psalm (8.3, lowest)

ImpureMethodCall

src/Definition/Type/PointInTimeType.php:42:41: ImpureMethodCall: Cannot call an impure method from a pure context (see https://psalm.dev/203)

Check failure on line 42 in src/Definition/Type/PointInTimeType.php

View workflow job for this annotation

GitHub Actions / Psalm (8.3, highest)

ImpureMethodCall

src/Definition/Type/PointInTimeType.php:42:41: ImpureMethodCall: Cannot call an impure method from a pure context (see https://psalm.dev/203)
}

/**
* @psalm-pure
* @deprecated Use ::new() instead
*
* @return callable(Types, Concrete, ?Contains): Maybe<self>
*/
public static function of(Clock $clock): callable
{
return static fn(Types $types, Concrete $type) => Maybe::just($type)
->filter(static fn($type) => $type->accepts(ClassName::of(PointInTime::class)))
->map(static fn() => new self($clock));
->map(static fn() => new self($clock, new Format));
}

public function normalize(mixed $value): null|string|int|bool
{
return $value->format(new Format);
return $value->format($this->format);
}

public function denormalize(null|string|int|bool $value): mixed
Expand All @@ -57,7 +68,7 @@ public function denormalize(null|string|int|bool $value): mixed

return $this
->clock
->at($value, new Format)
->at($value, $this->format)
->match(
static fn($point) => $point,
static fn() => throw new \LogicException("'$value' is not a date"),
Expand Down
17 changes: 16 additions & 1 deletion src/Definition/Type/PointInTimeType/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@
*/
final class Format implements FormatInterface
{
/**
* This is a non standard format (stupid mistake) but is kept as is until
* the next major release. The ::new named constructor should be used to
* have the correct format (that will be the future default).
*/
private string $format = 'Y:m:d\TH:i:s.uP';

public static function new(): self
{
$self = new self;
$self->format = 'Y-m-d\TH:i:s.uP';

return $self;
}

public function toString(): string
{
return 'Y:m:d\TH:i:s.uP';
return $this->format;
}
}

0 comments on commit 842e5a8

Please sign in to comment.