Skip to content
Open
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
12 changes: 8 additions & 4 deletions src/Generation/BuildMethodFragmentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,15 @@ public function buildMethodSnippet(MethodDetails $methodDetails, string $methodS
};

$methodSignatureArguments = array_map('trim', array_filter(explode(',', $methodSignature)));
$methodSignatureTopLevelArguments = array_values(array_unique(array_map(
fn ($arg) => explode('.', $arg, 2)[0],
$methodSignatureArguments
)));
$requiredFields = $methodDetails->allFields
->filter(fn ($f) => in_array($f->name, $methodSignatureArguments))
->orderBy(fn ($f) => array_search($f->name, $methodSignatureArguments));
->filter(fn ($f) => in_array($f->name, $methodSignatureTopLevelArguments))
->orderBy(fn ($f) => array_search($f->name, $methodSignatureTopLevelArguments));

if (count($methodSignatureArguments) !== $requiredFields->count()) {
if (count($methodSignatureTopLevelArguments) !== $requiredFields->count()) {
throw new \LogicException(sprintf(
'missing method signature arguments (Expected "%s", found %s)',
$methodSignature,
Expand All @@ -153,7 +157,7 @@ public function buildMethodSnippet(MethodDetails $methodDetails, string $methodS
$cleanSignature = preg_replace('/\s+/', '', $methodSignature);
$methodName = $isFirst
? 'build'
: 'buildFrom' . Helpers::toUpperCamelCase(str_replace(',', '_', $cleanSignature));
: 'buildFrom' . Helpers::toUpperCamelCase(str_replace([',', '.'], '_', $cleanSignature));

return AST::method($methodName)
->withAccess(Access::PUBLIC, Access::STATIC)
Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/Generation/BuildMethodFragmentGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

final class BuildMethodFragmentGeneratorTest extends TestCase
{
public function testBuildMethodNamesWithSpacesInSignatures(): void
public function testBuildMethodNamesWithSpacesAndDotNotationSignatures(): void
{
$descBytes = ProtoLoader::loadDescriptorBytes('Utils/example.proto');
$descSet = new \Google\Protobuf\Internal\FileDescriptorSet();
Expand Down Expand Up @@ -62,12 +62,19 @@ public function testBuildMethodNamesWithSpacesInSignatures(): void
$this->assertTrue(isset($fragments[$requestClassName]));

$buildMethods = $fragments[$requestClassName];
$this->assertEquals(2, $buildMethods->count());
$this->assertEquals(3, $buildMethods->count());

// First builder method should be 'build'
$this->assertEquals('build', $buildMethods[0]->name);

// Second builder method should be 'buildFromNameNumber' (spaces stripped and camel-cased)
$this->assertEquals('buildFromNameNumber', $buildMethods[1]->name);

// Third builder method should be based on the clean original dot-notation signature.
$this->assertEquals('buildFromFooAFooB', $buildMethods[2]->name);

$buildMethodCode = $buildMethods[2]->toCode();
$this->assertStringContainsString('function buildFromFooAFooB(\Example\Foo $foo): self', $buildMethodCode);
$this->assertStringContainsString('setFoo($foo)', $buildMethodCode);
}
}
8 changes: 8 additions & 0 deletions tests/Unit/Utils/example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ syntax = "proto3";
package example;

import "google/api/client.proto";
import "google/api/field_behavior.proto";
import "google/api/resource.proto";
import "google/longrunning/operations.proto";

Expand All @@ -40,6 +41,7 @@ service Example {
rpc ExampleMethodWithSignatures(Request) returns(Response) {
option (google.api.method_signature) = "name";
option (google.api.method_signature) = "name, number";
option (google.api.method_signature) = "foo.a, foo.b";
}
}

Expand All @@ -55,6 +57,12 @@ message Request {
// Required. Percentage of selected conversation to analyze, between
// [0, 100].
float analysis_percentage = 3;
Foo foo = 4 [(google.api.field_behavior) = REQUIRED];
}

message Foo {
string a = 1;
string b = 2;
}

message Response {
Expand Down
Loading