Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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: 11 additions & 5 deletions src/Plugin/Generator/ProtoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private function generateMessage(PhpNamespace $namespace, string $className, Par
'',
$this->namespacer->namespace,
$message->path,
\sprintf("{$oneOfName}%s::class", Naming::pascalCase($variant->name)),
\sprintf('%s::class', self::oneofVariantName($oneOfName, $variant)),
]),
),
$variants,
Expand Down Expand Up @@ -358,7 +358,7 @@ private function generateOneof(
->addComment('@api')
->addComment('@phpstan-sealed (')
->addComment(implode(" |\n", array_map(
static fn(Parser\FieldDescriptor $variant) => \sprintf(' %s', self::oneofVariantName($oneof->name, $variant->name)),
static fn(Parser\FieldDescriptor $variant) => \sprintf(' %s', self::oneofVariantName($oneof->name, $variant)),
$variants,
)))
->addComment(')');
Expand All @@ -381,7 +381,7 @@ private function generateOneofVariant(
Parser\FieldDescriptor $variant,
): iterable {
$interfaceName = Naming::pascalCase($oneof->name);
$className = self::oneofVariantName($oneof->name, $variant->name);
$className = self::oneofVariantName($oneof->name, $variant);

$descriptor = new Parser\MessageDescriptor(
name: $className,
Expand Down Expand Up @@ -449,8 +449,14 @@ private function parseDefaultValue(
};
}

private static function oneofVariantName(string $oneof, string $variant): string
private static function oneofVariantName(string $oneof, Parser\FieldDescriptor $variant): string
{
return Naming::pascalCase("{$oneof} {$variant}");
if ($variant->jsonName !== null) {
$name = \sprintf('%s%s', Naming::pascalCase($oneof), Naming::jsonName($variant->jsonName));
} else {
$name = Naming::pascalCase("{$oneof} {$variant->name}");
}

return Naming::secure($name);
}
}
5 changes: 5 additions & 0 deletions src/Plugin/Naming.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public static function pascalCase(string $name): string
return self::secure(ucfirst(self::camelCase($name)));
}

public static function jsonName(string $name): string
{
return ucfirst($name);
}

public static function namespace(string $name): string
{
$name = implode('.', array_map(
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/pgquery/pgquery.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package pg_query;

message Node {
oneof node {
RTEPermissionInfo rtepermission_info = 99 [json_name="RTEPermissionInfo"];
}
}

message RTEPermissionInfo
{
bool inh = 2 [json_name="inh"];
}
26 changes: 26 additions & 0 deletions tests/snapshots/pgquery/PgQuery/Node.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.3.x-dev
* protoc — v6.32.1
* Source: pgquery.proto
*/

declare(strict_types=1);

namespace PgQuery;

use Thesis\Protobuf\Reflection;

/**
* @api
*/
final readonly class Node
{
public function __construct(
#[Reflection\OneOf([\PgQuery\Node\NodeRTEPermissionInfo::class])]
public ?\PgQuery\Node\Node $node = null,
) {}
}
21 changes: 21 additions & 0 deletions tests/snapshots/pgquery/PgQuery/Node/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

declare(strict_types=1);

namespace PgQuery\Node;

/**
* @api
* @phpstan-sealed (
* NodeRTEPermissionInfo
* )
*/
interface Node {}
26 changes: 26 additions & 0 deletions tests/snapshots/pgquery/PgQuery/Node/NodeRTEPermissionInfo.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.3.x-dev
* protoc — v6.32.1
* Source: pgquery.proto
*/

declare(strict_types=1);

namespace PgQuery\Node;

use Thesis\Protobuf\Reflection;

/**
* @api
*/
final readonly class NodeRTEPermissionInfo implements \PgQuery\Node\Node
{
public function __construct(
#[Reflection\Field(99, new Reflection\ObjectT(\PgQuery\RTEPermissionInfo::class))]
public ?\PgQuery\RTEPermissionInfo $RTEPermissionInfo = null,
) {}
}
37 changes: 37 additions & 0 deletions tests/snapshots/pgquery/PgQuery/PgqueryDescriptorRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

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

declare(strict_types=1);

namespace PgQuery;

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

/**
* @api
*/
final readonly class PgqueryDescriptorRegistry implements Registry\Registrar
{
private const string DESCRIPTOR_BUFFER = 'Cg1wZ3F1ZXJ5LnByb3RvEghwZ19xdWVyeSJcCgROb2RlEkwKEnJ0ZXBlcm1pc3Npb25faW5mbxhjIAEoCzIbLnBnX3F1ZXJ5LlJURVBlcm1pc3Npb25JbmZvSABSEVJURVBlcm1pc3Npb25JbmZvQgYKBG5vZGUiJQoRUlRFUGVybWlzc2lvbkluZm8SEAoDaW5oGAIgASgIUgNpbmhKqgIKBhIEAAANAQoICgEMEgMAABIKCAoBAhIDAgARCgoKAgQAEgQEAAgBCgoKAwQAARIDBAgMCgwKBAQACAASBAUEBwUKDAoFBAAIAAESAwUKDgoLCgQEAAIAEgMGCFIKDAoFBAACAAYSAwYIGQoMCgUEAAIAARIDBhosCgwKBQQAAgADEgMGLzEKDAoFBAACAAgSAwYyUQoMCgUEAAIAChIDBjNQCgwKBQQAAgAKEgMGPVAKCgoCBAESBAoADQEKCgoDBAEBEgMKCBkKCwoEBAECABIDDAQjCgwKBQQBAgAFEgMMBAgKDAoFBAECAAESAwwJDAoMCgUEAQIAAxIDDA8QCgwKBQQBAgAIEgMMESIKDAoFBAECAAoSAwwSIQoMCgUEAQIAChIDDBwhYgZwcm90bzM=';

#[Override]
public function register(Registry\Pool $pool): void
{
$pool->add(Registry\Descriptor::base64(self::DESCRIPTOR_BUFFER), new File(
name: 'pgquery.proto',
messages: [
new File\MessageDescriptor('pg_query.Node', \PgQuery\Node::class),
new File\MessageDescriptor('pg_query.RTEPermissionInfo', \PgQuery\RTEPermissionInfo::class),
],
));
}
}
26 changes: 26 additions & 0 deletions tests/snapshots/pgquery/PgQuery/RTEPermissionInfo.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.3.x-dev
* protoc — v6.32.1
* Source: pgquery.proto
*/

declare(strict_types=1);

namespace PgQuery;

use Thesis\Protobuf\Reflection;

/**
* @api
*/
final readonly class RTEPermissionInfo
{
public function __construct(
#[Reflection\Field(2, Reflection\BoolT::T)]
public bool $inh = false,
) {}
}
14 changes: 14 additions & 0 deletions tests/snapshots/pgquery/PgQuery/autoload.metadata.php
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.3.x-dev
* protoc — v6.32.1
*/

declare(strict_types=1);

\Thesis\Protobuf\Registry\Pool::get()->register(
new \Thesis\Protobuf\Registry\OnceRegistrar(new \PgQuery\PgqueryDescriptorRegistry()),
);