Skip to content

Commit

Permalink
Convert values to forType->setType with update DTO as the result
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Dec 27, 2024
1 parent 0f98b14 commit ac18160
Show file tree
Hide file tree
Showing 21 changed files with 375 additions and 227 deletions.
96 changes: 96 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes;

use Temporal\Common\SearchAttributes\SearchAttributeKey\BoolValue;
use Temporal\Common\SearchAttributes\SearchAttributeKey\DatetimeValue;
use Temporal\Common\SearchAttributes\SearchAttributeKey\FloatValue;
use Temporal\Common\SearchAttributes\SearchAttributeKey\IntValue;
use Temporal\Common\SearchAttributes\SearchAttributeKey\KeywordListValue;
use Temporal\Common\SearchAttributes\SearchAttributeKey\KeywordValue;
use Temporal\Common\SearchAttributes\SearchAttributeKey\StringValue;

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

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

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

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

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

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

public static function forDatetime(string $key): DatetimeValue
{
return new DatetimeValue($key);

Check failure on line 71 in src/Common/SearchAttributes/SearchAttributeKey.php

View workflow job for this annotation

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

ArgumentTypeCoercion

src/Common/SearchAttributes/SearchAttributeKey.php:71:34: ArgumentTypeCoercion: Argument 1 of Temporal\Common\SearchAttributes\SearchAttributeKey\DatetimeValue::__construct expects non-empty-string, but parent type string provided (see https://psalm.dev/193)
}

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

Check failure on line 82 in src/Common/SearchAttributes/SearchAttributeKey.php

View workflow job for this annotation

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

UndefinedVariable

src/Common/SearchAttributes/SearchAttributeKey.php:82:18: UndefinedVariable: Cannot find referenced variable $value (see https://psalm.dev/024)
$values[] = (string) $item;
}

return new KeywordListValue($key, $values);

Check failure on line 86 in src/Common/SearchAttributes/SearchAttributeKey.php

View workflow job for this annotation

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

TooManyArguments

src/Common/SearchAttributes/SearchAttributeKey.php:86:16: TooManyArguments: Too many arguments for Temporal\Common\SearchAttributes\SearchAttributeKey\KeywordListValue::__construct - expecting 1 but saw 2 (see https://psalm.dev/026)
}

public function valueUnset(): SearchAttributeUpdate {}

Check failure on line 89 in src/Common/SearchAttributes/SearchAttributeKey.php

View workflow job for this annotation

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

InvalidReturnType

src/Common/SearchAttributes/SearchAttributeKey.php:89:35: InvalidReturnType: Not all code paths of Temporal\Common\SearchAttributes\SearchAttributeKey::valueUnset end in a return statement, return type Temporal\Common\SearchAttributes\SearchAttributeUpdate expected (see https://psalm.dev/011)

protected function prepareValueSet(mixed $value): SearchAttributeUpdate {
return SearchAttributeUpdate::valueSet($this->key, $this->getType(), $value);

Check failure on line 92 in src/Common/SearchAttributes/SearchAttributeKey.php

View workflow job for this annotation

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

ImpureMethodCall

src/Common/SearchAttributes/SearchAttributeKey.php:92:39: ImpureMethodCall: Cannot call a possibly-mutating method from a mutation-free context (see https://psalm.dev/203)
}

abstract protected function getType(): ValueType;
}
26 changes: 26 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeKey/BoolValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes\SearchAttributeKey;

use Temporal\Common\SearchAttributes\SearchAttributeKey;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
use Temporal\Common\SearchAttributes\ValueType;

/**
* @template-extends SearchAttributeKey<bool>
* @psalm-immutable
*/
final class BoolValue extends SearchAttributeKey
{
protected function getType(): ValueType
{
return ValueType::Bool;
}

public function valueSet(bool $value): SearchAttributeUpdate
{
return $this->prepareValueSet($value);
}
}
35 changes: 35 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeKey/DatetimeValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes\SearchAttributeKey;

use DateTimeImmutable;
use DateTimeInterface;
use Temporal\Common\SearchAttributes\SearchAttributeKey;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
use Temporal\Common\SearchAttributes\ValueType;

/**
* @template-extends SearchAttributeKey<DateTimeImmutable>
* @psalm-immutable
*/
final class DatetimeValue extends SearchAttributeKey
{
protected function getType(): ValueType
{
return ValueType::Datetime;
}

/**
* @param non-empty-string|DateTimeInterface $value
*/
public function valueSet(string|DateTimeInterface $value): SearchAttributeUpdate
{
return $this->prepareValueSet(match (true) {
\is_string($value) => new \DateTimeImmutable($value),
$value instanceof \DateTimeImmutable => $value,
default => \DateTimeImmutable::createFromInterface($value),
});
}
}
26 changes: 26 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeKey/FloatValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes\SearchAttributeKey;

use Temporal\Common\SearchAttributes\SearchAttributeKey;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
use Temporal\Common\SearchAttributes\ValueType;

/**
* @template-extends SearchAttributeKey<float>
* @psalm-immutable
*/
final class FloatValue extends SearchAttributeKey
{
protected function getType(): ValueType
{
return ValueType::Float;
}

public function valueSet(float $value): SearchAttributeUpdate
{
return $this->prepareValueSet($value);
}
}
26 changes: 26 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeKey/IntValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes\SearchAttributeKey;

use Temporal\Common\SearchAttributes\SearchAttributeKey;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
use Temporal\Common\SearchAttributes\ValueType;

/**
* @template-extends SearchAttributeKey<int>
* @psalm-immutable
*/
final class IntValue extends SearchAttributeKey
{
protected function getType(): ValueType
{
return ValueType::Int;
}

public function valueSet(int $value): SearchAttributeUpdate
{
return $this->prepareValueSet($value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes\SearchAttributeKey;

use Temporal\Common\SearchAttributes\SearchAttributeKey;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
use Temporal\Common\SearchAttributes\ValueType;

/**
* @template-extends SearchAttributeKey<list<string>>
* @psalm-immutable
*/
final class KeywordListValue extends SearchAttributeKey
{
protected function getType(): ValueType
{
return ValueType::KeywordList;
}

/**
* @param iterable<string|\Stringable> $value

Check failure on line 23 in src/Common/SearchAttributes/SearchAttributeKey/KeywordListValue.php

View workflow job for this annotation

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

MismatchingDocblockParamType

src/Common/SearchAttributes/SearchAttributeKey/KeywordListValue.php:23:15: MismatchingDocblockParamType: Parameter $value has wrong type 'iterable<mixed, Stringable|string>', should be 'array<array-key, mixed>' (see https://psalm.dev/141)
*/
public function valueSet(array $value): SearchAttributeUpdate
{
$values = [];
foreach ($value as $v) {
$values[] = (string) $v;
}

return $this->prepareValueSet($values);
}
}
26 changes: 26 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeKey/KeywordValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes\SearchAttributeKey;

use Temporal\Common\SearchAttributes\SearchAttributeKey;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
use Temporal\Common\SearchAttributes\ValueType;

/**
* @template-extends SearchAttributeKey<string>
* @psalm-immutable
*/
final class KeywordValue extends SearchAttributeKey
{
protected function getType(): ValueType
{
return ValueType::Keyword;
}

public function valueSet(string|\Stringable $value): SearchAttributeUpdate
{
return $this->prepareValueSet((string) $value);
}
}
26 changes: 26 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeKey/StringValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes\SearchAttributeKey;

use Temporal\Common\SearchAttributes\SearchAttributeKey;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
use Temporal\Common\SearchAttributes\ValueType;

/**
* @template-extends SearchAttributeKey<string>
* @psalm-immutable
*/
final class StringValue extends SearchAttributeKey
{
protected function getType(): ValueType
{
return ValueType::String;
}

public function valueSet(string|\Stringable $value): SearchAttributeUpdate
{
return $this->prepareValueSet((string) $value);
}
}
33 changes: 33 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes;

use Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueSet;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueUnset;

/**
* @template-extends SearchAttributeKey<string>
* @psalm-immutable
*/
abstract class SearchAttributeUpdate

Check failure on line 14 in src/Common/SearchAttributes/SearchAttributeUpdate.php

View workflow job for this annotation

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

InvalidDocblock

src/Common/SearchAttributes/SearchAttributeUpdate.php:14:1: InvalidDocblock: @template-extends must include the name of an extended class, got Temporal\Common\SearchAttributes\SearchAttributeKey<string> (see https://psalm.dev/008)
{
/**
* @param non-empty-string $key
*/
protected function __construct(
public readonly string $key,
public readonly ValueType $type,
) {}

public static function valueSet(string $key, ValueType $type, mixed $value): self
{
return new ValueSet($key, $type, $value);

Check failure on line 26 in src/Common/SearchAttributes/SearchAttributeUpdate.php

View workflow job for this annotation

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

ArgumentTypeCoercion

src/Common/SearchAttributes/SearchAttributeUpdate.php:26:29: ArgumentTypeCoercion: Argument 1 of Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueSet::__construct expects non-empty-string, but parent type string provided (see https://psalm.dev/193)
}

public static function valueUnset(string $key, ValueType $type): self
{
return new ValueUnset($key, $type);

Check failure on line 31 in src/Common/SearchAttributes/SearchAttributeUpdate.php

View workflow job for this annotation

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

ArgumentTypeCoercion

src/Common/SearchAttributes/SearchAttributeUpdate.php:31:31: ArgumentTypeCoercion: Argument 1 of Temporal\Common\SearchAttributes\SearchAttributeUpdate\ValueUnset::__construct expects non-empty-string, but parent type string provided (see https://psalm.dev/193)
}
}
27 changes: 27 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeUpdate/ValueSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes\SearchAttributeUpdate;

use Temporal\Common\SearchAttributes\SearchAttributeKey;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;
use Temporal\Common\SearchAttributes\ValueType;

/**
* @template-extends SearchAttributeKey<string>
* @psalm-immutable
*/
final class ValueSet extends SearchAttributeUpdate

Check failure on line 15 in src/Common/SearchAttributes/SearchAttributeUpdate/ValueSet.php

View workflow job for this annotation

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

InvalidDocblock

src/Common/SearchAttributes/SearchAttributeUpdate/ValueSet.php:15:1: InvalidDocblock: @template-extends must include the name of an extended class, got Temporal\Common\SearchAttributes\SearchAttributeKey<string> (see https://psalm.dev/008)
{
/**
* @param non-empty-string $key
*/
protected function __construct(
string $key,
ValueType $type,
public readonly mixed $value,
) {
parent::__construct($key, $type);
}
}
16 changes: 16 additions & 0 deletions src/Common/SearchAttributes/SearchAttributeUpdate/ValueUnset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Temporal\Common\SearchAttributes\SearchAttributeUpdate;

use Temporal\Common\SearchAttributes\SearchAttributeKey;
use Temporal\Common\SearchAttributes\SearchAttributeUpdate;

/**
* @template-extends SearchAttributeKey<string>
* @psalm-immutable
*/
final class ValueUnset extends SearchAttributeUpdate
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Temporal\DataConverter\SearchAttributes;
namespace Temporal\Common\SearchAttributes;

enum ValueType: string
{
Expand Down
Loading

0 comments on commit ac18160

Please sign in to comment.