From 6da3e545525c57dde7471f5487d85e0a4f435efb Mon Sep 17 00:00:00 2001 From: Alex Ostrovsky Date: Sun, 28 Jan 2024 08:55:53 +0100 Subject: [PATCH 1/2] feat: add support for PHP 8 attributes and symfony 7 --- .github/workflows/checks.yml | 23 +++------ README.md | 2 +- composer.json | 4 +- .../Symfony/Serializer/AvroSerDeEncoder.php | 2 +- .../Schema/Generation/AttributeReader.php | 30 ++++++++++++ .../Generation/Attributes/AvroAliases.php | 39 +++++++++++++++ .../Generation/Attributes/AvroDefault.php | 33 +++++++++++++ .../Schema/Generation/Attributes/AvroDoc.php | 33 +++++++++++++ .../Generation/Attributes/AvroItems.php | 47 +++++++++++++++++++ .../Schema/Generation/Attributes/AvroName.php | 33 +++++++++++++ .../Generation/Attributes/AvroNamespace.php | 33 +++++++++++++ .../Generation/Attributes/AvroOrder.php | 33 +++++++++++++ .../Schema/Generation/Attributes/AvroSize.php | 33 +++++++++++++ .../Generation/Attributes/AvroSymbols.php | 41 ++++++++++++++++ .../Generation/Attributes/AvroTargetClass.php | 33 +++++++++++++ .../Schema/Generation/Attributes/AvroType.php | 44 +++++++++++++++++ .../Generation/Attributes/AvroValues.php | 47 +++++++++++++++++++ .../Schema/Generation/Attributes/Order.php | 12 +++++ .../Schema/Generation/Attributes/Type.php | 31 ++++++++++++ 19 files changed, 533 insertions(+), 20 deletions(-) create mode 100644 src/Objects/Schema/Generation/AttributeReader.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroAliases.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroDefault.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroDoc.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroItems.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroName.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroNamespace.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroOrder.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroSize.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroSymbols.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroTargetClass.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroType.php create mode 100644 src/Objects/Schema/Generation/Attributes/AvroValues.php create mode 100644 src/Objects/Schema/Generation/Attributes/Order.php create mode 100644 src/Objects/Schema/Generation/Attributes/Type.php diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index be622c6..2f00700 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -27,18 +27,15 @@ jobs: strategy: matrix: php: - - - version: '7.4' - xdebug: '3.1.5' - - - version: '8.0' - xdebug: '3.1.5' - version: '8.1' xdebug: '3.1.5' - version: '8.2' xdebug: '3.2.1' + - + version: '8.3' + xdebug: '3.3.1' runs-on: ubuntu-20.04 steps: - @@ -81,12 +78,6 @@ jobs: strategy: matrix: php: - - - version: '7.4' - composer: --prefer-lowest - - - version: '8.0' - composer: --prefer-lowest - version: '8.1' composer: --prefer-lowest @@ -94,13 +85,13 @@ jobs: version: '8.2' composer: --prefer-lowest - - version: '7.4' - composer: --prefer-stable + version: '8.3' + composer: --prefer-lowest - - version: '8.0' + version: '8.1' composer: --prefer-stable - - version: '8.1' + version: '8.2' composer: --prefer-stable - version: '8.2' diff --git a/README.md b/README.md index a594954..017e875 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Avro SerDe for PHP 7.3+ and 8.0 +# Avro SerDe for PHP 8.1+ [![php-confluent-serde Actions Status](https://github.com/flix-tech/avro-serde-php/workflows/php-confluent-serde/badge.svg?branch=master)](https://github.com/flix-tech/avro-serde-php/actions) [![Maintainability](https://api.codeclimate.com/v1/badges/7500470a6812cf5a1ad5/maintainability)](https://codeclimate.com/github/flix-tech/avro-serde-php/maintainability) diff --git a/composer.json b/composer.json index 15fddc4..da7acc3 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ } ], "require": { - "php": "^7.3|^8.0", + "php": "^8.1", "beberlei/assert": "^2.9.9|~3.0", "flix-tech/confluent-schema-registry-api": "^8.0", "guzzlehttp/promises": "^1.4.0|^2.0.0", @@ -35,7 +35,7 @@ "phpunit/phpunit": "^9.4.2", "phpbench/phpbench": "1.0.0-alpha2", "vlucas/phpdotenv": "~2.4", - "symfony/serializer": "^3.4|^4.3", + "symfony/serializer": "^6.4|^7.0", "doctrine/annotations": "^1.11" }, "autoload": { diff --git a/integrations/Symfony/Serializer/AvroSerDeEncoder.php b/integrations/Symfony/Serializer/AvroSerDeEncoder.php index 99d8013..e6d1112 100644 --- a/integrations/Symfony/Serializer/AvroSerDeEncoder.php +++ b/integrations/Symfony/Serializer/AvroSerDeEncoder.php @@ -32,7 +32,7 @@ public function __construct(RecordSerializer $recordSerializer) * * @throws \FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException */ - public function decode($data, $format, array $context = []) + public function decode(string $data, string $format, array $context = []): mixed { $readersSchema = $context[self::CONTEXT_DECODE_READERS_SCHEMA] ?? null; Assert::that($readersSchema)->nullOr()->isInstanceOf(\AvroSchema::class); diff --git a/src/Objects/Schema/Generation/AttributeReader.php b/src/Objects/Schema/Generation/AttributeReader.php new file mode 100644 index 0000000..b549a28 --- /dev/null +++ b/src/Objects/Schema/Generation/AttributeReader.php @@ -0,0 +1,30 @@ +getAttributes(); + + return $this->getSchemaAttributes(...$attributes); + } + + public function readPropertyAttributes(\ReflectionProperty $property): SchemaAttributes + { + $attributes = $property->getAttributes(); + + return $this->getSchemaAttributes(...$attributes); + } + + private function getSchemaAttributes(\ReflectionAttribute ...$attributes): SchemaAttributes + { + $attributes = array_map(fn ($attr) => $attr->newInstance(), $attributes); + $attributes = array_filter($attributes, fn ($attr) => $attr instanceof SchemaAttribute); + + return new SchemaAttributes(...$attributes); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroAliases.php b/src/Objects/Schema/Generation/Attributes/AvroAliases.php new file mode 100644 index 0000000..b89f10a --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroAliases.php @@ -0,0 +1,39 @@ + + */ + public array $aliases; + + public function __construct( + string ...$aliases, + ) { + $this->aliases = $aliases; + } + + public function name(): string + { + return AttributeName::ALIASES; + } + + public function value(): array + { + return $this->aliases; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroDefault.php b/src/Objects/Schema/Generation/Attributes/AvroDefault.php new file mode 100644 index 0000000..ac6970c --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroDefault.php @@ -0,0 +1,33 @@ +value; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroDoc.php b/src/Objects/Schema/Generation/Attributes/AvroDoc.php new file mode 100644 index 0000000..477432c --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroDoc.php @@ -0,0 +1,33 @@ +value; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroItems.php b/src/Objects/Schema/Generation/Attributes/AvroItems.php new file mode 100644 index 0000000..f4fcf39 --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroItems.php @@ -0,0 +1,47 @@ +types = array_map(function ($type) { + if ($type instanceof AvroType) { + return $type; + } + + return new AvroType($type); + }, $types); + } + + /** + * @return AvroType[] + */ + public function value(): array + { + return $this->types; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(...$this->value()); + } + + public function name(): string + { + return AttributeName::ITEMS; + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroName.php b/src/Objects/Schema/Generation/Attributes/AvroName.php new file mode 100644 index 0000000..c9ccc94 --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroName.php @@ -0,0 +1,33 @@ +value; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroNamespace.php b/src/Objects/Schema/Generation/Attributes/AvroNamespace.php new file mode 100644 index 0000000..37f2000 --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroNamespace.php @@ -0,0 +1,33 @@ +value; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroOrder.php b/src/Objects/Schema/Generation/Attributes/AvroOrder.php new file mode 100644 index 0000000..7a7c6c5 --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroOrder.php @@ -0,0 +1,33 @@ +order->value; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroSize.php b/src/Objects/Schema/Generation/Attributes/AvroSize.php new file mode 100644 index 0000000..553919d --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroSize.php @@ -0,0 +1,33 @@ +size; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroSymbols.php b/src/Objects/Schema/Generation/Attributes/AvroSymbols.php new file mode 100644 index 0000000..2a6ce96 --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroSymbols.php @@ -0,0 +1,41 @@ + + */ + private array $symbols; + + public function __construct(callable $symbols) + { + $this->symbols = $symbols(); + } + + public function name(): string + { + return AttributeName::SYMBOLS; + } + + /** + * @return array + */ + public function value(): array + { + return $this->symbols; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroTargetClass.php b/src/Objects/Schema/Generation/Attributes/AvroTargetClass.php new file mode 100644 index 0000000..b5d9c1f --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroTargetClass.php @@ -0,0 +1,33 @@ +value; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroType.php b/src/Objects/Schema/Generation/Attributes/AvroType.php new file mode 100644 index 0000000..354923c --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroType.php @@ -0,0 +1,44 @@ + + */ + public array $attributes = []; + + public string $value; + + public function __construct( + Type|string $value, + SchemaAttribute ...$attributes + ) { + $this->value = \is_string($value) ? $value : $value->value; + + $this->attributes = $attributes; + } + + public function name(): string + { + return AttributeName::TYPE; + } + + public function value(): string + { + return $this->value; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(...$this->attributes); + } +} diff --git a/src/Objects/Schema/Generation/Attributes/AvroValues.php b/src/Objects/Schema/Generation/Attributes/AvroValues.php new file mode 100644 index 0000000..b69f319 --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/AvroValues.php @@ -0,0 +1,47 @@ +types = array_map(function ($type) { + if ($type instanceof AvroType) { + return $type; + } + + return new AvroType($type); + }, $types); + } + + /** + * @return AvroType[] + */ + public function value(): array + { + return $this->types; + } + + public function attributes(): SchemaAttributes + { + return new SchemaAttributes(...$this->value()); + } + + public function name(): string + { + return AttributeName::VALUES; + } +} diff --git a/src/Objects/Schema/Generation/Attributes/Order.php b/src/Objects/Schema/Generation/Attributes/Order.php new file mode 100644 index 0000000..32ad437 --- /dev/null +++ b/src/Objects/Schema/Generation/Attributes/Order.php @@ -0,0 +1,12 @@ + Date: Sun, 28 Jan 2024 09:03:06 +0100 Subject: [PATCH 2/2] chore: remove 7.4 --- .github/workflows/checks.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 2f00700..2219210 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -181,16 +181,16 @@ jobs: name: Download docker image uses: actions/download-artifact@v2 with: - name: php-avro-serde-7.4 + name: php-avro-serde-8.1 - name: Load docker image run: | - docker load -i php-avro-serde-7.4.tgz + docker load -i php-avro-serde-8.1.tgz - name: Install vendors run: | docker run -i --rm --net=host --sig-proxy=true --pid=host \ - -v "${GITHUB_WORKSPACE}":"${GITHUB_WORKSPACE}" -w "${GITHUB_WORKSPACE}" php-avro-serde:7.4 \ + -v "${GITHUB_WORKSPACE}":"${GITHUB_WORKSPACE}" -w "${GITHUB_WORKSPACE}" php-avro-serde:8.1 \ composer update --no-interaction --no-scripts --no-ansi --prefer-stable - name: Run PHPUnit Integration @@ -200,5 +200,5 @@ jobs: chmod a+x bin/wait-for-all.sh bin/wait-for-it.sh make platform docker run -i --rm --net=host --sig-proxy=true --pid=host \ - -v "${GITHUB_WORKSPACE}":"${GITHUB_WORKSPACE}" -w "${GITHUB_WORKSPACE}" php-avro-serde:7.4 \ + -v "${GITHUB_WORKSPACE}":"${GITHUB_WORKSPACE}" -w "${GITHUB_WORKSPACE}" php-avro-serde:8.1 \ vendor/bin/phpunit --group integration