Skip to content

Commit 4c68bb3

Browse files
committed
Convert values to forType->setType with update DTO as the result
1 parent 0f98b14 commit 4c68bb3

21 files changed

+368
-227
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeKey\BoolValue;
8+
use Temporal\Common\SearchAttributes\SearchAttributeKey\DatetimeValue;
9+
use Temporal\Common\SearchAttributes\SearchAttributeKey\FloatValue;
10+
use Temporal\Common\SearchAttributes\SearchAttributeKey\IntValue;
11+
use Temporal\Common\SearchAttributes\SearchAttributeKey\KeywordListValue;
12+
use Temporal\Common\SearchAttributes\SearchAttributeKey\KeywordValue;
13+
use Temporal\Common\SearchAttributes\SearchAttributeKey\StringValue;
14+
15+
/**
16+
* @template-covariant TValue
17+
* @psalm-immutable
18+
*/
19+
abstract class SearchAttributeKey
20+
{
21+
/**
22+
* @param non-empty-string $key
23+
* @param TValue $value
24+
*/
25+
final protected function __construct(
26+
protected readonly string $key,
27+
) {}
28+
29+
/**
30+
* @param non-empty-string $key
31+
*/
32+
public static function forBool(string $key): BoolValue
33+
{
34+
return new BoolValue($key);
35+
}
36+
37+
/**
38+
* @param non-empty-string $key
39+
*/
40+
public static function forInteger(string $key): IntValue
41+
{
42+
return new IntValue($key);
43+
}
44+
45+
/**
46+
* @param non-empty-string $key
47+
*/
48+
public static function forFloat(string $key): FloatValue
49+
{
50+
return new FloatValue($key);
51+
}
52+
53+
/**
54+
* @param non-empty-string $key
55+
*/
56+
public static function forKeyword(string $key): KeywordValue
57+
{
58+
return new KeywordValue($key);
59+
}
60+
61+
/**
62+
* @param non-empty-string $key
63+
*/
64+
public static function forString(string $key): StringValue
65+
{
66+
return new StringValue($key);
67+
}
68+
69+
public static function forDatetime(string $key): DatetimeValue
70+
{
71+
return new DatetimeValue($key);
72+
}
73+
74+
/**
75+
* @param non-empty-string $key
76+
*/
77+
public static function forKeywordList(string $key): KeywordListValue
78+
{
79+
return new KeywordListValue($key);
80+
}
81+
82+
public function valueUnset(): SearchAttributeUpdate {}
83+
84+
protected function prepareValueSet(mixed $value): SearchAttributeUpdate {
85+
return SearchAttributeUpdate::valueSet($this->key, $this->getType(), $value);
86+
}
87+
88+
abstract protected function getType(): ValueType;
89+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes\SearchAttributeKey;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeKey;
8+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
9+
use Temporal\Common\SearchAttributes\ValueType;
10+
11+
/**
12+
* @template-extends SearchAttributeKey<bool>
13+
* @psalm-immutable
14+
*/
15+
final class BoolValue extends SearchAttributeKey
16+
{
17+
protected function getType(): ValueType
18+
{
19+
return ValueType::Bool;
20+
}
21+
22+
public function valueSet(bool $value): SearchAttributeUpdate
23+
{
24+
return $this->prepareValueSet($value);
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes\SearchAttributeKey;
6+
7+
use DateTimeImmutable;
8+
use DateTimeInterface;
9+
use Temporal\Common\SearchAttributes\SearchAttributeKey;
10+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
11+
use Temporal\Common\SearchAttributes\ValueType;
12+
13+
/**
14+
* @template-extends SearchAttributeKey<DateTimeImmutable>
15+
* @psalm-immutable
16+
*/
17+
final class DatetimeValue extends SearchAttributeKey
18+
{
19+
protected function getType(): ValueType
20+
{
21+
return ValueType::Datetime;
22+
}
23+
24+
/**
25+
* @param non-empty-string|DateTimeInterface $value
26+
*/
27+
public function valueSet(string|DateTimeInterface $value): SearchAttributeUpdate
28+
{
29+
return $this->prepareValueSet(match (true) {
30+
\is_string($value) => new \DateTimeImmutable($value),
31+
$value instanceof \DateTimeImmutable => $value,
32+
default => \DateTimeImmutable::createFromInterface($value),
33+
});
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes\SearchAttributeKey;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeKey;
8+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
9+
use Temporal\Common\SearchAttributes\ValueType;
10+
11+
/**
12+
* @template-extends SearchAttributeKey<float>
13+
* @psalm-immutable
14+
*/
15+
final class FloatValue extends SearchAttributeKey
16+
{
17+
protected function getType(): ValueType
18+
{
19+
return ValueType::Float;
20+
}
21+
22+
public function valueSet(float $value): SearchAttributeUpdate
23+
{
24+
return $this->prepareValueSet($value);
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes\SearchAttributeKey;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeKey;
8+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
9+
use Temporal\Common\SearchAttributes\ValueType;
10+
11+
/**
12+
* @template-extends SearchAttributeKey<int>
13+
* @psalm-immutable
14+
*/
15+
final class IntValue extends SearchAttributeKey
16+
{
17+
protected function getType(): ValueType
18+
{
19+
return ValueType::Int;
20+
}
21+
22+
public function valueSet(int $value): SearchAttributeUpdate
23+
{
24+
return $this->prepareValueSet($value);
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes\SearchAttributeKey;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeKey;
8+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
9+
use Temporal\Common\SearchAttributes\ValueType;
10+
11+
/**
12+
* @template-extends SearchAttributeKey<list<string>>
13+
* @psalm-immutable
14+
*/
15+
final class KeywordListValue extends SearchAttributeKey
16+
{
17+
protected function getType(): ValueType
18+
{
19+
return ValueType::KeywordList;
20+
}
21+
22+
/**
23+
* @param iterable<string|\Stringable> $value
24+
*/
25+
public function valueSet(array $value): SearchAttributeUpdate
26+
{
27+
$values = [];
28+
foreach ($value as $v) {
29+
$values[] = (string) $v;
30+
}
31+
32+
return $this->prepareValueSet($values);
33+
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes\SearchAttributeKey;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeKey;
8+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
9+
use Temporal\Common\SearchAttributes\ValueType;
10+
11+
/**
12+
* @template-extends SearchAttributeKey<string>
13+
* @psalm-immutable
14+
*/
15+
final class KeywordValue extends SearchAttributeKey
16+
{
17+
protected function getType(): ValueType
18+
{
19+
return ValueType::Keyword;
20+
}
21+
22+
public function valueSet(string|\Stringable $value): SearchAttributeUpdate
23+
{
24+
return $this->prepareValueSet((string) $value);
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes\SearchAttributeKey;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeKey;
8+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
9+
use Temporal\Common\SearchAttributes\ValueType;
10+
11+
/**
12+
* @template-extends SearchAttributeKey<string>
13+
* @psalm-immutable
14+
*/
15+
final class StringValue extends SearchAttributeKey
16+
{
17+
protected function getType(): ValueType
18+
{
19+
return ValueType::String;
20+
}
21+
22+
public function valueSet(string|\Stringable $value): SearchAttributeUpdate
23+
{
24+
return $this->prepareValueSet((string) $value);
25+
}
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueSet;
8+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueUnset;
9+
10+
/**
11+
* @template-extends SearchAttributeKey<string>
12+
* @psalm-immutable
13+
*/
14+
abstract class SearchAttributeUpdate
15+
{
16+
/**
17+
* @param non-empty-string $key
18+
*/
19+
protected function __construct(
20+
public readonly string $key,
21+
public readonly ValueType $type,
22+
) {}
23+
24+
public static function valueSet(string $key, ValueType $type, mixed $value): self
25+
{
26+
return new ValueSet($key, $type, $value);
27+
}
28+
29+
public static function valueUnset(string $key, ValueType $type): self
30+
{
31+
return new ValueUnset($key, $type);
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Common\SearchAttributes\SearchAttributeUpdate;
6+
7+
use Temporal\Common\SearchAttributes\SearchAttributeKey;
8+
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
9+
use Temporal\Common\SearchAttributes\ValueType;
10+
11+
/**
12+
* @template-extends SearchAttributeKey<string>
13+
* @psalm-immutable
14+
*/
15+
final class ValueSet extends SearchAttributeUpdate
16+
{
17+
/**
18+
* @param non-empty-string $key
19+
*/
20+
protected function __construct(
21+
string $key,
22+
ValueType $type,
23+
public readonly mixed $value,
24+
) {
25+
parent::__construct($key, $type);
26+
}
27+
}

0 commit comments

Comments
 (0)