From dee3fb51fbfdb923bed4954b55543aeee63c3548 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 17 Jan 2025 00:24:07 +0400 Subject: [PATCH] Typed SA collection now has typed datetime values (ImmutableDatetime); fix tests --- src/Common/TypedSearchAttributes.php | 14 ------------ .../Request/UpsertTypedSearchAttributes.php | 7 +++++- src/Internal/Workflow/ScopeContext.php | 11 ++++++++++ .../Workflow/TypedSearchAttributesTest.php | 10 ++++++++- .../ConcurrentWorkflowContextTestCase.php | 3 +-- ....php => TypedSearchAttributesTestCase.php} | 22 +++++++++++++++++-- tests/Unit/DTO/WorkflowInfoTestCase.php | 1 + 7 files changed, 48 insertions(+), 20 deletions(-) rename tests/Unit/Common/{TypedSearchAttributesTest.php => TypedSearchAttributesTestCase.php} (91%) diff --git a/src/Common/TypedSearchAttributes.php b/src/Common/TypedSearchAttributes.php index f41bd056..319fc59e 100644 --- a/src/Common/TypedSearchAttributes.php +++ b/src/Common/TypedSearchAttributes.php @@ -169,20 +169,6 @@ public function offsetGet(string $name): mixed return $key === null ? null : $this->collection[$key]; } - /** - * @return array - */ - public function toArray(): array - { - $result = []; - /** @var SearchAttributeKey $key */ - foreach ($this as $key => $value) { - $result[$key->getName()] = $value; - } - - return $result; - } - /** * @param non-empty-string $name */ diff --git a/src/Internal/Transport/Request/UpsertTypedSearchAttributes.php b/src/Internal/Transport/Request/UpsertTypedSearchAttributes.php index 1accfc55..6c78f6ed 100644 --- a/src/Internal/Transport/Request/UpsertTypedSearchAttributes.php +++ b/src/Internal/Transport/Request/UpsertTypedSearchAttributes.php @@ -4,8 +4,10 @@ namespace Temporal\Internal\Transport\Request; +use DateTimeInterface; use Temporal\Common\SearchAttributes\SearchAttributeUpdate; use Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueSet; +use Temporal\Common\SearchAttributes\ValueType; use Temporal\Worker\Transport\Command\Client\Request; final class UpsertTypedSearchAttributes extends Request @@ -37,7 +39,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, diff --git a/src/Internal/Workflow/ScopeContext.php b/src/Internal/Workflow/ScopeContext.php index 78948a27..3ca446cd 100644 --- a/src/Internal/Workflow/ScopeContext.php +++ b/src/Internal/Workflow/ScopeContext.php @@ -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 diff --git a/tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php b/tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php index ad998bf8..9598454b 100644 --- a/tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php +++ b/tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php @@ -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] diff --git a/tests/Functional/ConcurrentWorkflowContextTestCase.php b/tests/Functional/ConcurrentWorkflowContextTestCase.php index 88644fd1..60da46fe 100644 --- a/tests/Functional/ConcurrentWorkflowContextTestCase.php +++ b/tests/Functional/ConcurrentWorkflowContextTestCase.php @@ -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(); @@ -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 << false, ], 'name3' => [ - 'type' => 'int', + 'type' => 'int64', 'value' => 42, ], 'name4' => [ @@ -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')); + + } } diff --git a/tests/Unit/DTO/WorkflowInfoTestCase.php b/tests/Unit/DTO/WorkflowInfoTestCase.php index 7321682f..bc801ad6 100644 --- a/tests/Unit/DTO/WorkflowInfoTestCase.php +++ b/tests/Unit/DTO/WorkflowInfoTestCase.php @@ -44,6 +44,7 @@ public function testMarshalling(): void 'ParentWorkflowNamespace' => null, 'ParentWorkflowExecution' => null, 'SearchAttributes' => null, + 'TypedSearchAttributes' => [], 'Memo' => null, 'BinaryChecksum' => '', ];