Skip to content

Commit

Permalink
add logical types and attributes support
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiabasone committed Jan 29, 2025
1 parent 9e437c9 commit 5c3dedc
Show file tree
Hide file tree
Showing 20 changed files with 632 additions and 11 deletions.
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/AttributeReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation;

class AttributeReader implements SchemaAttributeReader
{
public function readClassAttributes(\ReflectionClass $class): SchemaAttributes
{
$attributes = $class->getAttributes();

return $this->getSchemaAttributes(...$attributes);
}

public function readPropertyAttributes(\ReflectionProperty $property): SchemaAttributes
{
$attributes = $property->getAttributes();

return $this->getSchemaAttributes(...$attributes);
}

/**
* @param \ReflectionAttribute<object> ...$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);
}
}
39 changes: 39 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;
use FlixTech\AvroSerializer\Objects\Schema\Generation\VariadicAttribute;

#[\Attribute]
final class AvroAliases implements VariadicAttribute
{
/**
* @var array<string>
*/
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();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
final class AvroDefault implements SchemaAttribute
{
public function __construct(
public mixed $value,
) {
}

public function name(): string
{
return AttributeName::DEFAULT;
}

public function value(): mixed
{
return $this->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroDoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
final class AvroDoc implements SchemaAttribute
{
public function __construct(
public string $value,
) {
}

public function name(): string
{
return AttributeName::DOC;
}

public function value(): string
{
return $this->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
47 changes: 47 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;
use FlixTech\AvroSerializer\Objects\Schema\Generation\TypeOnlyAttribute;

#[\Attribute]
final class AvroItems implements TypeOnlyAttribute
{
/**
* @var AvroType[]
*/
private array $types;

public function __construct(Type|AvroType ...$types)
{
$this->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;
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
class AvroName implements SchemaAttribute
{
public function __construct(
public string $value,
) {
}

public function name(): string
{
return AttributeName::NAME;
}

public function value(): string
{
return $this->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
class AvroNamespace implements SchemaAttribute
{
public function __construct(
public string $value,
) {
}

public function name(): string
{
return AttributeName::NAMESPACE;
}

public function value(): string
{
return $this->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
final class AvroOrder implements SchemaAttribute
{
public function __construct(
private Order $order,
) {
}

public function name(): string
{
return AttributeName::ORDER;
}

public function value(): string
{
return $this->order->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
final class AvroSize implements SchemaAttribute
{
public function __construct(
public int $size,
) {
}

public function name(): string
{
return AttributeName::SIZE;
}

public function value(): int
{
return $this->size;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
41 changes: 41 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroSymbols.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;
use FlixTech\AvroSerializer\Objects\Schema\Generation\VariadicAttribute;

#[\Attribute]
final class AvroSymbols implements VariadicAttribute
{
/**
* @var array<string>
*/
private array $symbols;

public function __construct(callable $symbols)
{
$this->symbols = $symbols();
}

public function name(): string
{
return AttributeName::SYMBOLS;
}

/**
* @return array<string>
*/
public function value(): array
{
return $this->symbols;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
Loading

0 comments on commit 5c3dedc

Please sign in to comment.