Skip to content

Commit

Permalink
Implement defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
kafkiansky committed Aug 30, 2024
1 parent 22a8488 commit 2503674
Show file tree
Hide file tree
Showing 25 changed files with 24 additions and 93 deletions.
51 changes: 0 additions & 51 deletions Internal/Label/LabelDefault.php

This file was deleted.

1 change: 0 additions & 1 deletion Internal/Label/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
*/
enum Labels
{
public const default = LabelDefault::key;
public const wireType = LabelWireType::key;
public const schemaType = LabelSchemaType::key;
public const packed = LabelPacked::key;
Expand Down
1 change: 0 additions & 1 deletion Internal/Reflection/ArrayPropertyMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public function labels(): TypedMap
}

return $labels
->with(Labels::default, [])
->with(Labels::isEmpty, static fn (array $values): bool => [] === $values)
->with(Labels::serializeTag, false)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Reflection/ArrayShapePropertyMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public function matchValue(mixed $value): bool
public function labels(): TypedMap
{
return Labels::new(Wire\Type::BYTES)
->with(Labels::default, [])
->with(Labels::isEmpty, static fn (array $values): bool => [] === $values)
->with(Labels::serializeTag, false)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Reflection/ConstantEnumPropertyMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function matchValue(mixed $value): bool
public function labels(): TypedMap
{
return $this->type->labels()
->with(Labels::default, 0)
->with(Labels::isEmpty, static fn (int $variant): bool => 0 === $variant)
;
}
Expand Down
2 changes: 0 additions & 2 deletions Internal/Reflection/EnumPropertyMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

use Prototype\Byte;
use Prototype\Serializer\Exception\EnumDoesNotContainVariant;
use Prototype\Serializer\Exception\EnumDoesNotContainZeroVariant;
use Prototype\Serializer\Internal\Label\Labels;
use Prototype\Serializer\Internal\Type\TypeSerializer;
use Prototype\Serializer\Internal\Type\VarintType;
Expand Down Expand Up @@ -89,7 +88,6 @@ public function matchValue(mixed $value): bool
public function labels(): TypedMap
{
return $this->type->labels()
->with(Labels::default, $this->enumName::tryFrom(0) ?: throw new EnumDoesNotContainZeroVariant($this->enumName))
->with(Labels::isEmpty, static fn (\BackedEnum $enum): bool => 0 === $enum->value)
;
}
Expand Down
1 change: 0 additions & 1 deletion Internal/Reflection/HashTablePropertyMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public function matchValue(mixed $value): bool
public function labels(): TypedMap
{
return Labels::new(Wire\Type::BYTES)
->with(Labels::default, [])
->with(Labels::isEmpty, static fn (array $values): bool => [] === $values)
->with(Labels::serializeTag, false)
;
Expand Down
17 changes: 4 additions & 13 deletions Internal/Reflection/PropertyDeserializeDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
namespace Prototype\Serializer\Internal\Reflection;

use Prototype\Byte;
use Prototype\Serializer\Internal\Label\Labels;
use Prototype\Serializer\Internal\Wire\Tag;
use Prototype\Serializer\PrototypeException;

Expand All @@ -41,22 +40,14 @@ final class PropertyDeserializeDescriptor
{
/**
* @param PropertyMarshaller<T> $marshaller
* @param T $default
*/
public function __construct(
private readonly \ReflectionProperty $property,
private readonly PropertyMarshaller $marshaller,
private readonly mixed $default = null,
) {}

/**
* @return T
* @throws PrototypeException
*/
public function default(): mixed
{
/** @var T */
return $this->marshaller->labels()[Labels::default];
}

/**
* @return T
* @throws \ReflectionException
Expand All @@ -80,9 +71,9 @@ public function setValue(object $object, mixed $value): void
* @template TClass of object
* @param TClass $object
*/
public function setDefault(object $object, mixed $value): void
public function setDefault(object $object): void
{
$this->setValue($object, $this->property->getType()?->allowsNull() ? null : $value);
$this->setValue($object, $this->property->getType()?->allowsNull() && null === $this->default ? null : $this->default);
}

/**
Expand Down
16 changes: 14 additions & 2 deletions Internal/Reflection/ProtobufReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Typhoon\DeclarationId\NamedClassId;
use Typhoon\Reflection\AttributeReflection;
use Typhoon\Reflection\ClassReflection;
use Typhoon\Reflection\MethodReflection;
use Typhoon\Reflection\PropertyReflection;
use Typhoon\Reflection\TyphoonReflector;

Expand Down Expand Up @@ -66,17 +67,18 @@ public function propertySerializers(ClassReflection $class, TyphoonReflector $re
*/
public function propertyDeserializers(ClassReflection $class, TyphoonReflector $reflector): array
{
return self::properties($class, $reflector, static fn (\ReflectionProperty $property, PropertyMarshaller $marshaller): PropertyDeserializeDescriptor => new PropertyDeserializeDescriptor(
return self::properties($class, $reflector, static fn (\ReflectionProperty $property, PropertyMarshaller $marshaller, mixed $default = null): PropertyDeserializeDescriptor => new PropertyDeserializeDescriptor(
$property,
$marshaller,
$default,
));
}

/**
* @template T of object
* @template E
* @param ClassReflection<T, NamedClassId<class-string<T>>|AnonymousClassId<class-string<T>>> $class
* @param callable(\ReflectionProperty, PropertyMarshaller): E $toPropertyDescriptor
* @param callable(\ReflectionProperty, PropertyMarshaller, mixed=): E $toPropertyDescriptor
* @psalm-return array<positive-int, E>
* @throws PrototypeException
*/
Expand All @@ -89,6 +91,15 @@ private static function properties(ClassReflection $class, TyphoonReflector $ref
->filter(static fn (PropertyReflection $property): bool => $property->isPublic() && !$property->isStatic())
;

$constructorProperties = $class
->methods()
->filter(static fn (MethodReflection $reflection): bool => $reflection->id->name === '__construct')
->first()
?->parameters()
->toArray() ?? []
;

/** @var PropertyReflection $property */
foreach ($classProperties as $property) {
/** @var ?Field $field */
$field = $property
Expand Down Expand Up @@ -138,6 +149,7 @@ private static function properties(ClassReflection $class, TyphoonReflector $ref
$properties[$n] = $toPropertyDescriptor(
$property->toNativeReflection(),
$propertyMarshaller,
($constructorProperties[$property->id->name] ?? null)?->defaultValue() ?? null,
);
}
}
Expand Down
1 change: 0 additions & 1 deletion Internal/Reflection/StructPropertyMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public function matchValue(mixed $value): bool
public function labels(): TypedMap
{
return Labels::new(Wire\Type::BYTES)
->with(Labels::default, [])
->with(Labels::isEmpty, static fn (array $values): bool => [] === $values)
->with(Labels::serializeTag, false)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/BoolType.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function writeTo(Byte\Writer $writer, mixed $value): void
public function labels(): TypedMap
{
return Labels::new(Wire\Type::VARINT)
->with(Labels::default, false)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::bool)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/DoubleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function readFrom(Byte\Reader $reader): float
public function labels(): TypedMap
{
return Labels::new(Wire\Type::FIXED64)
->with(Labels::default, 0.0)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::double)
;
Expand Down
4 changes: 2 additions & 2 deletions Internal/Type/DurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ final class DurationType
* @param int32 $nanos
*/
private function __construct(
public readonly int $seconds,
public readonly int $nanos,
public readonly int $seconds = 0,
public readonly int $nanos = 0,
) {}

public static function fromDateInterval(\DateInterval $interval): self
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/Fixed32Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function readFrom(Byte\Reader $reader): int
public function labels(): TypedMap
{
return Labels::new(Wire\Type::FIXED32)
->with(Labels::default, 0)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::fixed32)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/Fixed64Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function readFrom(Byte\Reader $reader): int
public function labels(): TypedMap
{
return Labels::new(Wire\Type::FIXED64)
->with(Labels::default, 0)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::fixed64)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function readFrom(Byte\Reader $reader): float
public function labels(): TypedMap
{
return Labels::new(Wire\Type::FIXED32)
->with(Labels::default, 0.0)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::float)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/SFixed32Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function readFrom(Byte\Reader $reader): int
public function labels(): TypedMap
{
return Labels::new(Wire\Type::FIXED32)
->with(Labels::default, 0)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::sfixed32)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/SFixed64Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function readFrom(Byte\Reader $reader): int
public function labels(): TypedMap
{
return Labels::new(Wire\Type::FIXED64)
->with(Labels::default, 0)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::sfixed64)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/SInt32Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function readFrom(Byte\Reader $reader): int
public function labels(): TypedMap
{
return Labels::new(Wire\Type::VARINT)
->with(Labels::default, 0)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::sint32)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/SInt64Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function readFrom(Byte\Reader $reader): int
public function labels(): TypedMap
{
return Labels::new(Wire\Type::VARINT)
->with(Labels::default, 0)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::sint64)
;
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function readFrom(Byte\Reader $reader): string
public function labels(): TypedMap
{
return Labels::new(Wire\Type::BYTES)
->with(Labels::default, '')
->with(Labels::schemaType, ProtobufType::string)
;
}
Expand Down
4 changes: 2 additions & 2 deletions Internal/Type/TimestampType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ final class TimestampType
* @param int32 $nanos
*/
private function __construct(
public readonly int $seconds,
public readonly int $nanos,
public readonly int $seconds = 0,
public readonly int $nanos = 0,
) {}

public static function fromDateTime(\DateTimeInterface $dateTime): self
Expand Down
4 changes: 1 addition & 3 deletions Internal/Type/ValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ public function writeTo(Byte\Writer $writer, mixed $value): void

public function labels(): TypedMap
{
return Labels::new(Type::BYTES)
->with(Labels::default, [])
;
return Labels::new(Type::BYTES);
}

/**
Expand Down
1 change: 0 additions & 1 deletion Internal/Type/VarintType.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function writeTo(Byte\Writer $writer, mixed $value): void
public function labels(): TypedMap
{
return Labels::new(Wire\Type::VARINT)
->with(Labels::default, 0)
->with(Labels::packed, true)
->with(Labels::schemaType, ProtobufType::int64)
;
Expand Down
2 changes: 1 addition & 1 deletion Internal/Wire/ProtobufMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function deserialize(string $messageType, Byte\Reader $reader): object
// so that we can distinguish between a real value and no value.
foreach ($properties as $propertyDeserializer) {
if (!$propertyDeserializer->isInitialized($object)) {
$propertyDeserializer->setDefault($object, $propertyDeserializer->default());
$propertyDeserializer->setDefault($object);
}
}

Expand Down

0 comments on commit 2503674

Please sign in to comment.