Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/DataConverter/DataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function createDefault(): DataConverterInterface
return new DataConverter(
new NullConverter(),
new BinaryConverter(),
new RawValueConverter(),
new ProtoJsonConverter(),
new ProtoConverter(),
new JsonConverter(),
Expand All @@ -47,19 +48,18 @@ public function fromPayload(Payload $payload, $type)
$encoding = $meta[EncodingKeys::METADATA_ENCODING_KEY];

if (!isset($this->converters[$encoding])) {
throw new DataConverterException(\sprintf('Undefined payload encoding %s', $encoding));
throw new DataConverterException(\sprintf('Undefined payload encoding "%s"', $encoding));
}

$type = Type::create($type);
if (\in_array($type->getName(), [Type::TYPE_VOID, Type::TYPE_NULL, Type::TYPE_FALSE, Type::TYPE_TRUE], true)) {
return match ($type->getName()) {
Type::TYPE_VOID, Type::TYPE_NULL => null,
Type::TYPE_TRUE => true,
Type::TYPE_FALSE => false,
};
}

return $this->converters[$encoding]->fromPayload($payload, $type);
return match ($type->getName()) {
Type::TYPE_VOID,
Type::TYPE_NULL => null,
Type::TYPE_TRUE => true,
Type::TYPE_FALSE => false,
default => $this->converters[$encoding]->fromPayload($payload, $type),
};
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/DataConverter/EncodingKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class EncodingKeys
public const METADATA_MESSAGE_TYPE = 'messageType';
public const METADATA_ENCODING_NULL = 'binary/null';
public const METADATA_ENCODING_RAW = 'binary/plain';
public const METADATA_ENCODING_RAW_VALUE = 'binary';
public const METADATA_ENCODING_JSON = 'json/plain';
public const METADATA_ENCODING_PROTOBUF_JSON = 'json/protobuf';
public const METADATA_ENCODING_PROTOBUF = 'binary/protobuf';
Expand Down
29 changes: 29 additions & 0 deletions src/DataConverter/RawValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\DataConverter;

use Temporal\Api\Common\V1\Payload;

final class RawValue
{
private Payload $payload;

public function __construct(Payload $data)
{
$this->payload = $data;
}

public function getPayload(): Payload
{
return $this->payload;
}
}
39 changes: 39 additions & 0 deletions src/DataConverter/RawValueConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\DataConverter;

use Temporal\Api\Common\V1\Payload;

class RawValueConverter extends Converter
{
public function getEncodingType(): string
{
return EncodingKeys::METADATA_ENCODING_RAW_VALUE;
}

public function toPayload(mixed $value): ?Payload
{
if (!$value instanceof RawValue) {
return null;
}

$payload = $value->getPayload();
$payload->setMetadata([EncodingKeys::METADATA_ENCODING_KEY => EncodingKeys::METADATA_ENCODING_RAW_VALUE]);

return $payload;
}

public function fromPayload(Payload $payload, Type $type): RawValue
{
return new RawValue(clone $payload);
}
}
27 changes: 8 additions & 19 deletions src/DataConverter/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,14 @@ public static function fromReflectionType(\ReflectionType $type): self
*/
public static function create($type): Type
{
switch (true) {
case $type instanceof ReturnType:
return new self($type->name, $type->nullable);

case $type instanceof self:
return $type;

case \is_string($type):
return new self($type);

case $type instanceof \ReflectionClass:
return self::fromReflectionClass($type);

case $type instanceof \ReflectionType:
return self::fromReflectionType($type);

default:
return new self();
}
return match (true) {
$type instanceof ReturnType => new self($type->name, $type->nullable),
$type instanceof self => $type,
\is_string($type) => new self($type),
$type instanceof \ReflectionClass => self::fromReflectionClass($type),
$type instanceof \ReflectionType => self::fromReflectionType($type),
default => new self(),
};
}

public function getName(): string
Expand Down
2 changes: 2 additions & 0 deletions tests/Acceptance/App/Feature/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Temporal\Tests\Acceptance\App\Feature;

use Temporal\DataConverter\RawValueConverter;
use Temporal\Tests\Acceptance\App\Attribute\Client;
use Temporal\Tests\Acceptance\App\Runtime\State;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -52,6 +53,7 @@ public function workflowClient(\ReflectionParameter $context): WorkflowClientInt
if ($attribute->payloadConverters !== []) {
$converters = [
new NullConverter(),
new RawValueConverter(),
new BinaryConverter(),
new ProtoConverter(),
new ProtoJsonConverter(),
Expand Down
61 changes: 61 additions & 0 deletions tests/Acceptance/Extra/DataConverter/RawValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Acceptance\Extra\DataConverter\RawValue;

use PHPUnit\Framework\Attributes\Test;
use Temporal\Activity\ActivityInterface;
use Temporal\Activity\ActivityMethod;
use Temporal\Activity\ActivityOptions;
use Temporal\Api\Common\V1\Payload;
use Temporal\Client\WorkflowStubInterface;
use Temporal\DataConverter\RawValue;
use Temporal\Tests\Acceptance\App\Attribute\Stub;
use Temporal\Tests\Acceptance\App\TestCase;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowInterface;
use Temporal\Workflow\WorkflowMethod;

class RawValueTest extends TestCase
{
#[Test]
public function check(
#[Stub('Extra_DataConverter_RawValue')]
WorkflowStubInterface $stub,
): void {
$result = $stub->getResult();

self::assertInstanceOf(RawValue::class, $result);
self::assertInstanceOf(Payload::class, $result->getPayload());
self::assertSame('hello world', $result->getPayload()->getData());
}
}

#[WorkflowInterface]
class FeatureWorkflow
{
#[WorkflowMethod('Extra_DataConverter_RawValue')]
public function run()
{
$rawValue = new RawValue(new Payload(['data' => 'hello world']));

$activity = Workflow::newActivityStub(
RawValueActivity::class,
ActivityOptions::new()
->withScheduleToCloseTimeout('1 minute'),
);

return yield $activity->bypass($rawValue);
}
}

#[ActivityInterface(prefix: 'RawValueActivity.')]
class RawValueActivity
{
#[ActivityMethod]
public function bypass(RawValue $arg)
{
return $arg;
}
}
2 changes: 2 additions & 0 deletions tests/Acceptance/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Temporal\DataConverter\NullConverter;
use Temporal\DataConverter\ProtoConverter;
use Temporal\DataConverter\ProtoJsonConverter;
use Temporal\DataConverter\RawValueConverter;
use Temporal\Internal\Support\StackRenderer;
use Temporal\Testing\Command;
use Temporal\Tests\Acceptance\App\Runtime\Feature;
Expand Down Expand Up @@ -52,6 +53,7 @@
$converters = [
new NullConverter(),
new BinaryConverter(),
new RawValueConverter(),
new ProtoJsonConverter(),
new ProtoConverter(),
new JsonConverter(),
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/DataConverter/RawValueConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Unit\DataConverter;

use Temporal\Api\Common\V1\Payload;
use Temporal\DataConverter\DataConverter;
use Temporal\DataConverter\DataConverterInterface;
use Temporal\DataConverter\EncodingKeys;
use Temporal\DataConverter\RawValue;
use Temporal\Tests\Unit\AbstractUnit;

/**
* @group unit
* @group data-converter
*/
class RawValueConverterTest extends AbstractUnit
{
public function testRawPayloadEncoding(): void
{
$innerPayload = new Payload(['data' => 1]);
$message = new RawValue($innerPayload);

$payload = DataConverter::createDefault()->toPayload($message);

self::assertSame($innerPayload, $payload);
self::assertSame(EncodingKeys::METADATA_ENCODING_RAW_VALUE, $payload->getMetadata()[EncodingKeys::METADATA_ENCODING_KEY]);
}

public function testRawPayloadDecoding(): void
{
$innerPayload = new Payload(['data' => 1]);
$message = new RawValue($innerPayload);

$encoded = DataConverter::createDefault()->toPayload($message);
$decoded = DataConverter::createDefault()->fromPayload($encoded, RawValue::class);

self::assertInstanceOf(RawValue::class, $decoded);
self::assertSame($decoded->getPayload(), $encoded);
}

protected function create(): DataConverterInterface
{
return DataConverter::createDefault();
}
}