Skip to content

Commit

Permalink
Merge pull request #412: Fix DurationJsonType::parse()
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Apr 5, 2024
2 parents 82174fd + 4d06ea8 commit 03c654c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Internal/Marshaller/Type/DurationJsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Carbon\CarbonInterval;
use Google\Protobuf\Duration;
use Temporal\Internal\Marshaller\MarshallerInterface;
use Temporal\Internal\Marshaller\MarshallingRule;
use Temporal\Internal\Support\DateInterval;
use Temporal\Internal\Support\Inheritance;
Expand All @@ -23,6 +24,22 @@
*/
class DurationJsonType extends Type implements DetectableTypeInterface, RuleFactoryInterface
{
/**
* @var DateIntervalFormat
*/
private string $fallbackFormat;

/**
* @param MarshallerInterface $marshaller
* @param DateIntervalFormat $format Fall back format for parsing when the value is not an array.
*/
public function __construct(MarshallerInterface $marshaller, string $format = DateInterval::FORMAT_NANOSECONDS)
{
$this->fallbackFormat = $format;

parent::__construct($marshaller);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -70,6 +87,12 @@ public function serialize($value): ?array
*/
public function parse($value, $current): CarbonInterval
{
return DateInterval::parse($value);
if (is_array($value) && isset($value['seconds']) && isset($value['nanos'])) {
// The highest precision is milliseconds either way.
$value = $value['seconds'] * 1_000_000_000 + $value['nanos'];
return DateInterval::parse($value, DateInterval::FORMAT_NANOSECONDS);
}

return DateInterval::parse($value, $this->fallbackFormat);
}
}
18 changes: 18 additions & 0 deletions tests/Unit/DTO/RetryOptionsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ public function testMarshallingIntervals(): void

$this->assertSame($expected, $this->marshal($dto));
}

public function testUnmarshalLegacyIntervals(): void
{
$expected = [
'initial_interval' => 10_000_000_000,
'backoff_coefficient' => 3.0,
'maximum_interval' => 15_000_000_000,
'maximum_attempts' => 5,
'non_retryable_error_types' => [],
];

$this->unmarshal($expected, $unmarshalled = new RetryOptions());

self::assertSame(10.0, (float)$unmarshalled->initialInterval->totalSeconds);
self::assertSame(15.0, (float)$unmarshalled->maximumInterval->totalSeconds);
self::assertSame(3.0, $unmarshalled->backoffCoefficient);
self::assertSame(5, $unmarshalled->maximumAttempts);
}
}
38 changes: 38 additions & 0 deletions tests/Unit/DTO/Type/DurationJsonType/DurationJsonTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Type\DurationJsonType;

use Temporal\Internal\Marshaller\Type\DurationJsonType;
use Temporal\Internal\Support\DateInterval;
use Temporal\Tests\Unit\DTO\AbstractDTOMarshalling;
use Temporal\Tests\Unit\DTO\Type\DurationJsonType\Stub\DurationJsonDto;

class DurationJsonTestCase extends AbstractDTOMarshalling
{
public function testMarshalAndUnmarshalDuration(): void
{
$dto = new DurationJsonDto();
$dto->duration = DateInterval::parse(1);

$result = $this->marshal($dto);
$unmarshal = $this->unmarshal($result, new DurationJsonDto());

self::assertInstanceOf(\DateInterval::class, $unmarshal->duration);
}

protected function getTypeMatchers(): array
{
return [
DurationJsonType::class,
];
}
}
17 changes: 17 additions & 0 deletions tests/Unit/DTO/Type/DurationJsonType/Stub/DurationJsonDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Type\DurationJsonType\Stub;

class DurationJsonDto
{
public \DateInterval $duration;
}

0 comments on commit 03c654c

Please sign in to comment.