Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: miscellaneous snippet gen fixes #540

Merged
merged 8 commits into from
Dec 5, 2022
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
35 changes: 29 additions & 6 deletions src/Generation/SnippetDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private function handleField(FieldDetails $field, string $parentFieldName = null
{
// resource based format fields
if ($field->useResourceTestValue) {
$this->handleFormattedResource($field);
$this->handleFormattedResource($field, $parentFieldName);
return;
}

Expand Down Expand Up @@ -135,9 +135,16 @@ private function handleBidiOrClientStreaming(): void
}

$this->handleField($field, '');
$prefix = $field->useResourceTestValue
? 'formatted_'
: '';
$setter = $field->setter->getName();
$value = $value->$setter(
AST::var(Helpers::toCamelCase($field->camelName))
AST::var(
Helpers::toCamelCase(
$prefix . $field->camelName
)
)
);
}

Expand Down Expand Up @@ -192,8 +199,15 @@ private function handleMessage(FieldDetails $field, string $parentFieldName = nu

$this->handleField($subField, $fieldVar->name);
$setter = $subField->setter->getName();
$prefix = $subField->useResourceTestValue
? 'formatted_'
: '';
$value = $value->$setter(
AST::var(Helpers::toCamelCase($fieldVar->name . '_' . $subField->camelName))
AST::var(
Helpers::toCamelCase(
$prefix . $fieldVar->name . '_' . $subField->camelName
)
)
);
}

Expand Down Expand Up @@ -285,10 +299,13 @@ private function handleOneof(FieldDetails $field, string $parentFieldName = null

/**
* @param FieldDetails $field
* @param string|null $parentFieldName
*/
private function handleFormattedResource(FieldDetails $field): void
{
$fieldName = Helpers::toCamelCase("formatted_{$field->name}");
private function handleFormattedResource(
FieldDetails $field,
string $parentFieldName = null
): void {
$fieldName = Helpers::toCamelCase("formatted_{$parentFieldName}_{$field->name}");
$var = AST::var($fieldName);
$arrayElementVar = null;
$formatMethodArgs = $field->resourceDetails
Expand Down Expand Up @@ -341,6 +358,12 @@ private function handleFormattedResource(FieldDetails $field): void
$this->filterDocLines($field->docLines)
)
);

// Don't append to rpcArguments if a parent exists.
if ($parentFieldName !== null) {
return;
}

$this->rpcArguments = $this->rpcArguments->append($var);
}

Expand Down
48 changes: 31 additions & 17 deletions src/Generation/SnippetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@
namespace Google\Generator\Generation;

use Google\ApiCore\ApiException;
use Google\ApiCore\BidiStream;
use Google\ApiCore\ClientStream;
use Google\ApiCore\OperationResponse;
use Google\ApiCore\ServerStream;
use Google\Generator\Ast\AST;
use Google\Generator\Ast\PhpDoc;
use Google\Generator\Ast\PhpFunction;
use Google\Generator\Ast\Variable;
use Google\Generator\Collections\Vector;
use Google\Generator\Utils\Helpers;
use Google\Generator\Utils\Transport;
use Google\Generator\Utils\Type;
use Google\Protobuf\GPBEmpty;
use Google\Rpc\Status;

