Skip to content

Commit

Permalink
Fix logic for doc text related to grpc and rest transports
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Apr 25, 2024
1 parent bb59b9b commit 573609c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Generation/GapicClientGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ private function getClientDefaults(): PhpClassMember

// TODO: Consolidate setting all the known array values together.
// We do this here to maintain the existing sensible ordering.
if ($this->serviceDetails->transportType === Transport::GRPC_REST) {
if ($this->serviceDetails->transportType !== Transport::REST) {
$clientDefaultValues['gcpApiConfigPath'] =
AST::concat(AST::__DIR__, "/../resources/{$this->serviceDetails->grpcConfigFilename}");
}
Expand Down
51 changes: 34 additions & 17 deletions src/Generation/GapicClientV2Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,30 +506,30 @@ private function construct(): PhpClassMember
$options = AST::var('options');
$optionsParam = AST::param(ResolvedType::array(), $options, AST::array([]));
$clientOptions = AST::var('clientOptions');
$transportType = $this->serviceDetails->transportType;

// Assumes there are only two transport types.
$isGrpcRest = $this->serviceDetails->transportType === Transport::GRPC_REST;

$restTransportDocText = 'At the moment, supports only `rest`.';
$grpcTransportDocText = 'May be either the string `rest` or `grpc`. Defaults to `grpc` if gRPC support is detected on the system.';
$transportDocText =
PhpDoc::text(
'The transport used for executing network requests. ',
$isGrpcRest ? $grpcTransportDocText : $restTransportDocText,
$this->transportDocText($transportType),
'*Advanced usage*: Additionally, it is possible to pass in an already instantiated',
// TODO(vNext): Don't use a fully-qualified type here.
$ctx->type(Type::fromName(TransportInterface::class), true),
'object. Note that when this object is provided, any settings in $transportConfig, and any $apiEndpoint',
'setting, will be ignored.'
);

$transportConfigSampleValues = [
'grpc' => AST::arrayEllipsis(),
'rest' => AST::arrayEllipsis()
];

$transportConfigSampleValues = [];
if ($isGrpcRest) {
$transportConfigSampleValues['grpc'] = AST::arrayEllipsis();
if (Transport::isGrpcOnly($transportType)) {
unset($transportConfigSampleValues['rest']);
} elseif (Transport::isRestOnly($transportType)) {
unset($transportConfigSampleValues['grpc']);
}
// Set this value here, don't initialize it, so we can maintain alphabetical order
// for the resulting printed doc.
$transportConfigSampleValues['rest'] = AST::arrayEllipsis();

$transportConfigDocText =
PhpDoc::text(
'Configuration options that will be used to construct the transport. Options for',
Expand All @@ -547,22 +547,26 @@ private function construct(): PhpClassMember
'See the',
AST::call(
$ctx->type(
Type::fromName($isGrpcRest ? GrpcTransport::class : RestTransport::class),
Type::fromName(
Transport::isRestOnly($transportType) ?
RestTransport::class :
GrpcTransport::class
),
true
),
AST::method('build')
)(),
$isGrpcRest ? 'and' : '',
$isGrpcRest
? AST::call(
Transport::isGRPCRest($transportType) ? 'and' : '',
Transport::isGRPCRest($transportType) ?
AST::call(
$ctx->type(
Type::fromName(RestTransport::class),
true
),
AST::method('build')
)()
: '',
$isGrpcRest ? 'methods ' : 'method ',
Transport::isGRPCRest($transportType) ? 'methods ' : 'method ',
'for the supported options.'
);
return AST::method('__construct')
Expand Down Expand Up @@ -669,6 +673,19 @@ private function construct(): PhpClassMember
));
}

private function transportDocText(int $transportType): string
{
if (Transport::isRestOnly($transportType)) {
return 'At the moment, supports only `rest`.';
}

if (Transport::isGrpcOnly($transportType)) {
return 'At the moment, supports only `grpc`.';
}

return 'May be either the string `rest` or `grpc`. Defaults to `grpc` if gRPC support is detected on the system.';;
}

private function rpcMethod(MethodDetails $method): PhpClassMember
{
$request = AST::var('request');
Expand Down
20 changes: 20 additions & 0 deletions src/Utils/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,24 @@ public static function parseTransport(?string $transport): int

throw new \Exception("Transport $transport not supported");
}

public static function isRestOnly(int $transport): bool
{
return Transport::compareTransports(Transport::REST, $transport);
}

public static function isGrpcOnly(int $transport): bool
{
return Transport::compareTransports(Transport::GRPC, $transport);
}

public static function isGRPCRest(int $transport): bool
{
return Transport::compareTransports(Transport::GRPC_REST, $transport);
}

private static function compareTransports(int $transportA, int $transportB): bool
{
return $transportA === $transportB;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public static function parseName(string $formattedName, string $template = null)
* provided in the resources folder.
* @type string|TransportInterface $transport
* The transport used for executing network requests. At the moment, supports only
* `rest`. *Advanced usage*: Additionally, it is possible to pass in an already
* `grpc`. *Advanced usage*: Additionally, it is possible to pass in an already
* instantiated {@see \Google\ApiCore\Transport\TransportInterface} object. Note
* that when this object is provided, any settings in $transportConfig, and any
* $apiEndpoint setting, will be ignored.
Expand All @@ -281,9 +281,9 @@ public static function parseName(string $formattedName, string $template = null)
* each supported transport type should be passed in a key for that transport. For
* example:
* $transportConfig = [
* 'rest' => [...],
* 'grpc' => [...],
* ];
* See the {@see \Google\ApiCore\Transport\RestTransport::build()} method for the
* See the {@see \Google\ApiCore\Transport\GrpcTransport::build()} method for the
* supported options.
* @type callable $clientCertSource
* A callable which returns the client cert as a string. This can be used to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private static function getClientDefaults()
'apiEndpoint' => self::SERVICE_ADDRESS . ':' . self::DEFAULT_SERVICE_PORT,
'clientConfig' => __DIR__ . '/../resources/cloud_redis_client_config.json',
'descriptorsConfigPath' => __DIR__ . '/../resources/cloud_redis_descriptor_config.php',
'gcpApiConfigPath' => __DIR__ . '/../resources/cloud_redis_grpc_config.json',
'credentialsConfig' => [
'defaultScopes' => self::$serviceScopes,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ config_version: 3

authentication:
rules:
- selector: 'testing.basicGrpcOnly.BasicGrpcOnly.*'
- selector: 'testing.basicGrpcOnly.BasicGrpcOnly.*'
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private static function supportedTransports()
* provided in the resources folder.
* @type string|TransportInterface $transport
* The transport used for executing network requests. At the moment, supports only
* `rest`. *Advanced usage*: Additionally, it is possible to pass in an already
* `grpc`. *Advanced usage*: Additionally, it is possible to pass in an already
* instantiated {@see \Google\ApiCore\Transport\TransportInterface} object. Note
* that when this object is provided, any settings in $transportConfig, and any
* $apiEndpoint setting, will be ignored.
Expand All @@ -122,9 +122,9 @@ private static function supportedTransports()
* each supported transport type should be passed in a key for that transport. For
* example:
* $transportConfig = [
* 'rest' => [...],
* 'grpc' => [...],
* ];
* See the {@see \Google\ApiCore\Transport\RestTransport::build()} method for the
* See the {@see \Google\ApiCore\Transport\GrpcTransport::build()} method for the
* supported options.
* @type callable $clientCertSource
* A callable which returns the client cert as a string. This can be used to
Expand Down

0 comments on commit 573609c

Please sign in to comment.