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
34 changes: 30 additions & 4 deletions src/Plugin/Generator/GrpcGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ public function generateClient(Parser\ServiceDescriptor $service): PhpNamespace
$classType = new ClassType($name)
->setFinal()
->setReadOnly()
->addComment('@api')
->addComment($service->comment !== null ? "\n{$service->comment}" : '');
->addComment('@api');

if ($service->options?->deprecated === true) {
$classType = $classType->addComment('@deprecated');
}

if ($service->comment !== null) {
$classType = $classType->addComment("\n{$service->comment}");
}

$namespace->add($classType);

Expand All @@ -59,6 +66,12 @@ public function generateClient(Parser\ServiceDescriptor $service): PhpNamespace
->addMethod(Naming::camelCase($method->name))
->setPublic();

if ($method->options?->deprecated === true) {
$namespace->addUse('Deprecated');

$classMethod = $classMethod->addAttribute('Deprecated');
}

if ($method->comment !== null) {
$classMethod->addComment((string) $method->comment);
}
Expand Down Expand Up @@ -198,8 +211,15 @@ public function generateServer(Parser\ServiceDescriptor $service): PhpNamespace
$namespace = $this->namespacer->create($service->path);

$interfaceType = new InterfaceType(\sprintf('%sServer', Naming::pascalCase($service->name)))
->addComment('@api')
->addComment($service->comment !== null ? "\n{$service->comment}" : '');
->addComment('@api');

if ($service->options?->deprecated === true) {
$interfaceType = $interfaceType->addComment('@deprecated');
}

if ($service->comment !== null) {
$interfaceType = $interfaceType->addComment("\n{$service->comment}");
}

$namespace->add($interfaceType);

Expand All @@ -224,6 +244,12 @@ public function generateServer(Parser\ServiceDescriptor $service): PhpNamespace
->addMethod(Naming::camelCase($method->name))
->setPublic();

if ($method->options?->deprecated === true) {
$namespace->addUse('Deprecated');

$interfaceMethod = $interfaceMethod->addAttribute('Deprecated');
}

if ($method->comment !== null) {
$interfaceMethod->addComment((string) $method->comment);
}
Expand Down
34 changes: 27 additions & 7 deletions src/Plugin/Generator/ProtoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,23 @@ public function generateEnum(Parser\EnumDescriptor $enum): PhpNamespace
));

$enumType = new EnumType($enumName)
->addComment('@api')
->addComment($enum->comment !== null ? "\n{$enum->comment}" : '')
->setType('int')
->setCases(array_map(
static fn(Parser\EnumCaseDescriptor $case) => new EnumCase(Naming::secureEnumCase($case->name))
->setValue($case->value)
->setComment((string) $case->comment),
->addComment(($deprecated = $case->options?->deprecated) === true ? '@deprecated' : '')
->addComment(($deprecated === true ? "\n" : '') . (string) $case->comment),
$enum->cases,
));
))
->addComment('@api');

if ($enum->options?->deprecated === true) {
$enumType = $enumType->addComment('@deprecated');
}

if ($enum->comment !== null) {
$enumType = $enumType->addComment("\n{$enum->comment}");
}

$namespace->add($enumType);

