Skip to content

Commit

Permalink
Typed SA collection now has typed datetime values (ImmutableDatetime)…
Browse files Browse the repository at this point in the history
…; fix tests
  • Loading branch information
roxblnfk committed Jan 16, 2025
1 parent 401b621 commit 912c7e7
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ final class DatetimeValue extends SearchAttributeKey
*/
public function valueSet(string|\DateTimeInterface $value): SearchAttributeUpdate

Check failure on line 19 in src/Common/SearchAttributes/SearchAttributeKey/DatetimeValue.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.3, OS ubuntu-latest)

MoreSpecificReturnType

src/Common/SearchAttributes/SearchAttributeKey/DatetimeValue.php:19:65: MoreSpecificReturnType: The declared return type 'Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueSet' for Temporal\Common\SearchAttributes\SearchAttributeKey\DatetimeValue::valueSet is more specific than the inferred return type 'Temporal\Common\SearchAttributes\SearchAttributeUpdate' (see https://psalm.dev/070)
{
$datetime = \is_string($value) ? new \DateTimeImmutable($value) : $value;
return $this->prepareValueSet($datetime->format(\DateTimeInterface::RFC3339));
return $this->prepareValueSet(match (true) {

Check failure on line 21 in src/Common/SearchAttributes/SearchAttributeKey/DatetimeValue.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.3, OS ubuntu-latest)

LessSpecificReturnStatement

src/Common/SearchAttributes/SearchAttributeKey/DatetimeValue.php:21:16: LessSpecificReturnStatement: The type 'Temporal\Common\SearchAttributes\SearchAttributeUpdate' is more general than the declared return type 'Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueSet' for Temporal\Common\SearchAttributes\SearchAttributeKey\DatetimeValue::valueSet (see https://psalm.dev/129)
\is_string($value) => new \DateTimeImmutable($value),
$value instanceof \DateTimeImmutable => $value,
default => \DateTimeImmutable::createFromInterface($value),
});
}

public function getType(): ValueType
Expand Down
14 changes: 0 additions & 14 deletions src/Common/TypedSearchAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,6 @@ public function offsetGet(string $name): mixed
return $key === null ? null : $this->collection[$key];
}

/**
* @return array<non-empty-string, mixed>
*/
public function toArray(): array
{
$result = [];
/** @var SearchAttributeKey $key */
foreach ($this as $key => $value) {
$result[$key->getName()] = $value;
}

return $result;
}

/**
* @param non-empty-string $name
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ private function prepareSearchAttributes(): array
? [
'type' => $attr->type->value,
'operation' => 'set',
'value' => $attr->value,
'value' => match (true) {
$attr->value instanceof \DateTimeInterface => $attr->value->format(\DateTimeInterface::RFC3339),
default => $attr->value,
},
]
: [
'type' => $attr->type->value,
Expand Down
11 changes: 11 additions & 0 deletions src/Internal/Workflow/ScopeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ public function rejectConditionGroup(string $conditionGroupId): void
public function upsertSearchAttributes(array $searchAttributes): void
{
$this->request(new UpsertSearchAttributes($searchAttributes), waitResponse: false);

/** @psalm-suppress UnsupportedPropertyReferenceUsage $sa */
$sa = &$this->input->info->searchAttributes;
foreach ($searchAttributes as $name => $value) {
if ($value === null) {
unset($sa[$name]);
continue;
}

$sa[$name] = $value;
}
}

public function upsertTypedSearchAttributes(SearchAttributeUpdate ...$updates): void
Expand Down
10 changes: 9 additions & 1 deletion tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,15 @@ public function handle()
fn(): bool => $this->exit,
);

return Workflow::getInfo()->typedSearchAttributes->toArray();
$result = [];
/** @var SearchAttributeKey $key */
foreach (Workflow::getInfo()->typedSearchAttributes as $key => $value) {
$result[$key->getName()] = $value instanceof \DateTimeInterface
? $value->format(\DateTimeInterface::RFC3339)
: $value;
}

return $result;
}

#[Workflow\UpdateMethod]
Expand Down
3 changes: 1 addition & 2 deletions tests/Functional/ConcurrentWorkflowContextTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public function testMocks(): void
unset($generators[$i]);
}
}
// \shuffle($generators); // todo smart events merge
\array_map(static fn(\Generator $g) => $g->next(), $generators);

$stop or $addWorkflow();
Expand Down Expand Up @@ -137,7 +136,7 @@ private function iterateOtherWorkflow(bool $withQuery = true): iterable
[0m [{"command":"InvokeQuery","options":{"runId":"$runId","name":"wakeup"},"payloads":"ChkKFwoIZW5jb2RpbmcSC2JpbmFyeS9udWxs"}] {"taskQueue":"default","tickTime":"2021-01-12T15:25:13.3987564Z"}
EVENT;
yield <<<EVENT
[0m [{"payloads":"ChwKFgoIZW5jb2RpbmcSCmpzb24vcGxhaW4SAltd"}] {"receive": true}
[0m [{"payloads":"CjUKFgoIZW5jb2RpbmcSCmpzb24vcGxhaW4SGyIyMDIxLTAxLTEyVDE1OjI1OjE4KzAwOjAwIg=="}] {"receive": true}
EVENT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Temporal\Common\TypedSearchAttributes;
use PHPUnit\Framework\TestCase;

class TypedSearchAttributesTest extends TestCase
class TypedSearchAttributesTestCase extends TestCase
{
public function testCount(): void
{
Expand Down Expand Up @@ -156,7 +156,7 @@ public function testFromJsonArray(): void
'value' => false,
],
'name3' => [
'type' => 'int',
'type' => 'int64',
'value' => 42,
],
'name4' => [
Expand Down Expand Up @@ -218,4 +218,22 @@ public function testFromUntypedCollection(): void
self::assertSame('2021-01-01T00:00:00+00:00', $collection->get(SearchAttributeKey::forDatetime('name7'))->format(DATE_RFC3339));
self::assertSame(['foo', 'bar'], $collection->get(SearchAttributeKey::forKeywordList('name8')));
}

public function testValues()
{
$collection = TypedSearchAttributes::empty()
->withValue(SearchAttributeKey::forFloat('testFloat'), 1.1)
->withValue(SearchAttributeKey::forInteger('testInt'), -2)
->withValue(SearchAttributeKey::forBool('testBool'), false)
->withValue(SearchAttributeKey::forString('testString'), 'foo')
->withValue(SearchAttributeKey::forKeyword('testKeyword'), 'bar')
->withValue(SearchAttributeKey::forKeywordList('testKeywordList'), ['baz'])
->withValue(
SearchAttributeKey::forDatetime('testDatetime'),
new \DateTimeImmutable('2019-01-01T00:00:00Z'),
);

self::assertSame(1.1, $collection->offsetGet('testFloat'));

}
}
1 change: 1 addition & 0 deletions tests/Unit/DTO/WorkflowInfoTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function testMarshalling(): void
'ParentWorkflowNamespace' => null,
'ParentWorkflowExecution' => null,
'SearchAttributes' => null,
'TypedSearchAttributes' => [],
'Memo' => null,
'BinaryChecksum' => '',
];
Expand Down

0 comments on commit 912c7e7

Please sign in to comment.