-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow referencing previously defined named types
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.
- Loading branch information
Showing
6 changed files
with
141 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters