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 dee3fb5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 20 deletions.
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 @@ -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
Expand Down Expand Up @@ -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,
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 dee3fb5

Please sign in to comment.