class SnippetGenerator
Expand Down Expand Up @@ -175,7 +171,7 @@ private function rpcMethodExampleOperation(SnippetDetails $snippetDetails): AST
: Vector::new([
AST::inlineVarDoc(
$context->type($snippetDetails->methodDetails->lroResponseType),
$responseVar
$resultVar
),
AST::assign($resultVar, $responseVar->getResult()),
$this->buildPrintFCall(
Expand Down Expand Up @@ -207,20 +203,29 @@ private function rpcMethodExamplePaginated(SnippetDetails $snippetDetails): AST
$responseVar = AST::var('response');
$elementVar = AST::var('element');
$context = $snippetDetails->context;
$resourceType = $snippetDetails->methodDetails->resourceType;

return $this->buildSnippetFunctions(
$snippetDetails,
[
$this->buildClientMethodCall($snippetDetails, $responseVar),
PHP_EOL,
AST::inlineVarDoc(
$context->type($snippetDetails->methodDetails->resourceType),
$elementVar
),
// When transport is REST only, disabling this for now.
// Need to further investigate an issue causing the resourceType
// to render as ItemsEntry with a mapped entry, despite a
// different value being outlined in the proto.
$this->serviceDetails->transportType === Transport::REST
? null
: AST::inlineVarDoc(
$context->type($resourceType),
$elementVar
),
AST::foreach($responseVar, $elementVar)(
$this->buildPrintFCall(
'Element data: %s',
"{$elementVar->toCode()}->serializeToJsonString()"
$resourceType->isClass()
? "{$elementVar->toCode()}->serializeToJsonString()"
: $elementVar->toCode()
)
)
]
Expand All @@ -236,6 +241,7 @@ private function rpcMethodExampleBidiStreaming(SnippetDetails $snippetDetails):
$streamVar = AST::var('stream');
$elementVar = AST::var('element');
$context = $snippetDetails->context;
$responseType = $snippetDetails->methodDetails->responseType;

return $this->buildSnippetFunctions(
$snippetDetails,
Expand All @@ -244,13 +250,15 @@ private function rpcMethodExampleBidiStreaming(SnippetDetails $snippetDetails):
$streamVar->writeAll($snippetDetails->rpcArguments),
PHP_EOL,
AST::inlineVarDoc(
$context->type($snippetDetails->methodDetails->responseType),
$context->type($responseType),
$elementVar
),
AST::foreach($streamVar->closeWriteAndReadAll(), $elementVar)(
$this->buildPrintFCall(
'Element data: %s',
"{$elementVar->toCode()}->serializeToJsonString()"
$responseType->isClass()
? "{$elementVar->toCode()}->serializeToJsonString()"
: $elementVar->toCode()
)
)
]
Expand All @@ -266,20 +274,23 @@ private function rpcMethodExampleServerStreaming(SnippetDetails $snippetDetails)
$streamVar = AST::var('stream');
$elementVar = AST::var('element');
$context = $snippetDetails->context;
$responseType = $snippetDetails->methodDetails->responseType;

return $this->buildSnippetFunctions(
$snippetDetails,
[
$this->buildClientMethodCall($snippetDetails, $streamVar),
PHP_EOL,
AST::inlineVarDoc(
$context->type($snippetDetails->methodDetails->responseType),
$context->type($responseType),
$elementVar
),
AST::foreach($streamVar->readAll(), $elementVar)(
$this->buildPrintFCall(
'Element data: %s',
"{$elementVar->toCode()}->serializeToJsonString()"
$responseType->isClass()
? "{$elementVar->toCode()}->serializeToJsonString()"
: $elementVar->toCode()
)
)
]
Expand All @@ -295,14 +306,15 @@ private function rpcMethodExampleClientStreaming(SnippetDetails $snippetDetails)
$streamVar = AST::var('stream');
$responseVar = AST::var('response');
$context = $snippetDetails->context;
$responseType = $snippetDetails->methodDetails->responseType;

return $this->buildSnippetFunctions(
$snippetDetails,
[
$this->buildClientMethodCall($snippetDetails, $streamVar),
PHP_EOL,
AST::inlineVarDoc(
$context->type($snippetDetails->methodDetails->responseType),
$context->type($responseType),
$responseVar
),
AST::assign(
Expand All @@ -311,7 +323,9 @@ private function rpcMethodExampleClientStreaming(SnippetDetails $snippetDetails)
),
$this->buildPrintFCall(
'Response data: %s',
"{$responseVar->toCode()}->serializeToJsonString()"
$responseType->isClass()
? "{$responseVar->toCode()}->serializeToJsonString()"
: $responseVar->toCode()
)
]
);
Expand Down
2 changes: 2 additions & 0 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function showUsageAndExit()
$descBytes = file_get_contents($opts['descriptor']);
$package = $opts['package'];
$outputDir = $opts['output'];
$defaultLicenseYear = -1;
[$grpcServiceConfig, $gapicYaml, $serviceYaml, $transport, $generateGapicMetadata, $numericEnums, $generateSnippets] = readOptions($opts);

// Generate PHP code.
Expand All @@ -150,6 +151,7 @@ function showUsageAndExit()
$gapicYaml,
$serviceYaml,
$numericEnums,
$defaultLicenseYear,
$generateSnippets
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function analyze_iam_policy_longrunning_sample(string $analysisQueryScope): void
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var AnalyzeIamPolicyLongrunningResponse $response */
/** @var AnalyzeIamPolicyLongrunningResponse $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function export_assets_sample(string $parent): void
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var ExportAssetsResponse $response */
/** @var ExportAssetsResponse $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
// [START compute_v1_generated_Addresses_AggregatedList_sync]
use Google\ApiCore\ApiException;
use Google\ApiCore\PagedListResponse;
use Google\Cloud\Compute\V1\AddressAggregatedList\ItemsEntry;
use Google\Cloud\Compute\V1\AddressesClient;

/**
Expand All @@ -43,7 +42,6 @@ function aggregated_list_sample(string $project): void
/** @var PagedListResponse $response */
$response = $addressesClient->aggregatedList($project);

/** @var ItemsEntry $element */
foreach ($response as $element) {
printf('Element data: %s' . PHP_EOL, $element->serializeToJsonString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
// [START compute_v1_generated_Addresses_List_sync]
use Google\ApiCore\ApiException;
use Google\ApiCore\PagedListResponse;
use Google\Cloud\Compute\V1\Address;
use Google\Cloud\Compute\V1\AddressesClient;

/**
Expand All @@ -49,7 +48,6 @@ function list_sample(string $orderBy, string $project, string $region): void
/** @var PagedListResponse $response */
$response = $addressesClient->list($orderBy, $project, $region);

/** @var Address $element */
foreach ($response as $element) {
printf('Element data: %s' . PHP_EOL, $element->serializeToJsonString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function create_cluster_sample(
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var Cluster $response */
/** @var Cluster $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function diagnose_cluster_sample(string $projectId, string $region, string $clus
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var DiagnoseClusterResults $response */
/** @var DiagnoseClusterResults $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function start_cluster_sample(string $projectId, string $region, string $cluster
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var Cluster $response */
/** @var Cluster $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function stop_cluster_sample(string $projectId, string $region, string $clusterN
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var Cluster $response */
/** @var Cluster $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function update_cluster_sample(
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var Cluster $response */
/** @var Cluster $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function submit_job_as_operation_sample(
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var Job $response */
/** @var Job $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function create_function_sample(string $formattedLocation): void
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var CloudFunction $response */
/** @var CloudFunction $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function update_function_sample(): void
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var CloudFunction $response */
/** @var CloudFunction $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function list_logs_sample(string $formattedParent): void

/** @var string $element */
foreach ($response as $element) {
printf('Element data: %s' . PHP_EOL, $element->serializeToJsonString());
printf('Element data: %s' . PHP_EOL, $element);
}
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function create_instance_sample(
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var Instance $response */
/** @var Instance $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function export_instance_sample(string $name): void
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var Instance $response */
/** @var Instance $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function failover_instance_sample(string $formattedName): void
$response->pollUntilComplete();

if ($response->operationSucceeded()) {
/** @var Instance $response */
/** @var Instance $result */
$result = $response->getResult();
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
} else {
Expand Down
Loading