Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds binding for Temporal\DataConverter\DataConverterInterface #37

Merged
merged 4 commits into from
Oct 14, 2022

Conversation

butschster
Copy link
Member

@butschster butschster commented Sep 25, 2022

Now you can define a list of data converters

use Spiral\Boot\Bootloader\Bootloader;
use Temporal\DataConverter\DataConverter;
use Temporal\DataConverter\DataConverterInterface;

class AppBootloader extends Bootloader 
{
    protected const SINGLETONS = [
        DataConverterInterface::class => [self::class, 'initDataConverter'],
    ];

    protected function initDataConverter(): DataConverterInterface
    {
        return new DataConverter(
            new NullConverter(),
            new BinaryConverter(),
            new ProtoJsonConverter(),
            new \App\ObjectConverter(),
            new JsonConverter()
        );
    }
}
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Temporal\Api\Common\V1\Payload;
use Temporal\DataConverter\Converter;
use Temporal\DataConverter\Type;
use Temporal\Exception\DataConverterException;

class ObjectConverter extends Converter
{
    public function __construct(
        private readonly SerializerInterface $serializer = new Serializer(normalizers: [
            new BackedEnumNormalizer(),
            new ObjectNormalizer(),
        ], encoders: [
            new JsonEncode(),
            new JsonDecode()
        ])
    ) {}

    public function getEncodingType(): string
    {
        return 'json/object';
    }

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

        return $this->create(
            $this->serializer->serialize($value, 'json')
        );
    }

    public function fromPayload(Payload $payload, Type $type): object
    {
        if (!$type->isClass()) {
            throw new DataConverterException('Unable to decode value using object converter.');
        }

        try {
            return $this->serializer->deserialize($payload->getData(), $type->getName(), 'json');
        } catch (ExceptionInterface $e) {
            throw new DataConverterException($e->getMessage(), $e->getCode(), $e);
        }
    }
}

issue #36

@butschster butschster added the enhancement New feature or request label Sep 25, 2022
@butschster butschster self-assigned this Sep 25, 2022
@butschster butschster linked an issue Sep 28, 2022 that may be closed by this pull request
# Conflicts:
#	.github/workflows/run-tests.yml
#	.github/workflows/static-analysis.yml
@butschster butschster merged commit 89e0134 into 2.0 Oct 14, 2022
@roquie
Copy link
Contributor

roquie commented Oct 16, 2022

release version please

@butschster butschster deleted the feature/data-converter branch April 13, 2023 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to setup Data Converters from config file
2 participants