diff --git a/src/Plugin/Generator/ProtoGenerator.php b/src/Plugin/Generator/ProtoGenerator.php index 2c13f2d..90fe84a 100644 --- a/src/Plugin/Generator/ProtoGenerator.php +++ b/src/Plugin/Generator/ProtoGenerator.php @@ -375,7 +375,8 @@ private function generateMessage( $parameters[] = $field->options?->packed === true; } elseif ($this->edition !== null) { // In editions, PACKED is the default for eligible types unless explicitly set to EXPANDED. - $parameters[] = $features?->repeatedFieldEncoding === null || $features->repeatedFieldEncoding === FeatureSet\RepeatedFieldEncoding::PACKED; + // The feature resolves to PACKED for any field, but only packable types are packed. + $parameters[] = $type->packable && ($features?->repeatedFieldEncoding === null || $features->repeatedFieldEncoding === FeatureSet\RepeatedFieldEncoding::PACKED); } elseif ($field->options?->packed === false) { // In proto3, packable fields are packed by default, which is also what the runtime does // when packed is not given; only an explicit [packed = false] needs to be spelled out. diff --git a/src/Plugin/Generator/TypeDeclaration.php b/src/Plugin/Generator/TypeDeclaration.php index 56b9181..8c8a369 100644 --- a/src/Plugin/Generator/TypeDeclaration.php +++ b/src/Plugin/Generator/TypeDeclaration.php @@ -18,6 +18,11 @@ public function __construct( public bool $isMap = false, public ?string $docType = null, public mixed $default = null, + /** + * Whether repeated values of the type can be packed into one length-delimited record: + * only scalar numeric, bool and enum types can, as in protobuf and the runtime (ListT). + */ + public bool $packable = false, ) {} public function resolvedType(): string diff --git a/src/Plugin/Generator/TypeDeclarationFactory.php b/src/Plugin/Generator/TypeDeclarationFactory.php index a92a07c..dd07ff4 100644 --- a/src/Plugin/Generator/TypeDeclarationFactory.php +++ b/src/Plugin/Generator/TypeDeclarationFactory.php @@ -47,6 +47,7 @@ public function create(Parser\FieldDescriptor $field, RuntimeImports $runtime): nullable: !$isEnum, docType: $fieldType->fqcn, default: $default, + packable: $isEnum, ); } @@ -59,6 +60,7 @@ public function create(Parser\FieldDescriptor $field, RuntimeImports $runtime): Type::TYPE_DOUBLE => $runtime->reflection('DoubleT::T'), }), default: 0, + packable: true, ), Type::TYPE_UINT64, Type::TYPE_FIXED64 => new TypeDeclaration( @@ -68,6 +70,7 @@ public function create(Parser\FieldDescriptor $field, RuntimeImports $runtime): Type::TYPE_FIXED64 => $runtime->reflection('Fixed64T::T'), }), default: Literal::new('\BcMath\Number', [0]), + packable: true, ), Type::TYPE_INT32, Type::TYPE_UINT32, @@ -89,11 +92,13 @@ public function create(Parser\FieldDescriptor $field, RuntimeImports $runtime): Type::TYPE_SINT64 => $runtime->reflection('SInt64T::T'), }), default: 0, + packable: true, ), Type::TYPE_BOOL => new TypeDeclaration( phpType: 'bool', reflectionType: new Literal($runtime->reflection('BoolT::T')), default: false, + packable: true, ), Type::TYPE_STRING, Type::TYPE_BYTES => new TypeDeclaration( diff --git a/tests/GeneratedCode/samples/editions_packed.php b/tests/GeneratedCode/samples/editions_packed.php new file mode 100644 index 0000000..c0bf2a8 --- /dev/null +++ b/tests/GeneratedCode/samples/editions_packed.php @@ -0,0 +1,92 @@ + new Repeated( + int32Default: [1, -2, 3], + int32Expanded: [-6, 7], + enumDefault: [Color::COLOR_RED, Color::COLOR_GREEN], + enumExpanded: [Color::COLOR_RED, Color::COLOR_UNSPECIFIED], + boolDefault: [true, false], + boolExpanded: [true, true, false], + sint64Default: [-1, 1], + fixed32Default: [1, 4_294_967_295], + doubleDefault: [1.5, -0.25], + uint64Default: [new Number(1), new Number('18446744073709551615')], + uint64Expanded: [new Number(0), new Number(2)], + stringDefault: ['a', ''], + stringExpanded: ['b'], + bytesDefault: ["\x00\xff", ''], + bytesExpanded: ["\x01"], + messageDefault: [new Inner(1), new Inner()], + messageExpanded: [new Inner(-1)], + ), + 'all fields set, file default expanded' => new RepeatedExpanded( + int32Default: [1, -2, 3], + int32Packed: [4, 5], + enumDefault: [Color::COLOR_GREEN], + enumPacked: [Color::COLOR_RED, Color::COLOR_UNSPECIFIED], + doubleDefault: [0.5], + doublePacked: [1.5, -0.25], + stringDefault: ['a', ''], + bytesDefault: ["\x00\xff"], + messageDefault: [new Inner(1), new Inner()], + ), + 'wire encoding follows the repeated_field_encoding feature' => static function (): void { + $encoder = Encoder\Builder::buildDefault(); + + $expected = [ + // PACKED by default: one length-delimited record + 'int32Default' => [new Repeated(int32Default: [1, 2]), "\x0A\x02\x01\x02"], + 'enumDefault' => [new Repeated(enumDefault: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x1A\x02\x01\x02"], + 'boolDefault' => [new Repeated(boolDefault: [true, false]), "\x2A\x02\x01\x00"], + 'sint64Default' => [new Repeated(sint64Default: [-1, 1]), "\x3A\x02\x01\x02"], + 'fixed32Default' => [new Repeated(fixed32Default: [1, 2]), "\x42\x08\x01\x00\x00\x00\x02\x00\x00\x00"], + 'doubleDefault' => [new Repeated(doubleDefault: [1.5]), "\x4A\x08" . pack('e', 1.5)], + 'uint64Default' => [new Repeated(uint64Default: [new Number(1), new Number(2)]), "\x52\x02\x01\x02"], + // EXPANDED on the field: a record per element + 'int32Expanded' => [new Repeated(int32Expanded: [1, 2]), "\x10\x01\x10\x02"], + 'enumExpanded' => [new Repeated(enumExpanded: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x20\x01\x20\x02"], + 'boolExpanded' => [new Repeated(boolExpanded: [true, false]), "\x30\x01\x30\x00"], + 'uint64Expanded' => [new Repeated(uint64Expanded: [new Number(1), new Number(2)]), "\x58\x01\x58\x02"], + // non-packable types: a record per element whatever the feature resolves to + 'stringDefault' => [new Repeated(stringDefault: ['a', 'b']), "\x62\x01a\x62\x01b"], + 'stringExpanded' => [new Repeated(stringExpanded: ['a', 'b']), "\x6A\x01a\x6A\x01b"], + 'bytesDefault' => [new Repeated(bytesDefault: ["\x01"]), "\x72\x01\x01"], + 'bytesExpanded' => [new Repeated(bytesExpanded: ["\x01"]), "\x7A\x01\x01"], + 'messageDefault' => [new Repeated(messageDefault: [new Inner(1)]), "\x82\x01\x02\x08\x01"], + 'messageExpanded' => [new Repeated(messageExpanded: [new Inner(1)]), "\x8A\x01\x02\x08\x01"], + // file default EXPANDED: a record per element unless the field is set to PACKED + 'expanded int32Default' => [new RepeatedExpanded(int32Default: [1, 2]), "\x08\x01\x08\x02"], + 'expanded int32Packed' => [new RepeatedExpanded(int32Packed: [1, 2]), "\x12\x02\x01\x02"], + 'expanded enumDefault' => [new RepeatedExpanded(enumDefault: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x18\x01\x18\x02"], + 'expanded enumPacked' => [new RepeatedExpanded(enumPacked: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x22\x02\x01\x02"], + 'expanded doubleDefault' => [new RepeatedExpanded(doubleDefault: [1.5]), "\x29" . pack('e', 1.5)], + 'expanded doublePacked' => [new RepeatedExpanded(doublePacked: [1.5]), "\x32\x08" . pack('e', 1.5)], + 'expanded stringDefault' => [new RepeatedExpanded(stringDefault: ['a', 'b']), "\x3A\x01a\x3A\x01b"], + 'expanded bytesDefault' => [new RepeatedExpanded(bytesDefault: ["\x01"]), "\x42\x01\x01"], + 'expanded messageDefault' => [new RepeatedExpanded(messageDefault: [new Inner(1)]), "\x4A\x02\x08\x01"], + ]; + + foreach ($expected as $property => [$message, $bytes]) { + $actual = $encoder->encode($message); + + if ($actual !== $bytes) { + throw new LogicException(sprintf('%s: expected %s on the wire, got %s', $property, bin2hex($bytes), bin2hex($actual))); + } + } + }, +]; diff --git a/tests/fixtures/editions_packed/expanded.proto b/tests/fixtures/editions_packed/expanded.proto new file mode 100644 index 0000000..6d9433c --- /dev/null +++ b/tests/fixtures/editions_packed/expanded.proto @@ -0,0 +1,20 @@ +edition = "2023"; + +package editions.packed.v1; + +import "packed.proto"; + +option features.repeated_field_encoding = EXPANDED; + +// The file default is EXPANDED: packable fields are packed only when set to PACKED. +message RepeatedExpanded { + repeated int32 int32_default = 1; + repeated int32 int32_packed = 2 [features.repeated_field_encoding = PACKED]; + repeated Color enum_default = 3; + repeated Color enum_packed = 4 [features.repeated_field_encoding = PACKED]; + repeated double double_default = 5; + repeated double double_packed = 6 [features.repeated_field_encoding = PACKED]; + repeated string string_default = 7; + repeated bytes bytes_default = 8; + repeated Inner message_default = 9; +} diff --git a/tests/fixtures/editions_packed/packed.proto b/tests/fixtures/editions_packed/packed.proto new file mode 100644 index 0000000..95e682f --- /dev/null +++ b/tests/fixtures/editions_packed/packed.proto @@ -0,0 +1,35 @@ +edition = "2023"; + +package editions.packed.v1; + +enum Color { + COLOR_UNSPECIFIED = 0; + COLOR_RED = 1; + COLOR_GREEN = 2; +} + +message Inner { + int32 value = 1; +} + +// features.repeated_field_encoding is PACKED by default: packable fields are packed unless set to EXPANDED. +message Repeated { + repeated int32 int32_default = 1; + repeated int32 int32_expanded = 2 [features.repeated_field_encoding = EXPANDED]; + repeated Color enum_default = 3; + repeated Color enum_expanded = 4 [features.repeated_field_encoding = EXPANDED]; + repeated bool bool_default = 5; + repeated bool bool_expanded = 6 [features.repeated_field_encoding = EXPANDED]; + repeated sint64 sint64_default = 7; + repeated fixed32 fixed32_default = 8; + repeated double double_default = 9; + repeated uint64 uint64_default = 10; + repeated uint64 uint64_expanded = 11 [features.repeated_field_encoding = EXPANDED]; + // Non-packable types are never packed, although the feature resolves to PACKED for them too. + repeated string string_default = 12; + repeated string string_expanded = 13 [features.repeated_field_encoding = EXPANDED]; + repeated bytes bytes_default = 14; + repeated bytes bytes_expanded = 15 [features.repeated_field_encoding = EXPANDED]; + repeated Inner message_default = 16; + repeated Inner message_expanded = 17 [features.repeated_field_encoding = EXPANDED]; +} diff --git a/tests/snapshots/editions_packed/Editions/Packed/V1/Color.php b/tests/snapshots/editions_packed/Editions/Packed/V1/Color.php new file mode 100644 index 0000000..5a3d57c --- /dev/null +++ b/tests/snapshots/editions_packed/Editions/Packed/V1/Color.php @@ -0,0 +1,23 @@ +add(Registry\Descriptor::base64(self::EXPANDED_DESCRIPTOR_BUFFER), new File( + name: 'expanded.proto', + dependencies: [ + 'packed.proto', + ], + messages: [ + new File\MessageDescriptor('editions.packed.v1.RepeatedExpanded', \Editions\Packed\V1\RepeatedExpanded::class), + ], + )); + + $pool->add(Registry\Descriptor::base64(self::PACKED_DESCRIPTOR_BUFFER), new File( + name: 'packed.proto', + messages: [ + new File\MessageDescriptor('editions.packed.v1.Inner', \Editions\Packed\V1\Inner::class), + new File\MessageDescriptor('editions.packed.v1.Repeated', \Editions\Packed\V1\Repeated::class), + ], + enums: [ + new File\EnumDescriptor('editions.packed.v1.Color', \Editions\Packed\V1\Color::class), + ], + )); + } +} diff --git a/tests/snapshots/editions_packed/Editions/Packed/V1/Inner.php b/tests/snapshots/editions_packed/Editions/Packed/V1/Inner.php new file mode 100644 index 0000000..5722b4e --- /dev/null +++ b/tests/snapshots/editions_packed/Editions/Packed/V1/Inner.php @@ -0,0 +1,26 @@ + $int32Default + * @param list $int32Expanded + * @param list<\Editions\Packed\V1\Color> $enumDefault + * @param list<\Editions\Packed\V1\Color> $enumExpanded + * @param list $boolDefault + * @param list $boolExpanded + * @param list $sint64Default + * @param list $fixed32Default + * @param list $doubleDefault + * @param list<\BcMath\Number> $uint64Default + * @param list<\BcMath\Number> $uint64Expanded + * @param list $stringDefault Non-packable types are never packed, although the feature resolves to PACKED for them too. + * @param list $stringExpanded + * @param list $bytesDefault + * @param list $bytesExpanded + * @param list<\Editions\Packed\V1\Inner> $messageDefault + * @param list<\Editions\Packed\V1\Inner> $messageExpanded + */ + public function __construct( + #[Reflection\Field(1, new Reflection\ListT(Reflection\Int32T::T, true))] + public array $int32Default = [], + #[Reflection\Field(2, new Reflection\ListT(Reflection\Int32T::T, false))] + public array $int32Expanded = [], + #[Reflection\Field(3, new Reflection\ListT(new Reflection\EnumT(\Editions\Packed\V1\Color::class), true))] + public array $enumDefault = [], + #[Reflection\Field(4, new Reflection\ListT(new Reflection\EnumT(\Editions\Packed\V1\Color::class), false))] + public array $enumExpanded = [], + #[Reflection\Field(5, new Reflection\ListT(Reflection\BoolT::T, true))] + public array $boolDefault = [], + #[Reflection\Field(6, new Reflection\ListT(Reflection\BoolT::T, false))] + public array $boolExpanded = [], + #[Reflection\Field(7, new Reflection\ListT(Reflection\SInt64T::T, true))] + public array $sint64Default = [], + #[Reflection\Field(8, new Reflection\ListT(Reflection\Fixed32T::T, true))] + public array $fixed32Default = [], + #[Reflection\Field(9, new Reflection\ListT(Reflection\DoubleT::T, true))] + public array $doubleDefault = [], + #[Reflection\Field(10, new Reflection\ListT(Reflection\Uint64T::T, true))] + public array $uint64Default = [], + #[Reflection\Field(11, new Reflection\ListT(Reflection\Uint64T::T, false))] + public array $uint64Expanded = [], + #[Reflection\Field(12, new Reflection\ListT(Reflection\StringT::T, false))] + public array $stringDefault = [], + #[Reflection\Field(13, new Reflection\ListT(Reflection\StringT::T, false))] + public array $stringExpanded = [], + #[Reflection\Field(14, new Reflection\ListT(Reflection\BytesT::T, false))] + public array $bytesDefault = [], + #[Reflection\Field(15, new Reflection\ListT(Reflection\BytesT::T, false))] + public array $bytesExpanded = [], + #[Reflection\Field(16, new Reflection\ListT(new Reflection\ObjectT(\Editions\Packed\V1\Inner::class), false))] + public array $messageDefault = [], + #[Reflection\Field(17, new Reflection\ListT(new Reflection\ObjectT(\Editions\Packed\V1\Inner::class), false))] + public array $messageExpanded = [], + ) {} +} diff --git a/tests/snapshots/editions_packed/Editions/Packed/V1/RepeatedExpanded.php b/tests/snapshots/editions_packed/Editions/Packed/V1/RepeatedExpanded.php new file mode 100644 index 0000000..5c95948 --- /dev/null +++ b/tests/snapshots/editions_packed/Editions/Packed/V1/RepeatedExpanded.php @@ -0,0 +1,55 @@ + $int32Default + * @param list $int32Packed + * @param list<\Editions\Packed\V1\Color> $enumDefault + * @param list<\Editions\Packed\V1\Color> $enumPacked + * @param list $doubleDefault + * @param list $doublePacked + * @param list $stringDefault + * @param list $bytesDefault + * @param list<\Editions\Packed\V1\Inner> $messageDefault + */ + public function __construct( + #[Reflection\Field(1, new Reflection\ListT(Reflection\Int32T::T, false))] + public array $int32Default = [], + #[Reflection\Field(2, new Reflection\ListT(Reflection\Int32T::T, true))] + public array $int32Packed = [], + #[Reflection\Field(3, new Reflection\ListT(new Reflection\EnumT(\Editions\Packed\V1\Color::class), false))] + public array $enumDefault = [], + #[Reflection\Field(4, new Reflection\ListT(new Reflection\EnumT(\Editions\Packed\V1\Color::class), true))] + public array $enumPacked = [], + #[Reflection\Field(5, new Reflection\ListT(Reflection\DoubleT::T, false))] + public array $doubleDefault = [], + #[Reflection\Field(6, new Reflection\ListT(Reflection\DoubleT::T, true))] + public array $doublePacked = [], + #[Reflection\Field(7, new Reflection\ListT(Reflection\StringT::T, false))] + public array $stringDefault = [], + #[Reflection\Field(8, new Reflection\ListT(Reflection\BytesT::T, false))] + public array $bytesDefault = [], + #[Reflection\Field(9, new Reflection\ListT(new Reflection\ObjectT(\Editions\Packed\V1\Inner::class), false))] + public array $messageDefault = [], + ) {} +} diff --git a/tests/snapshots/editions_packed/Editions/Packed/V1/autoload.metadata.php b/tests/snapshots/editions_packed/Editions/Packed/V1/autoload.metadata.php new file mode 100644 index 0000000..eb9523d --- /dev/null +++ b/tests/snapshots/editions_packed/Editions/Packed/V1/autoload.metadata.php @@ -0,0 +1,14 @@ +register( + new \Thesis\Protobuf\Registry\OnceRegistrar(new \Editions\Packed\V1\DescriptorRegistry()), +);