Skip to content

Commit

Permalink
Allow referencing previously defined named types (#51)
Browse files Browse the repository at this point in the history
Once a named type is defined, in order to reuse that same type it is
needed to reference that type by the name (as in the primitive types).

Add support for NamedTypes in both the schema builder and the schema
generator, which is a way of referencing previously defined named types.

Add support for adding extra attributes for records in field's
definition.
  • Loading branch information
xico42 authored Nov 15, 2020
1 parent 89504da commit 322fcd6
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 69 deletions.
6 changes: 6 additions & 0 deletions src/Objects/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use FlixTech\AvroSerializer\Objects\Schema\LocalTimestampMillisType;
use FlixTech\AvroSerializer\Objects\Schema\LongType;
use FlixTech\AvroSerializer\Objects\Schema\MapType;
use FlixTech\AvroSerializer\Objects\Schema\NamedType;
use FlixTech\AvroSerializer\Objects\Schema\NullType;
use FlixTech\AvroSerializer\Objects\Schema\RecordType;
use FlixTech\AvroSerializer\Objects\Schema\StringType;
Expand Down Expand Up @@ -71,6 +72,11 @@ public static function string(): StringType
return new StringType();
}

public static function named(string $name): NamedType
{
return new NamedType($name);
}

public static function record(): RecordType
{
return new RecordType();
Expand Down
81 changes: 16 additions & 65 deletions src/Objects/Schema/Generation/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ class SchemaGenerator
*/
private $reader;

/**
* @var TypeMapper
*/
private $typeMapper;

public function __construct(SchemaAttributeReader $reader)
{
$this->reader = $reader;
$this->typeMapper = new TypeMapper($this);
}

/**
Expand All @@ -39,7 +45,7 @@ public function generate(string $className): Schema
*/
private function generateFromClass(ReflectionClass $class, Type $type): Schema
{
$schema = $this->schemaFromTypes($type);
$schema = $this->schemaFromType($type);

if (!$schema instanceof Schema\RecordType) {
return $schema;
Expand All @@ -56,75 +62,20 @@ private function generateFromClass(ReflectionClass $class, Type $type): Schema
private function schemaFromTypes(Type ...$types): Schema
{
if (\count($types) > 1) {
$unionSchemas = \array_map(function (Type $type) {
return $this->schemaFromTypes($type);
}, $types);
$unionSchemas = \array_map([$this, 'schemaFromType'], $types);

return Schema::union(...$unionSchemas);
}

$type = $types[0];
$attributes = $type->getAttributes();

switch ($type->getTypeName()) {
case TypeName::RECORD:
if ($attributes->has(AttributeName::TARGET_CLASS)) {
return $this->generate($attributes->get(AttributeName::TARGET_CLASS));
}
$schema = Schema::record();

return $this->applyAttributes($schema, $attributes);
case TypeName::NULL:
$schema = Schema::null();

return $this->applyAttributes($schema, $attributes);
case TypeName::BOOLEAN:
$schema = Schema::boolean();

return $this->applyAttributes($schema, $attributes);
case TypeName::INT:
$schema = Schema::int();

return $this->applyAttributes($schema, $attributes);
case TypeName::LONG:
$schema = Schema::long();

return $this->applyAttributes($schema, $attributes);
case TypeName::FLOAT:
$schema = Schema::float();

return $this->applyAttributes($schema, $attributes);
case TypeName::DOUBLE:
$schema = Schema::double();

return $this->applyAttributes($schema, $attributes);
case TypeName::BYTES:
$schema = Schema::bytes();

return $this->applyAttributes($schema, $attributes);
case TypeName::STRING:
$schema = Schema::string();

return $this->applyAttributes($schema, $attributes);
case TypeName::ARRAY:
$schema = Schema::array();

return $this->applyAttributes($schema, $attributes);
case TypeName::MAP:
$schema = Schema::map();

return $this->applyAttributes($schema, $attributes);
case TypeName::ENUM:
$schema = Schema::enum();

return $this->applyAttributes($schema, $attributes);
case TypeName::FIXED:
$schema = Schema::fixed();
return $this->schemaFromType($types[0]);
}

return $this->applyAttributes($schema, $attributes);
default:
throw new \InvalidArgumentException('$type is not a valid avro type');
}
private function schemaFromType(Type $type): Schema
{
return $this->applyAttributes(
$this->typeMapper->toSchema($type),
$type->getAttributes()
);
}

private function parseField(ReflectionProperty $property, Schema\RecordType $rootSchema): Schema
Expand Down
70 changes: 70 additions & 0 deletions src/Objects/Schema/Generation/TypeMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation;

use FlixTech\AvroSerializer\Objects\Schema;
use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\TypeName;

final class TypeMapper
{
/**
* @var array<string, callable>
*/
private $mappers;

public function __construct(SchemaGenerator $generator)
{
$this->mappers = [
TypeName::RECORD => $this->recordType($generator),
TypeName::NULL => $this->simpleType(Schema::null()),
TypeName::BOOLEAN => $this->simpleType(Schema::boolean()),
TypeName::INT => $this->simpleType(Schema::int()),
TypeName::LONG => $this->simpleType(Schema::long()),
TypeName::FLOAT => $this->simpleType(Schema::float()),
TypeName::DOUBLE => $this->simpleType(Schema::double()),
TypeName::BYTES => $this->simpleType(Schema::bytes()),
TypeName::STRING => $this->simpleType(Schema::string()),
TypeName::ARRAY => $this->simpleType(Schema::array()),
TypeName::MAP => $this->simpleType(Schema::map()),
TypeName::ENUM => $this->simpleType(Schema::enum()),
TypeName::FIXED => $this->simpleType(Schema::fixed()),
];
}

public function toSchema(Type $type): Schema
{
$mapper = $this->mappers[$type->getTypeName()] ?? $this->namedType();

return $mapper($type);
}

private function simpleType(Schema $schema): callable
{
return function () use ($schema): Schema {
return $schema;
};
}

private function recordType(SchemaGenerator $generator): callable
{
return function (Type $type) use ($generator): Schema {
$attributes = $type->getAttributes();

if ($attributes->has(AttributeName::TARGET_CLASS)) {
return $generator->generate($attributes->get(AttributeName::TARGET_CLASS));
}

return Schema::record();
};
}

private function namedType(): callable
{
return function (Type $type): Schema {
return Schema::named($type->getTypeName());
};
}
}
25 changes: 25 additions & 0 deletions src/Objects/Schema/NamedType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema;

use FlixTech\AvroSerializer\Objects\Schema;

class NamedType extends Schema
{
/**
* @var string
*/
private $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function serialize(): string
{
return $this->name;
}
}
12 changes: 10 additions & 2 deletions test/Objects/Schema/Generation/Fixture/RecordWithRecordType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@
class RecordWithRecordType
{
/**
* @SerDe\AvroName("simple")
* @SerDe\AvroName("simpleField")
* @SerDe\AvroType("record", attributes={
* @SerDe\AvroTargetClass("\FlixTech\AvroSerializer\Test\Objects\Schema\Generation\Fixture\SimpleRecord")
* @SerDe\AvroTargetClass("\FlixTech\AvroSerializer\Test\Objects\Schema\Generation\Fixture\SimpleRecord"),
* @SerDe\AvroDoc("This a simple record for testing purposes")
* })
*/
private $simpleRecord;

/**
* @SerDe\AvroName("unionField")
* @SerDe\AvroType("null")
* @SerDe\AvroType("org.acme.SimpleRecord")
*/
private $unionRecord;
}
16 changes: 14 additions & 2 deletions test/Objects/Schema/Generation/SchemaGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,23 @@ public function it_should_generate_records_containing_records()
$expected = Schema::record()
->name('RecordWithRecordType')
->field(
'simple',
'simpleField',
Schema::record()
->name('SimpleRecord')
->namespace('org.acme')
->field('intType', Schema::int(), Schema\Record\FieldOption::default(42))
->doc('This a simple record for testing purposes')
->field(
'intType',
Schema::int(),
Schema\Record\FieldOption::default(42)
),
)
->field(
'unionField',
Schema::union(
Schema::null(),
Schema::named('org.acme.SimpleRecord')
)
);

$this->assertEquals($expected, $schema);
Expand Down

0 comments on commit 322fcd6

Please sign in to comment.