Skip to content

Commit 616d1d1

Browse files
authored
Merge pull request #560: configure Search Attributes on DEV server
2 parents 3e12fef + b20e249 commit 616d1d1

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

psalm-baseline.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,12 @@
874874
<code><![CDATA[getReader]]></code>
875875
</UndefinedInterfaceMethod>
876876
</file>
877+
<file src="src/Internal/Support/DateInterval.php">
878+
<DeprecatedConstant>
879+
<code><![CDATA[self::FORMAT_MONTHS]]></code>
880+
<code><![CDATA[self::FORMAT_YEARS]]></code>
881+
</DeprecatedConstant>
882+
</file>
877883
<file src="src/Internal/Support/DateTime.php">
878884
<InvalidReturnStatement>
879885
<code><![CDATA[$time]]></code>

src/Internal/Support/DateInterval.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
*/
2121
final class DateInterval
2222
{
23+
/** @deprecated Use days instead */
2324
public const FORMAT_YEARS = 'years';
25+
26+
/** @deprecated Use days instead */
2427
public const FORMAT_MONTHS = 'months';
28+
2529
public const FORMAT_WEEKS = 'weeks';
2630
public const FORMAT_DAYS = 'days';
2731
public const FORMAT_HOURS = 'hours';
@@ -73,11 +77,17 @@ public static function parse($interval, string $format = self::FORMAT_MILLISECON
7377
default => $fraction > 0 ? match ($format) {
7478
self::FORMAT_SECONDS => $fraction * 1_000_000,
7579
self::FORMAT_MINUTES => $fraction * 60_000_000,
80+
self::FORMAT_HOURS => $fraction * 3_600_000_000,
81+
self::FORMAT_DAYS => $fraction * 86_400_000_000,
82+
self::FORMAT_WEEKS => $fraction * 604_800_000_000,
7683
default => 0,
7784
} : 0,
7885
};
86+
$micros = (int) \round($micros);
87+
$seconds = (int) \floor($micros / 1_000_000);
88+
$micros = $micros - ($seconds * 1_000_000);
7989

80-
$seconds = \floor($micros / 1_000_000) + \floor(match ($format) {
90+
$seconds += \floor(match ($format) {
8191
self::FORMAT_SECONDS => $int,
8292
self::FORMAT_MINUTES => $int * 60,
8393
self::FORMAT_HOURS => $int * 3600,
@@ -97,7 +107,7 @@ public static function parse($interval, string $format = self::FORMAT_MILLISECON
97107
hours: $hours % 24,
98108
minutes: $minutes % 60,
99109
seconds: $seconds % 60,
100-
microseconds: $micros % 1000_000,
110+
microseconds: $micros,
101111
);
102112

103113
case $interval instanceof Duration:

testing/src/Environment.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Filesystem\Filesystem;
1010
use Symfony\Component\HttpClient\HttpClient;
1111
use Symfony\Component\Process\Process;
12+
use Temporal\Common\SearchAttributes\ValueType;
1213

1314
final class Environment
1415
{
@@ -52,11 +53,41 @@ public function start(?string $rrCommand = null, int $commandTimeout = 10, array
5253

5354
/**
5455
* @param list<non-empty-string> $parameters
56+
* @param array<non-empty-string, ValueType|non-empty-string> $searchAttributes Key is the name of the search
57+
* attribute, value is the type of the search attribute. Expected values from {@see ValueType}.
5558
*/
56-
public function startTemporalServer(int $commandTimeout = 10, array $parameters = []): void
57-
{
59+
public function startTemporalServer(
60+
int $commandTimeout = 10,
61+
array $parameters = [],
62+
array $searchAttributes = [],
63+
): void {
5864
$temporalPort = \parse_url(\getenv('TEMPORAL_ADDRESS') ?: '127.0.0.1:7233', PHP_URL_PORT);
5965

66+
// Add search attributes
67+
foreach ($searchAttributes as $name => $type) {
68+
$type = \is_string($type) ? ValueType::tryFrom($type) : $type;
69+
if (!$type instanceof ValueType) {
70+
\trigger_error('Invalid search attribute type: ' . \get_debug_type($type), E_USER_WARNING);
71+
continue;
72+
}
73+
74+
if (\preg_match('/^[a-zA-Z0-9_-]+$/', $name) !== 1) {
75+
\trigger_error('Invalid search attribute name: ' . $name, E_USER_WARNING);
76+
continue;
77+
}
78+
79+
$parameters[] = '--search-attribute';
80+
$parameters[] = $name . '=' . match ($type) {
81+
ValueType::Bool => 'bool',
82+
ValueType::Float => 'double',
83+
ValueType::Int => 'int',
84+
ValueType::Keyword => 'keyword',
85+
ValueType::KeywordList => 'keywordList',
86+
ValueType::String => 'text',
87+
ValueType::Datetime => 'datetime',
88+
};
89+
}
90+
6091
$this->output->write('Starting Temporal test server... ');
6192
$this->temporalServerProcess = new Process(
6293
[

tests/Acceptance/App/Runtime/TemporalStarter.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Temporal\Tests\Acceptance\App\Runtime;
66

7+
use Temporal\Common\SearchAttributes\ValueType;
78
use Temporal\Testing\Environment;
89

910
final class TemporalStarter
@@ -23,16 +24,16 @@ public function start(): void
2324
return;
2425
}
2526

26-
$this->environment->startTemporalServer(parameters: [
27-
'--search-attribute', 'foo=text',
28-
'--search-attribute', 'bar=int',
29-
'--search-attribute', 'testBool=bool',
30-
'--search-attribute', 'testInt=int',
31-
'--search-attribute', 'testFloat=double',
32-
'--search-attribute', 'testString=text',
33-
'--search-attribute', 'testKeyword=keyword',
34-
'--search-attribute', 'testKeywordList=keywordList',
35-
'--search-attribute', 'testDatetime=datetime',
27+
$this->environment->startTemporalServer(searchAttributes: [
28+
'foo' => ValueType::String->value,
29+
'bar' => ValueType::Int->value,
30+
'testBool' => ValueType::Bool,
31+
'testInt' => ValueType::Int,
32+
'testFloat' => ValueType::Float,
33+
'testString' => ValueType::String,
34+
'testKeyword' => ValueType::Keyword,
35+
'testKeywordList' => ValueType::KeywordList,
36+
'testDatetime' => ValueType::Datetime,
3637
]);
3738
$this->started = true;
3839
}

tests/Unit/Internal/Support/DateIntervalTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ public static function provideValuesToParse(): iterable
5757
yield [1, DateInterval::FORMAT_HOURS, 3_600_000_000, '0/1/0/0'];
5858
yield [1, DateInterval::FORMAT_DAYS, 86_400_000_000, '1/0/0/0'];
5959
yield [1, DateInterval::FORMAT_WEEKS, 604_800_000_000, '7/0/0/0'];
60+
yield [(0.1 + 0.7) * 10.0, DateInterval::FORMAT_SECONDS, 8_000_000, '0/0/0/8'];
61+
yield [(0.1 + 0.7) * 10.0, DateInterval::FORMAT_DAYS, 691200000000, '8/0/0/0'];
62+
yield [(0.1 + 0.7) * 10.0, DateInterval::FORMAT_WEEKS, 4838400000000, '56/0/0/0'];
6063
}
6164
}

0 commit comments

Comments
 (0)