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
16 changes: 7 additions & 9 deletions src/Plugin/Generator/PhpNamespacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ public function fqcn(string $path): string
$paths = explode('.', $path);
$name = $paths[\count($paths) - 1];

return Naming::joinNamespace([
'',
$this->resolve($path),
$name,
]);
return '\\' . $this->resolve($path) . '\\' . Naming::pascalCase($name);
}

/**
* Only the protobuf segments of the type path are normalised. The namespace of the file
* is already a PHP namespace, possibly a user-defined one ("Foo_Bar\\baz"), so it is kept
* verbatim: nested types must land in the very namespace the top-level ones are declared in.
*/
private function resolve(string $path): string
{
$rewritten = $this->rewrite($path);
Expand All @@ -51,10 +52,7 @@ private function resolve(string $path): string
$typeNamespace = \array_slice($paths, 0, \count($paths) - 1);

if (\count($typeNamespace) > 0) {
return Naming::joinNamespace([
$this->namespace,
...$typeNamespace,
]);
return $this->namespace . '\\' . Naming::joinNamespace($typeNamespace);
}

return $this->namespace;
Expand Down
38 changes: 38 additions & 0 deletions tests/GeneratedCode/samples/php_namespace_verbatim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* Populated messages of the php_namespace_verbatim snapshot case (a php_namespace with underscores and lowercase
* letters, which nested types, oneof interfaces and variants must share with the top-level types), keyed by label
* (see GeneratedCodeTest).
*/

use Thesis\Protobuf\Map;
use verbatim\Lower_Case\Envelope;
use Verbatim_Ns\under_score\Holder;
use Verbatim_Ns\under_score\Outer;

return [
'nested types and oneof variants' => new Outer(
inner: new Outer\Inner(new Outer\Inner\Deep('deep')),
kind: Outer\Kind::KIND_ONE,
choice: new Outer\ChoiceNested(new Outer\Inner(new Outer\Inner\Deep('variant'))),
),
'enum oneof variant' => new Outer(choice: new Outer\ChoiceKnown(Outer\Kind::KIND_ONE)),
'scalar oneof variant' => new Outer(choice: new Outer\ChoiceText('text')),
'nested types referenced from another message' => new Holder(
inner: new Outer\Inner(new Outer\Inner\Deep('a')),
deep: new Outer\Inner\Deep('b'),
kind: Outer\Kind::KIND_ONE,
list: [new Outer\Inner(), new Outer\Inner(new Outer\Inner\Deep('c'))],
byName: Map::fromArray(['d' => new Outer\Inner\Deep('d')]),
),
'lowercase php_namespace referencing nested types of another file' => new Envelope(
body: new Envelope\Body(new Outer\Inner(new Outer\Inner\Deep('body'))),
payload: new Envelope\PayloadInlineBody(new Envelope\Body()),
),
'lowercase php_namespace with an enum variant of another file' => new Envelope(
payload: new Envelope\PayloadKind(Outer\Kind::KIND_ONE),
),
];
30 changes: 30 additions & 0 deletions tests/Plugin/Generator/PhpNamespacerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Thesis\Protoc\Plugin\Generator;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

#[CoversClass(PhpNamespacer::class)]
final class PhpNamespacerTest extends TestCase
{
/**
* The namespace of the file is a PHP namespace already, so only the protobuf segments of the path are normalised.
*/
#[TestWith(['Foo_Bar\baz', 'Outer', 'Foo_Bar\baz', '\Foo_Bar\baz\Outer'])]
#[TestWith(['Foo_Bar\baz', 'Outer.Inner', 'Foo_Bar\baz\Outer', '\Foo_Bar\baz\Outer\Inner'])]
#[TestWith(['Foo_Bar\baz', 'Outer.Inner.Deep', 'Foo_Bar\baz\Outer\Inner', '\Foo_Bar\baz\Outer\Inner\Deep'])]
#[TestWith(['Foo_Bar\baz', 'outer_msg.inner_msg', 'Foo_Bar\baz\OuterMsg', '\Foo_Bar\baz\OuterMsg\InnerMsg'])]
#[TestWith(['app\v1', 'Outer.Choice', 'app\v1\Outer', '\app\v1\Outer\Choice'])]
#[TestWith(['App\V1', 'Outer.Inner', 'App\V1\Outer', '\App\V1\Outer\Inner'])]
public function testKeepsFileNamespaceVerbatim(string $namespace, string $path, string $expectedNamespace, string $expectedFqcn): void
{
$namespacer = new PhpNamespacer($namespace);

self::assertSame($expectedNamespace, $namespacer->create($path)->getName());
self::assertSame($expectedFqcn, $namespacer->fqcn($path));
}
}
21 changes: 21 additions & 0 deletions tests/fixtures/php_namespace_verbatim/lowercase.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

package php_namespace.verbatim.lowercase;

import "underscore.proto";

// A php_namespace starting with a lowercase letter keeps its case for nested types as well.
option php_namespace = "verbatim\\Lower_Case";

message Envelope {
message Body {
php_namespace.verbatim.underscore.Outer.Inner inner = 1;
}

Body body = 1;

oneof payload {
Body inline_body = 2;
php_namespace.verbatim.underscore.Outer.Kind kind = 3;
}
}
43 changes: 43 additions & 0 deletions tests/fixtures/php_namespace_verbatim/underscore.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
syntax = "proto3";

package php_namespace.verbatim.underscore;

// A php_namespace is already a PHP namespace and is used as is: nested types, oneof interfaces and their
// variants are generated into Verbatim_Ns\under_score\…, next to the top-level types, not into VerbatimNs\…
option php_namespace = "Verbatim_Ns\\under_score";

message Outer {
message Inner {
message Deep {
string value = 1;
}

Deep deep = 1;
}

enum Kind {
KIND_UNSPECIFIED = 0;
KIND_ONE = 1;
}

Inner inner = 1;
Kind kind = 2;

oneof choice {
string text = 3;
Inner nested = 4;
Kind known = 5;
}
}

message Holder {
Outer.Inner inner = 1;
Outer.Inner.Deep deep = 2;
Outer.Kind kind = 3;
repeated Outer.Inner list = 4;
map<string, Outer.Inner.Deep> by_name = 5;
}

service Lookup {
rpc Find(Outer.Inner) returns (Outer.Inner.Deep);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

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

declare(strict_types=1);

namespace Verbatim_Ns\under_score;

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

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

#[Override]
public function register(Registry\Pool $pool): void
{
$pool->add(Registry\Descriptor::base64(self::UNDERSCORE_DESCRIPTOR_BUFFER), new File(
name: 'underscore.proto',
messages: [
new File\MessageDescriptor('php_namespace.verbatim.underscore.Outer', \Verbatim_Ns\under_score\Outer::class),
new File\MessageDescriptor('php_namespace.verbatim.underscore.Outer.Inner', \Verbatim_Ns\under_score\Outer\Inner::class),
new File\MessageDescriptor('php_namespace.verbatim.underscore.Outer.Inner.Deep', \Verbatim_Ns\under_score\Outer\Inner\Deep::class),
new File\MessageDescriptor('php_namespace.verbatim.underscore.Holder', \Verbatim_Ns\under_score\Holder::class),
],
enums: [
new File\EnumDescriptor('php_namespace.verbatim.underscore.Outer.Kind', \Verbatim_Ns\under_score\Outer\Kind::class),
],
services: [
new File\ServiceDescriptor(
name: 'php_namespace.verbatim.underscore.Lookup',
methods: [
new File\MethodDescriptor('Find', false, false),
],
),
],
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

declare(strict_types=1);

namespace Verbatim_Ns\under_score;

use Thesis\Protobuf;
use Thesis\Protobuf\Reflection;

/**
* @api
*/
final readonly class Holder
{
/**
* @param list<\Verbatim_Ns\under_score\Outer\Inner> $list
* @param Protobuf\Map<string, \Verbatim_Ns\under_score\Outer\Inner\Deep> $byName
*/
public function __construct(
#[Reflection\Field(1, new Reflection\ObjectT(\Verbatim_Ns\under_score\Outer\Inner::class))]
public ?\Verbatim_Ns\under_score\Outer\Inner $inner = null,
#[Reflection\Field(2, new Reflection\ObjectT(\Verbatim_Ns\under_score\Outer\Inner\Deep::class))]
public ?\Verbatim_Ns\under_score\Outer\Inner\Deep $deep = null,
#[Reflection\Field(3, new Reflection\EnumT(\Verbatim_Ns\under_score\Outer\Kind::class))]
public \Verbatim_Ns\under_score\Outer\Kind $kind = \Verbatim_Ns\under_score\Outer\Kind::KIND_UNSPECIFIED,
#[Reflection\Field(4, new Reflection\ListT(new Reflection\ObjectT(\Verbatim_Ns\under_score\Outer\Inner::class)))]
public array $list = [],
#[Reflection\Field(5, new Reflection\MapT(Reflection\StringT::T, new Reflection\ObjectT(\Verbatim_Ns\under_score\Outer\Inner\Deep::class)))]
public Protobuf\Map $byName = new Protobuf\Map(),
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

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

declare(strict_types=1);

namespace Verbatim_Ns\under_score;

use Amp\Cancellation;
use Amp\NullCancellation;
use Thesis\Grpc;
use Thesis\Grpc\Client;
use Thesis\Grpc\Exception\ClientStreamIsClosed;
use Thesis\Grpc\InvokeError;
use Thesis\Grpc\Metadata;

/**
* @api
*/
final readonly class LookupClient
{
public function __construct(
private Client $client,
) {}

/**
* @throws ClientStreamIsClosed
* @throws InvokeError
*/
public function find(
\Verbatim_Ns\under_score\Outer\Inner $request,
Metadata $md = new Metadata(),
Cancellation $cancellation = new NullCancellation(),
): \Verbatim_Ns\under_score\Outer\Inner\Deep {
/** @var Client\Invoke<\Verbatim_Ns\under_score\Outer\Inner, \Verbatim_Ns\under_score\Outer\Inner\Deep> $invoke */
$invoke = new Client\Invoke(
method: '/php_namespace.verbatim.underscore.Lookup/Find',
output: \Verbatim_Ns\under_score\Outer\Inner\Deep::class,
type: Grpc\RpcType::Unary,
);

return $this->client->invoke(
request: $request,
invoke: $invoke,
md: $md,
cancellation: $cancellation,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

declare(strict_types=1);

namespace Verbatim_Ns\under_score;

use Amp\Cancellation;
use Thesis\Grpc\Metadata;

/**
* @api
*/
interface LookupServer
{
public function find(
\Verbatim_Ns\under_score\Outer\Inner $request,
Metadata $md,
Cancellation $cancellation,
): \Verbatim_Ns\under_score\Outer\Inner\Deep;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

declare(strict_types=1);

namespace Verbatim_Ns\under_score;

use Override;
use Thesis\Grpc;
use Thesis\Grpc\Server;

/**
* @api
*/
final readonly class LookupServerRegistry implements Server\ServiceRegistry
{
public function __construct(
private \Verbatim_Ns\under_score\LookupServer $server,
) {}

#[Override]
public function services(): iterable
{
yield new Server\Service('php_namespace.verbatim.underscore.Lookup', [
new Server\Rpc(
handle: new Server\Handle('Find', \Verbatim_Ns\under_score\Outer\Inner::class),
handler: new Server\UnaryHandler($this->server->find(...)),
type: Grpc\RpcType::Unary,
),
]);
}
}
Loading