Expand Down Expand Up @@ -127,9 +135,17 @@ private function generateMessage(PhpNamespace $namespace, string $className, Par
$classType = new ClassType($className)
->setFinal()
->setReadOnly()
->setImplements($implements)
->addComment('@api')
->addComment($message->comment !== null ? "\n{$message->comment}" : '');
->setImplements($implements);

$classType = $classType->addComment('@api');

if ($message->options?->deprecated === true) {
$classType = $classType->addComment('@deprecated');
}

if ($message->comment !== null) {
$classType = $classType->addComment("\n{$message->comment}");
}

$namespace->add($classType);

Expand Down Expand Up @@ -226,6 +242,10 @@ private function generateMessage(PhpNamespace $namespace, string $className, Par
->setNullable($nullable)
->setDefaultValue($repeated ? [] : $default);

if ($field->options?->deprecated === true) {
$parameter->addComment('@deprecated');
}

if ($repeated || $field->comment !== null || $type->isMap) {
$docType = $type->resolvedType();

Expand Down
2 changes: 2 additions & 0 deletions src/Plugin/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ private static function parseServices(array $descriptors, CommentExtractor $comm
$name,
self::parseServiceMethods($descriptor->method, $comments->clone($commentPath)),
$comments->extract($commentPath),
$descriptor->options,
);
}

Expand Down Expand Up @@ -273,6 +274,7 @@ private static function parseServiceMethods(
clientStreaming: $descriptor->clientStreaming ?? false,
serverStreaming: $descriptor->serverStreaming ?? false,
comment: $comments->extract(\sprintf('%d.%d', Comments::SERVICE_METHOD_COMMENT_PATH, $idx)),
options: $descriptor->options,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Plugin/Parser/ServiceDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Thesis\Protoc\Plugin\Parser;

use Google\Protobuf\ServiceOptions;
use Thesis\Protoc\Plugin\Comment;

/**
Expand All @@ -19,5 +20,6 @@ public function __construct(
public string $path,
public array $methods,
public ?Comment $comment = null,
public ?ServiceOptions $options = null,
) {}
}
2 changes: 2 additions & 0 deletions src/Plugin/Parser/ServiceMethodDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Thesis\Protoc\Plugin\Parser;

use Google\Protobuf\MethodOptions;
use Thesis\Protoc\Plugin\Comment;

/**
Expand All @@ -20,6 +21,7 @@ public function __construct(
public bool $clientStreaming,
public bool $serverStreaming,
public ?Comment $comment = null,
public ?MethodOptions $options = null,
) {
$this->bidirectionalStreaming = $this->clientStreaming && $this->serverStreaming;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/fixtures/deprecated/deprecated.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
syntax = "proto3";

package test.api.v1;

// Type is deprecated.
enum Type {
option deprecated = true;

// Case is deprecated.
TYPE_UNSPECIFIED = 0 [deprecated = true];
TYPE_PROTO3 = 1;
}

// Request is deprecated.
message Request {
option deprecated = true;

// Field is deprecated.
string name = 1 [deprecated = true];

// Nested message is also deprecated.
message NestedRequest {
option deprecated = true;

// Nested field is deprecated too.
string name = 1 [deprecated = true];
}
}

// Service is deprecated.
service EchoService {
option deprecated = true;

// Rpc is also deprecated.
rpc Echo(Request) returns (Request) {
option deprecated = true;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

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

declare(strict_types=1);

namespace Test\Api\V1;

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

/**
* @api
*/
final readonly class DeprecatedDescriptorRegistry implements Registry\Registrar
{
private const string DESCRIPTOR_BUFFER = 'ChBkZXByZWNhdGVkLnByb3RvEgt0ZXN0LmFwaS52MSJSCgdSZXF1ZXN0EhYKBG5hbWUYASABKAlSBG5hbWVCAhgBGisKDU5lc3RlZFJlcXVlc3QSFgoEbmFtZRgBIAEoCVIEbmFtZUICGAE6AhgBOgIYASo1CgRUeXBlEhgKEFRZUEVfVU5TUEVDSUZJRUQQABoCCAESDwoLVFlQRV9QUk9UTzMQARoCGAEySwoLRWNob1NlcnZpY2USNwoERWNobxIULnRlc3QuYXBpLnYxLlJlcXVlc3QaFC50ZXN0LmFwaS52MS5SZXF1ZXN0IgOIAgEaA4gCAUq+BgoGEgQAACUBCggKAQwSAwAAEgoICgECEgMCABQKIQoCBQASBAUACwEaFSBUeXBlIGlzIGRlcHJlY2F0ZWQuCgoKCgMFAAESAwUFCQoKCgMFAAMSAwYEHQoLCgQFAAMDEgMGBB0KIgoEBQACABIDCQQtGhUgQ2FzZSBpcyBkZXByZWNhdGVkLgoKDAoFBQACAAESAwkEFAoMCgUFAAIAAhIDCRcYCgwKBQUAAgADEgMJGSwKDQoGBQACAAMBEgMJGisKCwoEBQACARIDCgQUCgwKBQUAAgEBEgMKBA8KDAoFBQACAQISAwoSEwokCgIEABIEDgAbARoYIFJlcXVlc3QgaXMgZGVwcmVjYXRlZC4KCgoKAwQAARIDDggPCgoKAwQABxIDDwQdCgsKBAQABwMSAw8EHQojCgQEAAIAEgMSBCgaFiBGaWVsZCBpcyBkZXByZWNhdGVkLgoKDAoFBAACAAUSAxIECgoMCgUEAAIAARIDEgsPCgwKBQQAAgADEgMSEhMKDAoFBAACAAgSAxIUJwoNCgYEAAIACAMSAxIVJgoyCgQEAAMAEgQVBBoFGiQgTmVzdGVkIG1lc3NhZ2UgaXMgYWxzbyBkZXByZWNhdGVkLgoKDAoFBAADAAESAxUMGQoMCgUEAAMABxIDFgghCg0KBgQAAwAHAxIDFgghCjAKBgQAAwACABIDGQgsGiEgTmVzdGVkIGZpZWxkIGlzIGRlcHJlY2F0ZWQgdG9vLgoKDgoHBAADAAIABRIDGQgOCg4KBwQAAwACAAESAxkPEwoOCgcEAAMAAgADEgMZFhcKDgoHBAADAAIACBIDGRgrCg8KCAQAAwACAAgDEgMZGSoKJAoCBgASBB4AJQEaGCBTZXJ2aWNlIGlzIGRlcHJlY2F0ZWQuCgoKCgMGAAESAx4IEwoKCgMGAAMSAx8EHQoLCgQGAAMhEgMfBB0KJwoEBgACABIEIgQkBRoZIFJwYyBpcyBhbHNvIGRlcHJlY2F0ZWQuCgoMCgUGAAIAARIDIggMCgwKBQYAAgACEgMiDRQKDAoFBgACAAMSAyIfJgoMCgUGAAIABBIDIwghCg0KBgYAAgAEIRIDIwghYgZwcm90bzM=';

#[Override]
public function register(Registry\Pool $pool): void
{
$pool->add(Registry\Descriptor::base64(self::DESCRIPTOR_BUFFER), new File(
name: 'deprecated.proto',
messages: [
new File\MessageDescriptor('test.api.v1.Request', \Test\Api\V1\Request::class),
new File\MessageDescriptor('test.api.v1.Request.NestedRequest', \Test\Api\V1\Request\NestedRequest::class),
],
enums: [
new File\EnumDescriptor('test.api.v1.Type', \Test\Api\V1\Type::class),
],
services: [
new File\ServiceDescriptor(
name: 'test.api.v1.EchoService',
methods: [
new File\MethodDescriptor('Echo', false, false),
],
),
],
));
}
}
59 changes: 59 additions & 0 deletions tests/snapshots/deprecated/Test/Api/V1/EchoServiceClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

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

declare(strict_types=1);

namespace Test\Api\V1;

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

/**
* @api
* @deprecated
*
* Service is deprecated.
*/
final readonly class EchoServiceClient
{
public function __construct(
private Client $client,
) {}

/**
* Rpc is also deprecated.
* @throws ClientStreamIsClosed
* @throws InvokeError
*/
#[Deprecated]
public function echo(
\Test\Api\V1\Request $request,
Metadata $md = new Metadata(),
Cancellation $cancellation = new NullCancellation(),
): \Test\Api\V1\Request {
/** @var Client\Invoke<\Test\Api\V1\Request, \Test\Api\V1\Request> $invoke */
$invoke = new Client\Invoke(
method: '/test.api.v1.EchoService/Echo',
type: \Test\Api\V1\Request::class,
);

return $this->client->invoke(
request: $request,
invoke: $invoke,
md: $md,
cancellation: $cancellation,
);
}
}
36 changes: 36 additions & 0 deletions tests/snapshots/deprecated/Test/Api/V1/EchoServiceServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

declare(strict_types=1);

namespace Test\Api\V1;

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

/**
* @api
* @deprecated
*
* Service is deprecated.
*/
interface EchoServiceServer
{
/**
* Rpc is also deprecated.
*/
#[Deprecated]
public function echo(
\Test\Api\V1\Request $request,
Metadata $md,
Cancellation $cancellation,
): \Test\Api\V1\Request;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

declare(strict_types=1);

namespace Test\Api\V1;

use Override;
use Thesis\Grpc\Server;

/**
* @api
*/
final readonly class EchoServiceServerRegistry implements Server\ServiceRegistry
{
public function __construct(
private \Test\Api\V1\EchoServiceServer $server,
) {}

#[Override]
public function services(): iterable
{
yield new Server\Service('test.api.v1.EchoService', [
new Server\Rpc(
new Server\Handle('Echo', \Test\Api\V1\Request::class),
new Server\UnaryHandler($this->server->echo(...)),
Server\RpcType::Unary,
),
]);
}
}
Loading