Skip to content

Commit b8f6c68

Browse files
committed
Add collection TypedSearchAttributes
1 parent b75f71d commit b8f6c68

File tree

3 files changed

+146
-27
lines changed

3 files changed

+146
-27
lines changed

src/Common/SearchAttributes/SearchAttributeKey.php

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,84 +11,104 @@
1111
use Temporal\Common\SearchAttributes\SearchAttributeKey\KeywordListValue;
1212
use Temporal\Common\SearchAttributes\SearchAttributeKey\KeywordValue;
1313
use Temporal\Common\SearchAttributes\SearchAttributeKey\StringValue;
14+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueSet;
1415

1516
/**
1617
* @psalm-immutable
18+
* @method ValueSet valueSet(mixed $value)
1719
*/
1820
abstract class SearchAttributeKey
1921
{
2022
/**
21-
* @param non-empty-string $key
23+
* @param non-empty-string $name
2224
*/
2325
final protected function __construct(
24-
private readonly string $key,
26+
private readonly string $name,
2527
) {}
2628

2729
/**
28-
* @param non-empty-string $key
30+
* @param non-empty-string $name
2931
*/
30-
public static function forBool(string $key): BoolValue
32+
public static function forBool(string $name): BoolValue
3133
{
32-
return new BoolValue($key);
34+
return new BoolValue($name);
3335
}
3436

3537
/**
36-
* @param non-empty-string $key
38+
* @param non-empty-string $name
3739
*/
38-
public static function forInteger(string $key): IntValue
40+
public static function forInteger(string $name): IntValue
3941
{
40-
return new IntValue($key);
42+
return new IntValue($name);
4143
}
4244

4345
/**
44-
* @param non-empty-string $key
46+
* @param non-empty-string $name
4547
*/
46-
public static function forFloat(string $key): FloatValue
48+
public static function forFloat(string $name): FloatValue
4749
{
48-
return new FloatValue($key);
50+
return new FloatValue($name);
4951
}
5052

5153
/**
52-
* @param non-empty-string $key
54+
* @param non-empty-string $name
5355
*/
54-
public static function forKeyword(string $key): KeywordValue
56+
public static function forKeyword(string $name): KeywordValue
5557
{
56-
return new KeywordValue($key);
58+
return new KeywordValue($name);
5759
}
5860

5961
/**
60-
* @param non-empty-string $key
62+
* @param non-empty-string $name
6163
*/
62-
public static function forString(string $key): StringValue
64+
public static function forString(string $name): StringValue
6365
{
64-
return new StringValue($key);
66+
return new StringValue($name);
6567
}
6668

6769
/**
68-
* @param non-empty-string $key
70+
* @param non-empty-string $name
6971
*/
70-
public static function forDatetime(string $key): DatetimeValue
72+
public static function forDatetime(string $name): DatetimeValue
7173
{
72-
return new DatetimeValue($key);
74+
return new DatetimeValue($name);
7375
}
7476

7577
/**
76-
* @param non-empty-string $key
78+
* @param non-empty-string $name
7779
*/
78-
public static function forKeywordList(string $key): KeywordListValue
80+
public static function forKeywordList(string $name): KeywordListValue
7981
{
80-
return new KeywordListValue($key);
82+
return new KeywordListValue($name);
83+
}
84+
85+
public static function for(ValueType $tryFrom, mixed $name): self
86+
{
87+
return match ($tryFrom) {
88+
ValueType::Bool => self::forBool($name),
89+
ValueType::Int => self::forInteger($name),
90+
ValueType::Float => self::forFloat($name),
91+
ValueType::Keyword => self::forKeyword($name),
92+
ValueType::String => self::forString($name),
93+
ValueType::Datetime => self::forDatetime($name),
94+
ValueType::KeywordList => self::forKeywordList($name),
95+
};
96+
}
97+
98+
public function getName(): string
99+
{
100+
return $this->name;
81101
}
82102

83103
public function valueUnset(): SearchAttributeUpdate
84104
{
85105
return SearchAttributeUpdate::valueUnset($this->key, $this->getType());
86106
}
87107

108+
abstract public function getType(): ValueType;
109+
88110
protected function prepareValueSet(mixed $value): SearchAttributeUpdate
89111
{
90112
return SearchAttributeUpdate::valueSet($this->key, $this->getType(), $value);
91113
}
92-
93-
abstract protected function getType(): ValueType;
94114
}

src/Common/SearchAttributes/SearchAttributeUpdate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ protected function __construct(
2424
/**
2525
* @param non-empty-string $key
2626
*/
27-
public static function valueSet(string $key, ValueType $type, mixed $value): self
27+
public static function valueSet(string $key, ValueType $type, mixed $value): ValueSet
2828
{
2929
return new ValueSet($key, $type, $value);
3030
}
3131

3232
/**
3333
* @param non-empty-string $key
3434
*/
35-
public static function valueUnset(string $key, ValueType $type): self
35+
public static function valueUnset(string $key, ValueType $type): ValueUnset
3636
{
3737
return new ValueUnset($key, $type);
3838
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeKey;
8+
use Temporal\Common\SearchAttributes\ValueType;
9+
10+
class TypedSearchAttributes implements \Countable
11+
{
12+
/**
13+
* @param null|\SplObjectStorage<SearchAttributeKey, mixed> $collection
14+
*/
15+
private function __construct(
16+
private readonly ?\SplObjectStorage $collection = null,
17+
) {}
18+
19+
public static function empty(): self
20+
{
21+
return new self();
22+
}
23+
24+
/**
25+
* @param array<non-empty-string, array{value: string, type: non-empty-string}> $array
26+
*
27+
* ```php
28+
* [
29+
* "foo" => [
30+
* "value" => "bar",
31+
* "type" => "keyword",
32+
* ],
33+
* ]
34+
* ```
35+
*
36+
* @internal
37+
*/
38+
public static function fromJsonArray(array $array): self
39+
{
40+
if ($array === []) {
41+
return self::empty();
42+
}
43+
44+
$collection = new \SplObjectStorage();
45+
foreach ($array as $name => ['type' => $type, 'value' => $value]) {
46+
try {
47+
$vt = ValueType::from($type);
48+
$key = SearchAttributeKey::for($vt, $name);
49+
$collection->offsetSet($key, $key->valueSet($value)->value);
50+
} catch (\Throwable) {
51+
// Ignore invalid values.
52+
}
53+
}
54+
55+
return new self($collection);
56+
}
57+
58+
/**
59+
* @throws \LogicException If key type mismatch.
60+
*/
61+
public function get(SearchAttributeKey $key): mixed
62+
{
63+
$found = $this->findByName($key->getName());
64+
65+
return match (true) {
66+
$found === null => null,
67+
$found->getType() === $key->getType() => $this->collection[$found],
68+
default => throw new \LogicException('Search Attribute type mismatch.'),
69+
};
70+
}
71+
72+
public function hasKey(SearchAttributeKey $key): bool
73+
{
74+
return $this->findByName($key->getName()) !== null;
75+
}
76+
77+
public function withValue(SearchAttributeKey $key, mixed $value): self {}
78+
79+
/**
80+
* @return int<0, max>
81+
*/
82+
public function count(): int
83+
{
84+
$count = (int) $this->collection?->count();
85+
\assert($count >= 0);
86+
return $count;
87+
}
88+
89+
private function findByName(string $name): ?SearchAttributeKey
90+
{
91+
foreach ($this->collection ?? [] as $item) {
92+
if ($item->getName() === $name) {
93+
return $item;
94+
}
95+
}
96+
97+
return null;
98+
}
99+
}

0 commit comments

Comments
 (0)