Skip to content

Commit

Permalink
Add SearchAttributeKey value objects
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Dec 24, 2024
1 parent fb4d5ec commit 0f98b14
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/DataConverter/SearchAttributes/BoolValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;

/**
* @template-extends SearchAttributeKey<bool>
* @psalm-immutable
*/
final class BoolValue extends SearchAttributeKey
{
protected function getType(): string
{
return ValueType::Bool->value;
}
}
24 changes: 24 additions & 0 deletions src/DataConverter/SearchAttributes/DatetimeValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;

use DateTimeImmutable;

/**
* @template-extends SearchAttributeKey<DateTimeImmutable>
* @psalm-immutable
*/
final class DatetimeValue extends SearchAttributeKey
{
public function getValue(): string
{
return $this->value->format(\DateTimeImmutable::RFC3339);
}

protected function getType(): string
{
return ValueType::Datetime->value;
}
}
17 changes: 17 additions & 0 deletions src/DataConverter/SearchAttributes/FloatValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;

/**
* @template-extends SearchAttributeKey<float>
* @psalm-immutable
*/
final class FloatValue extends SearchAttributeKey
{
protected function getType(): string
{
return ValueType::Float->value;
}
}
17 changes: 17 additions & 0 deletions src/DataConverter/SearchAttributes/IntValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;

/**
* @template-extends SearchAttributeKey<int>
* @psalm-immutable
*/
final class IntValue extends SearchAttributeKey
{
protected function getType(): string
{
return ValueType::Int->value;
}
}
17 changes: 17 additions & 0 deletions src/DataConverter/SearchAttributes/KeywordListValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;

/**
* @template-extends SearchAttributeKey<list<string>>
* @psalm-immutable
*/
final class KeywordListValue extends SearchAttributeKey
{
protected function getType(): string
{
return ValueType::KeywordList->value;
}
}
17 changes: 17 additions & 0 deletions src/DataConverter/SearchAttributes/KeywordValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;

/**
* @template-extends SearchAttributeKey<string>
* @psalm-immutable
*/
final class KeywordValue extends SearchAttributeKey
{
protected function getType(): string
{
return ValueType::Keyword->value;
}
}
100 changes: 100 additions & 0 deletions src/DataConverter/SearchAttributes/SearchAttributeKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;

/**
* @template-covariant TValue
* @psalm-immutable
*/
abstract class SearchAttributeKey implements \JsonSerializable
{
/**
* @param non-empty-string $key
* @param TValue $value
*/
final protected function __construct(
public readonly string $key,
public readonly mixed $value,
) {}

/**
* @param non-empty-string $key
*/
public static function bool(string $key, bool $value): BoolValue
{
return new BoolValue($key, $value);
}

/**
* @param non-empty-string $key
*/
public static function integer(string $key, int $value): IntValue
{
return new IntValue($key, $value);
}

/**
* @param non-empty-string $key
*/
public static function float(string $key, float $value): FloatValue
{
return new FloatValue($key, $value);
}

/**
* @param non-empty-string $key
*/
public static function keyword(string $key, string $value): KeywordValue
{
return new KeywordValue($key, $value);
}

/**
* @param non-empty-string $key
*/
public static function string(string $key, string $value): StringValue
{
return new StringValue($key, $value);
}

public static function datetime(string $key, \DateTimeInterface|string $value): DatetimeValue
{
return new DatetimeValue($key, match (true) {

Check failure on line 64 in src/DataConverter/SearchAttributes/SearchAttributeKey.php

View workflow job for this annotation

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

ArgumentTypeCoercion

src/DataConverter/SearchAttributes/SearchAttributeKey.php:64:34: ArgumentTypeCoercion: Argument 1 of Temporal\DataConverter\SearchAttributes\DatetimeValue::__construct expects non-empty-string, but parent type string provided (see https://psalm.dev/193)
\is_string($value) => new \DateTimeImmutable($value),
$value instanceof \DateTimeImmutable => $value,
default => \DateTimeImmutable::createFromInterface($value),
});
}

/**
* @param non-empty-string $key
* @param iterable<scalar> $value
*/
public static function keywordList(string $key, iterable $value): KeywordListValue
{
/** @var list<string> $values */
$values = [];
foreach ($value as $item) {
$values[] = (string) $item;
}

return new KeywordListValue($key, $values);
}

public function jsonSerialize(): array
{
return [
'type' => $this->getType(),
'value' => $this->getValue(),
];
}

abstract protected function getType(): string;

protected function getValue(): mixed
{
return $this->value;
}
}
17 changes: 17 additions & 0 deletions src/DataConverter/SearchAttributes/StringValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;

/**
* @template-extends SearchAttributeKey<string>
* @psalm-immutable
*/
final class StringValue extends SearchAttributeKey
{
protected function getType(): string
{
return ValueType::String->value;
}
}
16 changes: 16 additions & 0 deletions src/DataConverter/SearchAttributes/ValueType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;

enum ValueType: string
{
case Bool = 'bool';
case Float = 'float64';
case Int = 'int';
case Keyword = 'keyword';
case KeywordList = 'keyword_list';
case String = 'string';
case Datetime = 'datetime';
}

0 comments on commit 0f98b14

Please sign in to comment.