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
4 changes: 4 additions & 0 deletions src/Plugin/Generator/ProtoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ private function generateMessage(
} 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;
} 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.
$parameters[] = false;
}

$reflectionType = Literal::new($runtime->reflection('ListT'), $parameters);
Expand Down
70 changes: 70 additions & 0 deletions tests/GeneratedCode/samples/proto3_packed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

/**
* Samples of the proto3_packed snapshot case, keyed by label (see GeneratedCodeTest): a populated message and a check
* of the wire encoding every repeated field is written in.
*/

use BcMath\Number;
use Proto3\Packed\V1\Color;
use Proto3\Packed\V1\Inner;
use Proto3\Packed\V1\Repeated;
use Thesis\Protobuf\Encoder;

return [
'all fields set' => new Repeated(
int32Default: [1, -2, 3],
int32Packed: [4, 5],
int32Unpacked: [-6, 7],
enumDefault: [Color::COLOR_RED, Color::COLOR_GREEN],
enumPacked: [Color::COLOR_GREEN],
enumUnpacked: [Color::COLOR_RED, Color::COLOR_UNSPECIFIED],
boolDefault: [true, false],
boolPacked: [false, true],
boolUnpacked: [true, true, false],
sint64Unpacked: [-1, 1],
fixed32Unpacked: [1, 4_294_967_295],
doubleUnpacked: [1.5, -0.25],
uint64Unpacked: [new Number(1), new Number('18446744073709551615')],
stringDefault: ['a', ''],
stringUnpacked: ['b'],
bytesUnpacked: ["\x00\xff"],
messageUnpacked: [new Inner(1), new Inner()],
),
'wire encoding follows the packed option' => static function (): void {
$encoder = Encoder\Builder::buildDefault();

$expected = [
// packed by default and with [packed = true]: one length-delimited record
'int32Default' => [new Repeated(int32Default: [1, 2]), "\x0A\x02\x01\x02"],
'int32Packed' => [new Repeated(int32Packed: [1, 2]), "\x12\x02\x01\x02"],
'enumDefault' => [new Repeated(enumDefault: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x22\x02\x01\x02"],
'enumPacked' => [new Repeated(enumPacked: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x2A\x02\x01\x02"],
'boolDefault' => [new Repeated(boolDefault: [true, false]), "\x3A\x02\x01\x00"],
'boolPacked' => [new Repeated(boolPacked: [true, false]), "\x42\x02\x01\x00"],
// [packed = false]: a record per element
'int32Unpacked' => [new Repeated(int32Unpacked: [1, 2]), "\x18\x01\x18\x02"],
'enumUnpacked' => [new Repeated(enumUnpacked: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x30\x01\x30\x02"],
'boolUnpacked' => [new Repeated(boolUnpacked: [true, false]), "\x48\x01\x48\x00"],
'sint64Unpacked' => [new Repeated(sint64Unpacked: [-1, 1]), "\x50\x01\x50\x02"],
'fixed32Unpacked' => [new Repeated(fixed32Unpacked: [1, 2]), "\x5D\x01\x00\x00\x00\x5D\x02\x00\x00\x00"],
'doubleUnpacked' => [new Repeated(doubleUnpacked: [1.5]), "\x61" . pack('e', 1.5)],
'uint64Unpacked' => [new Repeated(uint64Unpacked: [new Number(1), new Number(2)]), "\x68\x01\x68\x02"],
// non-packable types: a record per element regardless of the option
'stringDefault' => [new Repeated(stringDefault: ['a', 'b']), "\x72\x01a\x72\x01b"],
'stringUnpacked' => [new Repeated(stringUnpacked: ['a', 'b']), "\x7A\x01a\x7A\x01b"],
'bytesUnpacked' => [new Repeated(bytesUnpacked: ["\x01"]), "\x82\x01\x01\x01"],
'messageUnpacked' => [new Repeated(messageUnpacked: [new Inner(1)]), "\x8A\x01\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)));
}
}
},
];
35 changes: 35 additions & 0 deletions tests/fixtures/proto3_packed/proto3_packed.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
syntax = "proto3";

package proto3.packed.v1;

enum Color {
COLOR_UNSPECIFIED = 0;
COLOR_RED = 1;
COLOR_GREEN = 2;
}

message Inner {
int32 value = 1;
}

// Repeated packable fields are packed by default in proto3; [packed = false] makes them expanded.
message Repeated {
repeated int32 int32_default = 1;
repeated int32 int32_packed = 2 [packed = true];
repeated int32 int32_unpacked = 3 [packed = false];
repeated Color enum_default = 4;
repeated Color enum_packed = 5 [packed = true];
repeated Color enum_unpacked = 6 [packed = false];
repeated bool bool_default = 7;
repeated bool bool_packed = 8 [packed = true];
repeated bool bool_unpacked = 9 [packed = false];
repeated sint64 sint64_unpacked = 10 [packed = false];
repeated fixed32 fixed32_unpacked = 11 [packed = false];
repeated double double_unpacked = 12 [packed = false];
repeated uint64 uint64_unpacked = 13 [packed = false];
// Non-packable types are never packed: [packed = false] changes nothing for them.
repeated string string_default = 14;
repeated string string_unpacked = 15 [packed = false];
repeated bytes bytes_unpacked = 16 [packed = false];
repeated Inner message_unpacked = 17 [packed = false];
}
23 changes: 23 additions & 0 deletions tests/snapshots/proto3_packed/Proto3/Packed/V1/Color.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.6.x-dev
* protoc — v6.32.1
* Source: proto3_packed.proto
*/

declare(strict_types=1);

namespace Proto3\Packed\V1;

/**
* @api
*/
enum Color: int
{
case COLOR_UNSPECIFIED = 0;
case COLOR_RED = 1;
case COLOR_GREEN = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.6.x-dev
* protoc — v6.32.1
* Source: proto3_packed.proto
*/

declare(strict_types=1);

namespace Proto3\Packed\V1;

use Override;
use Thesis\Protobuf\Registry;
use Thesis\Protobuf\Registry\File;

/**
* @api
*/
final readonly class DescriptorRegistry implements Registry\Registrar
{
private const string PROTO3_PACKED_DESCRIPTOR_BUFFER = 'ChNwcm90bzNfcGFja2VkLnByb3RvEhBwcm90bzMucGFja2VkLnYxIh0KBUlubmVyEhQKBXZhbHVlGAEgASgFUgV2YWx1ZSKrBgoIUmVwZWF0ZWQSIwoNaW50MzJfZGVmYXVsdBgBIAMoBVIMaW50MzJEZWZhdWx0EiUKDGludDMyX3BhY2tlZBgCIAMoBVILaW50MzJQYWNrZWRCAhABEikKDmludDMyX3VucGFja2VkGAMgAygFUg1pbnQzMlVucGFja2VkQgIQABI6CgxlbnVtX2RlZmF1bHQYBCADKA4yFy5wcm90bzMucGFja2VkLnYxLkNvbG9yUgtlbnVtRGVmYXVsdBI8CgtlbnVtX3BhY2tlZBgFIAMoDjIXLnByb3RvMy5wYWNrZWQudjEuQ29sb3JSCmVudW1QYWNrZWRCAhABEkAKDWVudW1fdW5wYWNrZWQYBiADKA4yFy5wcm90bzMucGFja2VkLnYxLkNvbG9yUgxlbnVtVW5wYWNrZWRCAhAAEiEKDGJvb2xfZGVmYXVsdBgHIAMoCFILYm9vbERlZmF1bHQSIwoLYm9vbF9wYWNrZWQYCCADKAhSCmJvb2xQYWNrZWRCAhABEicKDWJvb2xfdW5wYWNrZWQYCSADKAhSDGJvb2xVbnBhY2tlZEICEAASKwoPc2ludDY0X3VucGFja2VkGAogAygSUg5zaW50NjRVbnBhY2tlZEICEAASLQoQZml4ZWQzMl91bnBhY2tlZBgLIAMoB1IPZml4ZWQzMlVucGFja2VkQgIQABIrCg9kb3VibGVfdW5wYWNrZWQYDCADKAFSDmRvdWJsZVVucGFja2VkQgIQABIrCg91aW50NjRfdW5wYWNrZWQYDSADKARSDnVpb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SUKDAoFBAECCgMSAxooKgoMCgUEAQIKCBIDGis7Cg0KBgQBAgoIAhIDGiw6CgsKBAQBAgsSAxsEOgoMCgUEAQILBBIDGwQMCgwKBQQBAgsFEgMbDRMKDAoFBAECCwESAxsUIwoMCgUEAQILAxIDGyYoCgwKBQQBAgsIEgMbKTkKDQoGBAECCwgCEgMbKjgKCwoEBAECDBIDHAQ6CgwKBQQBAgwEEgMcBAwKDAoFBAECDAUSAxwNEwoMCgUEAQIMARIDHBQjCgwKBQQBAgwDEgMcJigKDAoFBAECDAgSAxwpOQoNCgYEAQIMCAISAxwqOApeCgQEAQINEgMeBCgaUSBOb24tcGFja2FibGUgdHlwZXMgYXJlIG5ldmVyIHBhY2tlZDogW3BhY2tlZCA9IGZhbHNlXSBjaGFuZ2VzIG5vdGhpbmcgZm9yIHRoZW0uCgoMCgUEAQINBBIDHgQMCgwKBQQBAg0FEgMeDRMKDAoFBAECDQESAx4UIgoMCgUEAQINAxIDHiUnCgsKBAQBAg4SAx8EOgoMCgUEAQIOBBIDHwQMCgwKBQQBAg4FEgMfDRMKDAoFBAECDgESAx8UIwoMCgUEAQIOAxIDHyYoCgwKBQQBAg4IEgMfKTkKDQoGBAECDggCEgMfKjgKCwoEBAECDxIDIAQ4CgwKBQQBAg8EEgMgBAwKDAoFBAECDwUSAyANEgoMCgUEAQIPARIDIBMhCgwKBQQBAg8DEgMgJCYKDAoFBAECDwgSAyAnNwoNCgYEAQIPCAISAyAoNgoLCgQEAQIQEgMhBDoKDAoFBAECEAQSAyEEDAoMCgUEAQIQBhIDIQ0SCgwKBQQBAhABEgMhEyMKDAoFBAECEAMSAyEmKAoMCgUEAQIQCBIDISk5Cg0KBgQBAhAIAhIDISo4YgZwcm90bzM=';

#[Override]
public function register(Registry\Pool $pool): void
{
$pool->add(Registry\Descriptor::base64(self::PROTO3_PACKED_DESCRIPTOR_BUFFER), new File(
name: 'proto3_packed.proto',
messages: [
new File\MessageDescriptor('proto3.packed.v1.Inner', \Proto3\Packed\V1\Inner::class),
new File\MessageDescriptor('proto3.packed.v1.Repeated', \Proto3\Packed\V1\Repeated::class),
],
enums: [
new File\EnumDescriptor('proto3.packed.v1.Color', \Proto3\Packed\V1\Color::class),
],
));
}
}
26 changes: 26 additions & 0 deletions tests/snapshots/proto3_packed/Proto3/Packed/V1/Inner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.6.x-dev
* protoc — v6.32.1
* Source: proto3_packed.proto
*/

declare(strict_types=1);

namespace Proto3\Packed\V1;

use Thesis\Protobuf\Reflection;

/**
* @api
*/
final readonly class Inner
{
public function __construct(
#[Reflection\Field(1, Reflection\Int32T::T)]
public int $value = 0,
) {}
}
79 changes: 79 additions & 0 deletions tests/snapshots/proto3_packed/Proto3/Packed/V1/Repeated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.6.x-dev
* protoc — v6.32.1
* Source: proto3_packed.proto
*/

declare(strict_types=1);

namespace Proto3\Packed\V1;

use Thesis\Protobuf\Reflection;

/**
* @api
*
* Repeated packable fields are packed by default in proto3; [packed = false] makes them expanded.
*/
final readonly class Repeated
{
/**
* @param list<int> $int32Default
* @param list<int> $int32Packed
* @param list<int> $int32Unpacked
* @param list<\Proto3\Packed\V1\Color> $enumDefault
* @param list<\Proto3\Packed\V1\Color> $enumPacked
* @param list<\Proto3\Packed\V1\Color> $enumUnpacked
* @param list<bool> $boolDefault
* @param list<bool> $boolPacked
* @param list<bool> $boolUnpacked
* @param list<int> $sint64Unpacked
* @param list<int> $fixed32Unpacked
* @param list<float> $doubleUnpacked
* @param list<\BcMath\Number> $uint64Unpacked
* @param list<string> $stringDefault Non-packable types are never packed: [packed = false] changes nothing for them.
* @param list<string> $stringUnpacked
* @param list<string> $bytesUnpacked
* @param list<\Proto3\Packed\V1\Inner> $messageUnpacked
*/
public function __construct(
#[Reflection\Field(1, new Reflection\ListT(Reflection\Int32T::T))]
public array $int32Default = [],
#[Reflection\Field(2, new Reflection\ListT(Reflection\Int32T::T))]
public array $int32Packed = [],
#[Reflection\Field(3, new Reflection\ListT(Reflection\Int32T::T, false))]
public array $int32Unpacked = [],
#[Reflection\Field(4, new Reflection\ListT(new Reflection\EnumT(\Proto3\Packed\V1\Color::class)))]
public array $enumDefault = [],
#[Reflection\Field(5, new Reflection\ListT(new Reflection\EnumT(\Proto3\Packed\V1\Color::class)))]
public array $enumPacked = [],
#[Reflection\Field(6, new Reflection\ListT(new Reflection\EnumT(\Proto3\Packed\V1\Color::class), false))]
public array $enumUnpacked = [],
#[Reflection\Field(7, new Reflection\ListT(Reflection\BoolT::T))]
public array $boolDefault = [],
#[Reflection\Field(8, new Reflection\ListT(Reflection\BoolT::T))]
public array $boolPacked = [],
#[Reflection\Field(9, new Reflection\ListT(Reflection\BoolT::T, false))]
public array $boolUnpacked = [],
#[Reflection\Field(10, new Reflection\ListT(Reflection\SInt64T::T, false))]
public array $sint64Unpacked = [],
#[Reflection\Field(11, new Reflection\ListT(Reflection\Fixed32T::T, false))]
public array $fixed32Unpacked = [],
#[Reflection\Field(12, new Reflection\ListT(Reflection\DoubleT::T, false))]
public array $doubleUnpacked = [],
#[Reflection\Field(13, new Reflection\ListT(Reflection\Uint64T::T, false))]
public array $uint64Unpacked = [],
#[Reflection\Field(14, new Reflection\ListT(Reflection\StringT::T))]
public array $stringDefault = [],
#[Reflection\Field(15, new Reflection\ListT(Reflection\StringT::T, false))]
public array $stringUnpacked = [],
#[Reflection\Field(16, new Reflection\ListT(Reflection\BytesT::T, false))]
public array $bytesUnpacked = [],
#[Reflection\Field(17, new Reflection\ListT(new Reflection\ObjectT(\Proto3\Packed\V1\Inner::class), false))]
public array $messageUnpacked = [],
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.6.x-dev
* protoc — v6.32.1
*/

declare(strict_types=1);

\Thesis\Protobuf\Registry\Pool::get()->register(
new \Thesis\Protobuf\Registry\OnceRegistrar(new \Proto3\Packed\V1\DescriptorRegistry()),
);