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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 2 additions & 13 deletions src/Plugin/Generator/ProtoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}");
Expand All @@ -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(
Expand Down
24 changes: 22 additions & 2 deletions src/Plugin/Generator/TypeDeclarationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
99 changes: 0 additions & 99 deletions tests/CompilerGroupTest.php

This file was deleted.

104 changes: 0 additions & 104 deletions tests/CompilerOneofVariantTest.php

This file was deleted.

20 changes: 2 additions & 18 deletions tests/GeneratedCode/GeneratedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -105,22 +97,18 @@ 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';
exit(\Thesis\Protoc\GeneratedCode\SnapshotValidator::main(array_slice($argv, 1)));
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 = '';
Expand Down Expand Up @@ -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 . '.');
}
}

/**
Expand Down
Loading