Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- Declaring a repeated field of a non-packable type (string, bytes, message) as `packed: true` now throws
`Exception\InvalidSchema` when the type is created. It used to produce output that no parser reads correctly.
15 changes: 15 additions & 0 deletions src/Exception/InvalidSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Exception;

use Thesis\Protobuf\ProtobufException;

/**
* A type or schema declaration that protobuf does not allow, e.g. a packed repeated field of strings.
* It is a configuration error, not a malformed message.
*
* @api
*/
final class InvalidSchema extends ProtobufException {}
121 changes: 121 additions & 0 deletions src/Reflection/Internal/Visitor/IsPackable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Reflection\Internal\Visitor;

use Thesis\Protobuf\Reflection\BoolT;
use Thesis\Protobuf\Reflection\DoubleT;
use Thesis\Protobuf\Reflection\EnumT;
use Thesis\Protobuf\Reflection\Fixed32T;
use Thesis\Protobuf\Reflection\Fixed64T;
use Thesis\Protobuf\Reflection\FloatT;
use Thesis\Protobuf\Reflection\Int32T;
use Thesis\Protobuf\Reflection\Int64T;
use Thesis\Protobuf\Reflection\SFixed32T;
use Thesis\Protobuf\Reflection\SFixed64T;
use Thesis\Protobuf\Reflection\SInt32T;
use Thesis\Protobuf\Reflection\SInt64T;
use Thesis\Protobuf\Reflection\Type;
use Thesis\Protobuf\Reflection\Uint32T;
use Thesis\Protobuf\Reflection\Uint64T;

/**
* Whether repeated elements of the type can be packed: only scalar numeric, bool and enum types can.
* Any other type, including one added later, is not packable unless listed here.
*
* @internal
* @template-extends DefaultTypeVisitor<bool>
*/
final class IsPackable extends DefaultTypeVisitor
{
#[\Override]
public function bool(BoolT $type): bool
{
return true;
}

#[\Override]
public function float(FloatT $type): bool
{
return true;
}

#[\Override]
public function double(DoubleT $type): bool
{
return true;
}

#[\Override]
public function int32(Int32T $type): bool
{
return true;
}

#[\Override]
public function uint32(Uint32T $type): bool
{
return true;
}

#[\Override]
public function sint32(SInt32T $type): bool
{
return true;
}

#[\Override]
public function int64(Int64T $type): bool
{
return true;
}

#[\Override]
public function uint64(Uint64T $type): bool
{
return true;
}

#[\Override]
public function sint64(SInt64T $type): bool
{
return true;
}

#[\Override]
public function fixed32(Fixed32T $type): bool
{
return true;
}

#[\Override]
public function sfixed32(SFixed32T $type): bool
{
return true;
}

#[\Override]
public function fixed64(Fixed64T $type): bool
{
return true;
}

#[\Override]
public function sfixed64(SFixed64T $type): bool
{
return true;
}

#[\Override]
public function enum(EnumT $type): bool
{
return true;
}

#[\Override]
protected function default(Type $type): bool
{
return false;
}
}
13 changes: 12 additions & 1 deletion src/Reflection/ListT.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Thesis\Protobuf\Reflection;

use Thesis\Protobuf\Exception\InvalidSchema;
use Thesis\Protobuf\Reflection\Internal\Visitor\IsPackable;

/**
* @api
* @template T
Expand All @@ -13,11 +16,19 @@
{
/**
* @param Type<T, 'repeatable', *, *> $element
* @throws InvalidSchema if a non-packable element type is declared packed
*/
public function __construct(
public Type $element,
public ?bool $packed = null,
) {}
) {
if ($packed === true && !$element->accept(new IsPackable())) {
throw new InvalidSchema(\sprintf(
'Repeated fields of type "%s" cannot be packed: only scalar numeric, bool and enum elements can.',
$element::class,
));
}
}

#[\Override]
public function accept(Visitor $visitor): mixed
Expand Down
13 changes: 12 additions & 1 deletion src/Type/ListT.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Thesis\Protobuf\Type;

use Thesis\Protobuf\Exception\InvalidSchema;
use Thesis\Protobuf\Type;
use Thesis\Protobuf\Type\Visitor\IsPackable;

