From c9f15b89182cc44f8f152a9daea7233a6131d127 Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Tue, 24 Nov 2026 08:32:13 +0300 Subject: [PATCH] feat: generate groups thesis/protobuf 0.2.4 can write a message between start and end group tags, so groups are generated instead of being reported: - a proto2 group (optional, required, repeated, nested, a oneof variant); - an editions message field with features.message_encoding = DELIMITED, set on the field or inherited from the file. Such a field gets Reflection\GroupT in place of ObjectT and otherwise looks like a message field. The key and the value of a map entry stay length-prefixed, as in protoc. The bytes on the wire are checked against the output of protoc --encode. The runtime version gate of the generated code samples is gone, as thesis/protobuf ^0.2.4 is now required. Co-Authored-By: Claude Fable 5.2 --- README.md | 2 +- composer.json | 2 +- src/Plugin/Generator/ProtoGenerator.php | 15 +-- .../Generator/TypeDeclarationFactory.php | 24 +++- tests/CompilerGroupTest.php | 99 -------------- tests/CompilerOneofVariantTest.php | 104 --------------- tests/GeneratedCode/GeneratedCodeTest.php | 20 +-- tests/GeneratedCode/samples/groups.php | 121 ++++++++++++++++++ tests/fixtures/groups/editions_field.proto | 21 +++ tests/fixtures/groups/editions_file.proto | 25 ++++ tests/fixtures/groups/proto2.proto | 37 ++++++ .../Groups/Editions/Field/V1/Delimited.php | 42 ++++++ .../Editions/Field/V1/Delimited/Choice.php | 22 ++++ .../Field/V1/Delimited/ChoiceVariant.php | 26 ++++ .../Field/V1/Delimited/ChoiceVariantPlain.php | 26 ++++ .../Editions/Field/V1/DescriptorRegistry.php | 37 ++++++ .../groups/Groups/Editions/Field/V1/Inner.php | 26 ++++ .../Editions/File/V1/DescriptorRegistry.php | 37 ++++++ .../groups/Groups/Editions/File/V1/Leaf.php | 26 ++++ .../groups/Groups/Editions/File/V1/Tree.php | 42 ++++++ .../Groups/Editions/File/V1/Tree/Choice.php | 22 ++++ .../Editions/File/V1/Tree/ChoiceNumber.php | 26 ++++ .../Editions/File/V1/Tree/ChoiceVariant.php | 26 ++++ .../Groups/Proto2/V1/DescriptorRegistry.php | 42 ++++++ .../groups/Groups/Proto2/V1/RequiredGroup.php | 26 ++++ .../Groups/Proto2/V1/RequiredGroup/Header.php | 26 ++++ .../Groups/Proto2/V1/SearchResponse.php | 41 ++++++ .../Proto2/V1/SearchResponse/Choice.php | 22 ++++ .../Proto2/V1/SearchResponse/ChoiceNone.php | 26 ++++ .../Proto2/V1/SearchResponse/ChoicePicked.php | 26 ++++ .../Groups/Proto2/V1/SearchResponse/Item.php | 28 ++++ .../Proto2/V1/SearchResponse/Item/Tag.php | 28 ++++ .../Proto2/V1/SearchResponse/Picked.php | 26 ++++ .../Proto2/V1/SearchResponse/Result.php | 28 ++++ .../groups/Groups/autoload.metadata.php | 16 +++ 35 files changed, 925 insertions(+), 238 deletions(-) delete mode 100644 tests/CompilerGroupTest.php delete mode 100644 tests/CompilerOneofVariantTest.php create mode 100644 tests/GeneratedCode/samples/groups.php create mode 100644 tests/fixtures/groups/editions_field.proto create mode 100644 tests/fixtures/groups/editions_file.proto create mode 100644 tests/fixtures/groups/proto2.proto create mode 100644 tests/snapshots/groups/Groups/Editions/Field/V1/Delimited.php create mode 100644 tests/snapshots/groups/Groups/Editions/Field/V1/Delimited/Choice.php create mode 100644 tests/snapshots/groups/Groups/Editions/Field/V1/Delimited/ChoiceVariant.php create mode 100644 tests/snapshots/groups/Groups/Editions/Field/V1/Delimited/ChoiceVariantPlain.php create mode 100644 tests/snapshots/groups/Groups/Editions/Field/V1/DescriptorRegistry.php create mode 100644 tests/snapshots/groups/Groups/Editions/Field/V1/Inner.php create mode 100644 tests/snapshots/groups/Groups/Editions/File/V1/DescriptorRegistry.php create mode 100644 tests/snapshots/groups/Groups/Editions/File/V1/Leaf.php create mode 100644 tests/snapshots/groups/Groups/Editions/File/V1/Tree.php create mode 100644 tests/snapshots/groups/Groups/Editions/File/V1/Tree/Choice.php create mode 100644 tests/snapshots/groups/Groups/Editions/File/V1/Tree/ChoiceNumber.php create mode 100644 tests/snapshots/groups/Groups/Editions/File/V1/Tree/ChoiceVariant.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/DescriptorRegistry.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/RequiredGroup.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/RequiredGroup/Header.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/SearchResponse.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/Choice.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/ChoiceNone.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/ChoicePicked.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/Item.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/Item/Tag.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/Picked.php create mode 100644 tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/Result.php create mode 100644 tests/snapshots/groups/Groups/autoload.metadata.php diff --git a/README.md b/README.md index d777d34..3d0723c 100644 --- a/README.md +++ b/README.md @@ -788,7 +788,7 @@ To avoid having to generate these types each time, it is recommended to use libr - [x] `enum defaults` - [x] `proto3 optional` - [ ] `extensions` -- [ ] `groups` +- [x] `groups` - [x] `nested types` - [x] `import public` - [x] `packed repeated` diff --git a/composer.json b/composer.json index 2bb657f..e59e5cf 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "composer-runtime-api": "^2.2", "nette/php-generator": "^4.2.1", "thesis/package-version": "^0.1.2", - "thesis/protobuf": "^0.2.2", + "thesis/protobuf": "^0.2.4", "thesis/protobuf-compiler-types": "^0.3", "thesis/protobuf-descriptor-types": "^0.3", "thesis/protoregistry": "^0.1.2" diff --git a/src/Plugin/Generator/ProtoGenerator.php b/src/Plugin/Generator/ProtoGenerator.php index 52442f4..bff0778 100644 --- a/src/Plugin/Generator/ProtoGenerator.php +++ b/src/Plugin/Generator/ProtoGenerator.php @@ -319,13 +319,6 @@ private function generateMessage( $properties = []; foreach ($message->fields as $field) { - // A group is written between start and end group tags, not length-prefixed like a message. The runtime - // has no such encoding, so generating it as a message would silently produce an incompatible wire format. - // Checked before oneofs are set aside, as a group may also be a oneof variant. - if ($field->type === FieldDescriptorProto\Type::TYPE_GROUP) { - throw new CodeCannotBeGenerated("Message {$message->fqcn}: field {$field->name} is a group, groups are not supported."); - } - if ($field->oneOfIndex !== null && $field->proto3Optional === null) { $oneOfByIndex[$field->oneOfIndex][] = $field; @@ -334,10 +327,6 @@ private function generateMessage( $features = $field->features; - if ($features?->messageEncoding === FeatureSet\MessageEncoding::DELIMITED) { - throw new CodeCannotBeGenerated('DELIMITED message encoding are not supported'); - } - $parameterName = self::fieldPropertyName($field); self::claimPropertyName($properties, $message, $parameterName, "field {$field->name}"); @@ -347,8 +336,8 @@ private function generateMessage( $type = null; if ($field->map !== null) { - $keyType = $this->types->create($field->map->key, $runtime); - $valueType = $this->types->create($field->map->value, $runtime); + $keyType = $this->types->create($field->map->key, $runtime, mapEntry: true); + $valueType = $this->types->create($field->map->value, $runtime, mapEntry: true); $map = $runtime->protobuf('Map'); $type = new TypeDeclaration( diff --git a/src/Plugin/Generator/TypeDeclarationFactory.php b/src/Plugin/Generator/TypeDeclarationFactory.php index dd07ff4..d9f8701 100644 --- a/src/Plugin/Generator/TypeDeclarationFactory.php +++ b/src/Plugin/Generator/TypeDeclarationFactory.php @@ -5,6 +5,7 @@ namespace Thesis\Protoc\Plugin\Generator; use Nette\PhpGenerator\Literal; +use Thesis\Google\Protobuf\FeatureSet\MessageEncoding; use Thesis\Google\Protobuf\FieldDescriptorProto\Type; use Thesis\Protoc\Plugin\Dependency; use Thesis\Protoc\Plugin\Naming; @@ -19,7 +20,10 @@ public function __construct( private Dependency\Graph $graph, ) {} - public function create(Parser\FieldDescriptor $field, RuntimeImports $runtime): TypeDeclaration + /** + * @param bool $mapEntry whether $field is the key or the value of a map entry, which is always length-prefixed + */ + public function create(Parser\FieldDescriptor $field, RuntimeImports $runtime, bool $mapEntry = false): TypeDeclaration { if ($field->typeName !== null) { $fieldType = $this->graph->get($field->typeName); @@ -41,7 +45,11 @@ public function create(Parser\FieldDescriptor $field, RuntimeImports $runtime): return new TypeDeclaration( phpType: $fieldType->fqcn, - reflectionType: Literal::new($runtime->reflection($isEnum ? 'EnumT' : 'ObjectT'), [ + reflectionType: Literal::new($runtime->reflection(match (true) { + $isEnum => 'EnumT', + self::isGroup($field, $mapEntry) => 'GroupT', + default => 'ObjectT', + }), [ new Literal("{$fieldType->fqcn}::class"), ]), nullable: !$isEnum, @@ -112,4 +120,16 @@ public function create(Parser\FieldDescriptor $field, RuntimeImports $runtime): default => throw new \RuntimeException('No suitable declaration found.'), }; } + + /** + * Whether $field holds a message written as a group, between a start and an end group tag: a proto2 group, or + * an editions message field with `features.message_encoding = DELIMITED`, set on the field or inherited from + * the file. DELIMITED does not apply to a map entry: its key and value stay length-prefixed, as in protoc, + * and the runtime does not support groups as map values. + */ + private static function isGroup(Parser\FieldDescriptor $field, bool $mapEntry): bool + { + return $field->type === Type::TYPE_GROUP + || (!$mapEntry && $field->type === Type::TYPE_MESSAGE && $field->features?->messageEncoding === MessageEncoding::DELIMITED); + } } diff --git a/tests/CompilerGroupTest.php b/tests/CompilerGroupTest.php deleted file mode 100644 index 948cb50..0000000 --- a/tests/CompilerGroupTest.php +++ /dev/null @@ -1,99 +0,0 @@ -expectException(CodeCannotBeGenerated::class); - $this->expectExceptionMessage('Message t.M: field result is a group, groups are not supported.'); - - new Compiler(Encoder\Builder::buildDefault(), __DIR__ . '/../map.json')->compile(new CodeGeneratorRequest( - fileToGenerate: ['t.proto'], - protoFile: [ - new FileDescriptorProto( - name: 't.proto', - package: 't', - messageType: [ - new DescriptorProto( - name: 'M', - field: [$field], - nestedType: [ - new DescriptorProto( - name: 'Result', - field: [ - new FieldDescriptorProto( - name: 'url', - number: 2, - label: Label::LABEL_OPTIONAL, - type: Type::TYPE_STRING, - jsonName: 'url', - ), - ], - ), - ], - oneofDecl: $oneof !== null ? [$oneof] : [], - ), - ], - ), - ], - )); - } - - /** - * @return iterable - */ - public static function provideGroupIsReportedCases(): iterable - { - // syntax = "proto2"; message M { optional group Result = 1 { optional string url = 2; } } - yield 'optional' => [self::group(Label::LABEL_OPTIONAL), null]; - - // syntax = "proto2"; message M { required group Result = 1 { optional string url = 2; } } - yield 'required' => [self::group(Label::LABEL_REQUIRED), null]; - - // syntax = "proto2"; message M { repeated group Result = 1 { optional string url = 2; } } - yield 'repeated' => [self::group(Label::LABEL_REPEATED), null]; - - // syntax = "proto2"; message M { oneof o { group Result = 1 { optional string url = 2; } } } - yield 'oneof variant' => [self::group(Label::LABEL_OPTIONAL, oneofIndex: 0), new OneofDescriptorProto('o')]; - } - - private static function group(Label $label, ?int $oneofIndex = null): FieldDescriptorProto - { - return new FieldDescriptorProto( - name: 'result', - number: 1, - label: $label, - type: Type::TYPE_GROUP, - typeName: '.t.M.Result', - oneofIndex: $oneofIndex, - jsonName: 'result', - ); - } -} diff --git a/tests/CompilerOneofVariantTest.php b/tests/CompilerOneofVariantTest.php deleted file mode 100644 index 9da1c86..0000000 --- a/tests/CompilerOneofVariantTest.php +++ /dev/null @@ -1,104 +0,0 @@ -expectException(CodeCannotBeGenerated::class); - $this->expectExceptionMessage('DELIMITED message encoding are not supported'); - - self::compile($oneofIndex); - } - - /** - * @return iterable - */ - public static function provideDelimitedMessageIsReportedCases(): iterable - { - // edition = "2023"; message M { message Inner { int32 a = 1; } Inner d = 1 [features.message_encoding = DELIMITED]; } - yield 'field' => [null]; - - // edition = "2023"; message M { message Inner { int32 a = 1; } oneof o { Inner d = 1 [features.message_encoding = DELIMITED]; } } - yield 'oneof variant' => [0]; - } - - private static function compile(?int $oneofIndex): void - { - new Compiler(Encoder\Builder::buildDefault(), __DIR__ . '/../map.json')->compile(new CodeGeneratorRequest( - fileToGenerate: ['t.proto'], - protoFile: [ - new FileDescriptorProto( - name: 't.proto', - package: 't', - messageType: [ - new DescriptorProto( - name: 'M', - field: [ - new FieldDescriptorProto( - name: 'd', - number: 1, - label: Label::LABEL_OPTIONAL, - type: Type::TYPE_MESSAGE, - typeName: '.t.M.Inner', - oneofIndex: $oneofIndex, - jsonName: 'd', - options: new FieldOptions( - features: new FeatureSet(messageEncoding: FeatureSet\MessageEncoding::DELIMITED), - ), - ), - ], - nestedType: [ - new DescriptorProto( - name: 'Inner', - field: [ - new FieldDescriptorProto( - name: 'a', - number: 1, - label: Label::LABEL_OPTIONAL, - type: Type::TYPE_INT32, - jsonName: 'a', - ), - ], - ), - ], - oneofDecl: $oneofIndex !== null ? [new OneofDescriptorProto('o')] : [], - ), - ], - syntax: 'editions', - edition: Edition::EDITION_2023, - ), - ], - )); - } -} diff --git a/tests/GeneratedCode/GeneratedCodeTest.php b/tests/GeneratedCode/GeneratedCodeTest.php index f016bcc..b17f62b 100644 --- a/tests/GeneratedCode/GeneratedCodeTest.php +++ b/tests/GeneratedCode/GeneratedCodeTest.php @@ -4,7 +4,6 @@ namespace Thesis\Protoc\GeneratedCode; -use Composer\InstalledVersions; use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -55,13 +54,6 @@ final class GeneratedCodeTest extends TestCase ], ]; - /** - * thesis/protobuf before 0.2.2 writes expanded repeated fields (ListT(..., false)) packed and then cannot read - * them back, so the populated samples do not round-trip on the lowest dependencies. Remove the gate once the - * plugin requires thesis/protobuf ^0.2.2. - */ - private const string SAMPLES_MIN_RUNTIME = '0.2.2'; - #[DataProvider('provideSnapshotCases')] public function testSnapshotFilesCompile(string $case): void { @@ -105,8 +97,6 @@ public function testSnapshotCodeLoadsAndRoundTrips(string $case): void )); $samples = self::SAMPLES_DIR . "/{$case}.php"; - $runtime = InstalledVersions::getVersion('thesis/protobuf') ?? '0'; - $skipSamples = is_file($samples) && version_compare($runtime, self::SAMPLES_MIN_RUNTIME, '<'); $code = <<<'PHP' require 'vendor/autoload.php'; @@ -114,13 +104,11 @@ public function testSnapshotCodeLoadsAndRoundTrips(string $case): void PHP; [$exitCode, $output, $stderr] = self::runPhp( - ['-r', $code, '--', self::SNAPSHOTS_DIR . "/{$case}", is_file($samples) && !$skipSamples ? $samples : '', ...$lintOnly], + ['-r', $code, '--', self::SNAPSHOTS_DIR . "/{$case}", is_file($samples) ? $samples : '', ...$lintOnly], \dirname(__DIR__, 2), ); - $knownDefects = $skipSamples - ? array_filter(self::KNOWN_DEFECTS[$case] ?? [], static fn(string $step): bool => !str_starts_with($step, 'round-trip sample '), ARRAY_FILTER_USE_KEY) - : self::KNOWN_DEFECTS[$case] ?? []; + $knownDefects = self::KNOWN_DEFECTS[$case] ?? []; $step = null; $failures = []; $noise = ''; @@ -153,10 +141,6 @@ public function testSnapshotCodeLoadsAndRoundTrips(string $case): void $knownDefects, "Snapshot case {$case}: these known defects do not reproduce any more, remove them from KNOWN_DEFECTS.", ); - - if ($skipSamples) { - self::markTestSkipped("Samples of {$case} are not round-tripped: thesis/protobuf {$runtime} < " . self::SAMPLES_MIN_RUNTIME . '.'); - } } /** diff --git a/tests/GeneratedCode/samples/groups.php b/tests/GeneratedCode/samples/groups.php new file mode 100644 index 0000000..07d7ff4 --- /dev/null +++ b/tests/GeneratedCode/samples/groups.php @@ -0,0 +1,121 @@ + .proto`); they are also decoded back into the message. protoc writes the fields + * in the order of their numbers, the encoder required fields first and oneofs last, so the fixture numbers the + * fields in that order for whole messages to be compared byte for byte. + */ + +use Groups\Editions\Field\V1 as Field; +use Groups\Editions\File\V1 as File; +use Groups\Proto2\V1 as Proto2; +use Thesis\Protobuf\Decoder; +use Thesis\Protobuf\Encoder; +use Thesis\Protobuf\Map; + +$searchResponse = new Proto2\SearchResponse( + result: new Proto2\SearchResponse\Result(url: 'a', rank: 1), + item: [ + new Proto2\SearchResponse\Item(id: 1, tag: new Proto2\SearchResponse\Item\Tag(name: 'x')), + new Proto2\SearchResponse\Item(id: 2), + new Proto2\SearchResponse\Item(), + ], + best: new Proto2\SearchResponse\Result(url: 'b'), + others: [new Proto2\SearchResponse\Result(rank: 2)], + choice: new Proto2\SearchResponse\ChoicePicked(new Proto2\SearchResponse\Picked(id: 3)), +); + +$delimited = new Field\Delimited( + required: new Field\Inner(1), + plain: new Field\Inner(2), + single: new Field\Inner(3), + list: [new Field\Inner(4), new Field\Inner()], + choice: new Field\Delimited\ChoiceVariant(new Field\Inner(5)), +); + +$tree = new File\Tree( + single: new File\Leaf(1), + list: [new File\Leaf(2), new File\Leaf()], + byName: Map::fromArray(['k' => new File\Leaf(4)]), + plain: new File\Leaf(5), + child: new File\Tree(single: new File\Leaf(6), child: new File\Tree()), + choice: new File\Tree\ChoiceVariant(new File\Leaf(7)), +); + +return [ + 'proto2 groups set' => $searchResponse, + 'proto2 required group' => new Proto2\RequiredGroup(new Proto2\RequiredGroup\Header(version: 1)), + 'editions DELIMITED fields set' => $delimited, + 'editions DELIMITED file default' => $tree, + 'groups are written between start and end group tags' => static function () use ($searchResponse, $delimited, $tree): void { + $encoder = Encoder\Builder::buildDefault(); + $decoder = Decoder\Builder::buildDefault(); + + $expected = [ + // Result{url:"a" rank:1} Item{id:1 Tag{name:"x"}} Item{id:2} Item{} Picked{id:3} best{url:"b"} others{rank:2} + 'proto2 groups' => [ + $searchResponse, + '0b 12 01 61 18 01 0c 23 28 01 33 3a 01 78 34 24 23 28 02 24 23 24 42 03 12 01 62 4a 02 18 02 53 58 03 54', + ], + // Result{} none:"n" + 'proto2 empty group, string variant' => [ + new Proto2\SearchResponse( + result: new Proto2\SearchResponse\Result(), + choice: new Proto2\SearchResponse\ChoiceNone('n'), + ), + '0b 0c 62 01 6e', + ], + // Picked{} + 'proto2 empty group variant' => [ + new Proto2\SearchResponse(choice: new Proto2\SearchResponse\ChoicePicked(new Proto2\SearchResponse\Picked())), + '53 54', + ], + // Header{version:1} + 'proto2 required group' => [new Proto2\RequiredGroup(new Proto2\RequiredGroup\Header(version: 1)), '0b 10 01 0c'], + // Header{} + 'proto2 empty required group' => [new Proto2\RequiredGroup(new Proto2\RequiredGroup\Header()), '0b 0c'], + // required{value:1} plain{value:2} single{value:3} list{value:4} list{} variant{value:5} + 'editions DELIMITED fields' => [ + $delimited, + '0b 08 01 0c 12 02 08 02 1b 08 03 1c 23 08 04 24 23 24 2b 08 05 2c', + ], + // required{} variant_plain{value:6} + 'editions empty required group, length-prefixed variant' => [ + new Field\Delimited( + required: new Field\Inner(), + choice: new Field\Delimited\ChoiceVariantPlain(new Field\Inner(6)), + ), + '0b 0c 32 02 08 06', + ], + // single{value:1} list{value:2} list{} by_name{key:"k" value{value:4}} plain{value:5} child{single{value:6} child{}} variant{value:7} + 'editions DELIMITED file default' => [ + $tree, + '0b 08 01 0c 13 08 02 14 13 14 1a 07 0a 01 6b 12 02 08 04 22 02 08 05 2b 0b 08 06 0c 2b 2c 2c 33 08 07 34', + ], + // variant{} + 'editions empty group variant' => [new File\Tree(choice: new File\Tree\ChoiceVariant(new File\Leaf())), '33 34'], + ]; + + foreach ($expected as $label => [$message, $hex]) { + $bytes = hex2bin(str_replace(' ', '', $hex)); + $actual = $encoder->encode($message); + + if ($actual !== $bytes) { + throw new LogicException(sprintf('%s: expected %s on the wire, got %s', $label, bin2hex($bytes), bin2hex($actual))); + } + + // Compared by structure: the decoded message is another object. + $decoded = var_export($decoder->decode($bytes, $message::class), true); + $original = var_export($message, true); + + if ($decoded !== $original) { + throw new LogicException(sprintf('%s: decoded %s, expected %s', $label, $decoded, $original)); + } + } + }, +]; diff --git a/tests/fixtures/groups/editions_field.proto b/tests/fixtures/groups/editions_field.proto new file mode 100644 index 0000000..b38a439 --- /dev/null +++ b/tests/fixtures/groups/editions_field.proto @@ -0,0 +1,21 @@ +edition = "2023"; + +package groups.editions.field.v1; + +message Inner { + int32 value = 1; +} + +// features.message_encoding is LENGTH_PREFIXED by default: a message field is a group only with DELIMITED. +message Delimited { + Inner required = 1 [features.message_encoding = DELIMITED, features.field_presence = LEGACY_REQUIRED]; + Inner plain = 2; + Inner single = 3 [features.message_encoding = DELIMITED]; + repeated Inner list = 4 [features.message_encoding = DELIMITED]; + + oneof choice { + Inner variant = 5 [features.message_encoding = DELIMITED]; + Inner variant_plain = 6; + } +} + diff --git a/tests/fixtures/groups/editions_file.proto b/tests/fixtures/groups/editions_file.proto new file mode 100644 index 0000000..f4d8a05 --- /dev/null +++ b/tests/fixtures/groups/editions_file.proto @@ -0,0 +1,25 @@ +edition = "2023"; + +package groups.editions.file.v1; + +// DELIMITED for every message field of the file. +option features.message_encoding = DELIMITED; + +message Leaf { + int32 value = 1; +} + +message Tree { + Leaf single = 1; + repeated Leaf list = 2; + // A map is never a group: its entries and their message values stay length-prefixed. + map by_name = 3; + Leaf plain = 4 [features.message_encoding = LENGTH_PREFIXED]; + // A message referring to itself through a group. + Tree child = 5; + + oneof choice { + Leaf variant = 6; + int32 number = 7; + } +} diff --git a/tests/fixtures/groups/proto2.proto b/tests/fixtures/groups/proto2.proto new file mode 100644 index 0000000..a7ee7f4 --- /dev/null +++ b/tests/fixtures/groups/proto2.proto @@ -0,0 +1,37 @@ +syntax = "proto2"; + +package groups.proto2.v1; + +// A group is written between a start and an end group tag (wire types 3 and 4), not after its length. +message SearchResponse { + optional group Result = 1 { + optional string url = 2; + optional int32 rank = 3; + } + + repeated group Item = 4 { + optional int32 id = 5; + + // A group nested in a group. + optional group Tag = 6 { + optional string name = 7; + } + } + + // The type of a group is an ordinary message: as a message field it is length-prefixed. + optional Result best = 8; + repeated Result others = 9; + + oneof choice { + group Picked = 10 { + optional int32 id = 11; + } + string none = 12; + } +} + +message RequiredGroup { + required group Header = 1 { + optional int32 version = 2; + } +} diff --git a/tests/snapshots/groups/Groups/Editions/Field/V1/Delimited.php b/tests/snapshots/groups/Groups/Editions/Field/V1/Delimited.php new file mode 100644 index 0000000..fdb5006 --- /dev/null +++ b/tests/snapshots/groups/Groups/Editions/Field/V1/Delimited.php @@ -0,0 +1,42 @@ + $list + */ + public function __construct( + #[Reflection\Field(1, new Reflection\GroupT(\Groups\Editions\Field\V1\Inner::class))] + public \Groups\Editions\Field\V1\Inner $required, + #[Reflection\Field(2, new Reflection\ObjectT(\Groups\Editions\Field\V1\Inner::class))] + public ?\Groups\Editions\Field\V1\Inner $plain = null, + #[Reflection\Field(3, new Reflection\GroupT(\Groups\Editions\Field\V1\Inner::class))] + public ?\Groups\Editions\Field\V1\Inner $single = null, + #[Reflection\Field(4, new Reflection\ListT(new Reflection\GroupT(\Groups\Editions\Field\V1\Inner::class), false))] + public array $list = [], + #[Reflection\OneOf([ + \Groups\Editions\Field\V1\Delimited\ChoiceVariant::class, + \Groups\Editions\Field\V1\Delimited\ChoiceVariantPlain::class, + ])] + public ?\Groups\Editions\Field\V1\Delimited\Choice $choice = null, + ) {} +} diff --git a/tests/snapshots/groups/Groups/Editions/Field/V1/Delimited/Choice.php b/tests/snapshots/groups/Groups/Editions/Field/V1/Delimited/Choice.php new file mode 100644 index 0000000..f40e3e9 --- /dev/null +++ b/tests/snapshots/groups/Groups/Editions/Field/V1/Delimited/Choice.php @@ -0,0 +1,22 @@ +add(Registry\Descriptor::base64(self::EDITIONS_FIELD_DESCRIPTOR_BUFFER), new File( + name: 'editions_field.proto', + messages: [ + new File\MessageDescriptor('groups.editions.field.v1.Inner', \Groups\Editions\Field\V1\Inner::class), + new File\MessageDescriptor('groups.editions.field.v1.Delimited', \Groups\Editions\Field\V1\Delimited::class), + ], + )); + } +} diff --git a/tests/snapshots/groups/Groups/Editions/Field/V1/Inner.php b/tests/snapshots/groups/Groups/Editions/Field/V1/Inner.php new file mode 100644 index 0000000..e30b611 --- /dev/null +++ b/tests/snapshots/groups/Groups/Editions/Field/V1/Inner.php @@ -0,0 +1,26 @@ +add(Registry\Descriptor::base64(self::EDITIONS_FILE_DESCRIPTOR_BUFFER), new File( + name: 'editions_file.proto', + messages: [ + new File\MessageDescriptor('groups.editions.file.v1.Leaf', \Groups\Editions\File\V1\Leaf::class), + new File\MessageDescriptor('groups.editions.file.v1.Tree', \Groups\Editions\File\V1\Tree::class), + ], + )); + } +} diff --git a/tests/snapshots/groups/Groups/Editions/File/V1/Leaf.php b/tests/snapshots/groups/Groups/Editions/File/V1/Leaf.php new file mode 100644 index 0000000..715f9eb --- /dev/null +++ b/tests/snapshots/groups/Groups/Editions/File/V1/Leaf.php @@ -0,0 +1,26 @@ + $list + * @param ?Protobuf\Map $byName A map is never a group: its entries and their message values stay length-prefixed. + * @param ?\Groups\Editions\File\V1\Tree $child A message referring to itself through a group. + */ + public function __construct( + #[Reflection\Field(1, new Reflection\GroupT(\Groups\Editions\File\V1\Leaf::class))] + public ?\Groups\Editions\File\V1\Leaf $single = null, + #[Reflection\Field(2, new Reflection\ListT(new Reflection\GroupT(\Groups\Editions\File\V1\Leaf::class), false))] + public array $list = [], + #[Reflection\Field(3, new Reflection\MapT(Reflection\StringT::T, new Reflection\ObjectT(\Groups\Editions\File\V1\Leaf::class)))] + public ?Protobuf\Map $byName = null, + #[Reflection\Field(4, new Reflection\ObjectT(\Groups\Editions\File\V1\Leaf::class))] + public ?\Groups\Editions\File\V1\Leaf $plain = null, + #[Reflection\Field(5, new Reflection\GroupT(\Groups\Editions\File\V1\Tree::class))] + public ?\Groups\Editions\File\V1\Tree $child = null, + #[Reflection\OneOf([\Groups\Editions\File\V1\Tree\ChoiceVariant::class, \Groups\Editions\File\V1\Tree\ChoiceNumber::class])] + public ?\Groups\Editions\File\V1\Tree\Choice $choice = null, + ) {} +} diff --git a/tests/snapshots/groups/Groups/Editions/File/V1/Tree/Choice.php b/tests/snapshots/groups/Groups/Editions/File/V1/Tree/Choice.php new file mode 100644 index 0000000..a5d5c82 --- /dev/null +++ b/tests/snapshots/groups/Groups/Editions/File/V1/Tree/Choice.php @@ -0,0 +1,22 @@ +add(Registry\Descriptor::base64(self::PROTO2_DESCRIPTOR_BUFFER), new File( + name: 'proto2.proto', + messages: [ + new File\MessageDescriptor('groups.proto2.v1.SearchResponse', \Groups\Proto2\V1\SearchResponse::class), + new File\MessageDescriptor('groups.proto2.v1.SearchResponse.Result', \Groups\Proto2\V1\SearchResponse\Result::class), + new File\MessageDescriptor('groups.proto2.v1.SearchResponse.Item', \Groups\Proto2\V1\SearchResponse\Item::class), + new File\MessageDescriptor('groups.proto2.v1.SearchResponse.Item.Tag', \Groups\Proto2\V1\SearchResponse\Item\Tag::class), + new File\MessageDescriptor('groups.proto2.v1.SearchResponse.Picked', \Groups\Proto2\V1\SearchResponse\Picked::class), + new File\MessageDescriptor('groups.proto2.v1.RequiredGroup', \Groups\Proto2\V1\RequiredGroup::class), + new File\MessageDescriptor('groups.proto2.v1.RequiredGroup.Header', \Groups\Proto2\V1\RequiredGroup\Header::class), + ], + )); + } +} diff --git a/tests/snapshots/groups/Groups/Proto2/V1/RequiredGroup.php b/tests/snapshots/groups/Groups/Proto2/V1/RequiredGroup.php new file mode 100644 index 0000000..aa3271e --- /dev/null +++ b/tests/snapshots/groups/Groups/Proto2/V1/RequiredGroup.php @@ -0,0 +1,26 @@ + $item + * @param ?\Groups\Proto2\V1\SearchResponse\Result $best The type of a group is an ordinary message: as a message field it is length-prefixed. + * @param list<\Groups\Proto2\V1\SearchResponse\Result> $others + */ + public function __construct( + #[Reflection\Field(1, new Reflection\GroupT(\Groups\Proto2\V1\SearchResponse\Result::class))] + public ?\Groups\Proto2\V1\SearchResponse\Result $result = null, + #[Reflection\Field(4, new Reflection\ListT(new Reflection\GroupT(\Groups\Proto2\V1\SearchResponse\Item::class), false))] + public array $item = [], + #[Reflection\Field(8, new Reflection\ObjectT(\Groups\Proto2\V1\SearchResponse\Result::class))] + public ?\Groups\Proto2\V1\SearchResponse\Result $best = null, + #[Reflection\Field(9, new Reflection\ListT(new Reflection\ObjectT(\Groups\Proto2\V1\SearchResponse\Result::class), false))] + public array $others = [], + #[Reflection\OneOf([\Groups\Proto2\V1\SearchResponse\ChoicePicked::class, \Groups\Proto2\V1\SearchResponse\ChoiceNone::class])] + public ?\Groups\Proto2\V1\SearchResponse\Choice $choice = null, + ) {} +} diff --git a/tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/Choice.php b/tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/Choice.php new file mode 100644 index 0000000..e264126 --- /dev/null +++ b/tests/snapshots/groups/Groups/Proto2/V1/SearchResponse/Choice.php @@ -0,0 +1,22 @@ +register( + new \Thesis\Protobuf\Registry\OnceRegistrar(new \Groups\Editions\Field\V1\DescriptorRegistry()), + new \Thesis\Protobuf\Registry\OnceRegistrar(new \Groups\Editions\File\V1\DescriptorRegistry()), + new \Thesis\Protobuf\Registry\OnceRegistrar(new \Groups\Proto2\V1\DescriptorRegistry()), +);