/**
* @template T
Expand All @@ -14,11 +16,20 @@
{
/**
* @param Type<T, 'repeatable', *, *> $element
* @throws InvalidSchema if a non-packable element type is declared packed
*/
public function __construct(
public Type $element,
public ?bool $packed = null,
) {}
) {
// A recursive message is not resolved here: the type it refers to may still be under construction.
if ($packed === true && ($element instanceof RecursionT || !$element->accept(new IsPackable()))) {
throw new InvalidSchema(\sprintf(
'Repeated fields of type "%s" cannot be packed: only scalar numeric, bool and enum elements can.',
$element::class,
));
}
}

#[\Override]
public function accept(Visitor $visitor): mixed
Expand Down
120 changes: 120 additions & 0 deletions src/Type/Visitor/IsPackable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Type\Visitor;

use Thesis\Protobuf\Type;
use Thesis\Protobuf\Type\BoolT;
use Thesis\Protobuf\Type\DoubleT;
use Thesis\Protobuf\Type\EnumT;
use Thesis\Protobuf\Type\Fixed32T;
use Thesis\Protobuf\Type\Fixed64T;
use Thesis\Protobuf\Type\FloatT;
use Thesis\Protobuf\Type\Int32T;
use Thesis\Protobuf\Type\Int64T;
use Thesis\Protobuf\Type\SFixed32T;
use Thesis\Protobuf\Type\SFixed64T;
use Thesis\Protobuf\Type\SInt32T;
use Thesis\Protobuf\Type\SInt64T;
use Thesis\Protobuf\Type\Uint32T;
use Thesis\Protobuf\Type\Uint64T;

/**
* Whether repeated elements of the type can be packed: only scalar numeric, bool and enum types can.
* Any other type, including one added later, is not packable unless listed here.
*
* @template-extends DefaultTypeVisitor<bool>
*/
final class IsPackable extends DefaultTypeVisitor
{
#[\Override]
public function bool(BoolT $type): bool
{
return true;
}

#[\Override]
public function float(FloatT $type): bool
{
return true;
}

#[\Override]
public function double(DoubleT $type): bool
{
return true;
}

#[\Override]
public function int32(Int32T $type): bool
{
return true;
}

#[\Override]
public function uint32(Uint32T $type): bool
{
return true;
}

#[\Override]
public function sint32(SInt32T $type): bool
{
return true;
}

#[\Override]
public function int64(Int64T $type): bool
{
return true;
}

#[\Override]
public function uint64(Uint64T $type): bool
{
return true;
}

#[\Override]
public function sint64(SInt64T $type): bool
{
return true;
}

#[\Override]
public function fixed32(Fixed32T $type): bool
{
return true;
}

#[\Override]
public function sfixed32(SFixed32T $type): bool
{
return true;
}

#[\Override]
public function fixed64(Fixed64T $type): bool
{
return true;
}

#[\Override]
public function sfixed64(SFixed64T $type): bool
{
return true;
}

#[\Override]
public function enum(EnumT $type): bool
{
return true;
}

#[\Override]
protected function default(Type $type): bool
{
return false;
}
}
33 changes: 0 additions & 33 deletions src/Type/Visitor/IsPacked.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Type/Visitor/TypeDeserializerVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function list(ListT $type): DeserializeValue
/** @phpstan-ignore argument.type */
$type->element->accept($this),
$this->tag,
$this->tag->type === WireType::BYTES && $type->element->accept(new IsPacked()),
$this->tag->type === WireType::BYTES && $type->element->accept(new IsPackable()),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/Visitor/TypeSerializerVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function list(ListT $type): SerializeValue
->accept($this)
->without(SerializeTag::class),
$this->tag,
$type->packed ?? $type->element->accept(new IsPacked()),
$type->packed ?? $type->element->accept(new IsPackable()),
),
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public static function message(Message $message): self
* @param Type<E, 'repeatable', *, *> $type
* @param list<E> $values
* @return self<list<E>>
* @throws Exception\InvalidSchema if a non-packable element type is declared packed
*/
public static function listOf(
Type $type,
Expand Down
Loading
Loading