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

feat: Add union type to support the ClientOptions class in the client constructor #725

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion src/Generation/GapicClientV2Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private function generateImpl(): PhpFile
{
// TODO(vNext): Remove the forced addition of these `use` clauses.
$this->ctx->type(Type::fromName(\Google\ApiCore\PathTemplate::class));
$this->ctx->type(Type::fromName(\Google\ApiCore\Options\ClientOptions::class));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably go in private function construct(), to keep it together with the function it goes with below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see what you mean, my thought was to match 1 to 1 how does this gets placed in the generated file. For example, due finding Google\ApiCore\PathTemplate on the generated file, I was able to determine how this gets added.

Still, I am not completely opposed to moving it to another place.

$this->ctx->type(Type::fromName(RequestParamsHeaderDescriptor::class));
$this->ctx->type(Type::fromName(RetrySettings::class));
if ($this->serviceDetails->hasLro) {
Expand Down Expand Up @@ -561,7 +562,14 @@ private function construct(): PhpClassMember
$buildClientOptions = AST::method('buildClientOptions');
$setClientOptions = AST::method('setClientOptions');
$options = AST::var('options');
$optionsParam = AST::param(ResolvedType::array(), $options, AST::array([]));
$optionsParam = AST::param(
ResolvedType::union(
Type::array(),
Type::fromName(\Google\ApiCore\Options\ClientOptions::class)
),
$options,
AST::array([])
);
$clientOptions = AST::var('clientOptions');
$transportType = $this->serviceDetails->transportType;

Expand Down
16 changes: 16 additions & 0 deletions src/Utils/ResolvedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ public static function self(): ResolvedType
return new ResolvedType(Type::self(), fn () => 'self');
}

/**
* The 'union' built-in type for multiple types
*
* @return ResolvedType
*/
public static function union(Type ...$types): ResolvedType
{
return new ResolvedType(
Type::union(...$types),
fn () => implode(
'|',
array_map(fn (Type $type) => $type->name, $types)
)
Hectorhammett marked this conversation as resolved.
Show resolved Hide resolved
);
}

/**
* Construct a ResolvedType.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Utils/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ public static function stdClass(): Type
return new Type(Vector::new([]), 'stdClass');
}

/** Combines multiple types into a single union type */
public static function union(Type ...$types): Type
{
return new Type(
null,
implode(
'|',
array_map(fn (Type $type) => $type->name, $types)
)
);
}

/** An array of the specified type, for PhpDoc use only. */
public static function arrayOf(Type $elementType): Type
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\OperationResponse;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\PagedListResponse;
use Google\ApiCore\ResourceHelperTrait;
use Google\ApiCore\RetrySettings;
Expand Down Expand Up @@ -282,7 +283,7 @@ public static function parseName(string $formattedName, string $template = null)
/**
* Constructor.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -333,7 +334,7 @@ public static function parseName(string $formattedName, string $template = null)
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$clientOptions = $this->buildClientOptions($options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As buildClientOptions doesn't support ClientOptions, we'll need to update this in GAX (this is somewhat handled in googleapis/gax-php#580, but should probably be separated from the changes to make the properties public).

$this->setClientOptions($clientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\OperationResponse;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\PagedListResponse;
use Google\ApiCore\ResourceHelperTrait;
use Google\ApiCore\RetrySettings;
Expand Down Expand Up @@ -259,7 +260,7 @@ public static function parseName(string $formattedName, string $template = null)
/**
* Constructor.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -307,7 +308,7 @@ public static function parseName(string $formattedName, string $template = null)
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\LongRunning\OperationsClient;
use Google\ApiCore\OperationResponse;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\PagedListResponse;
use Google\ApiCore\ResourceHelperTrait;
use Google\ApiCore\RetrySettings;
Expand Down Expand Up @@ -1248,7 +1249,7 @@ public static function parseName(string $formattedName, string $template = null)
/**
* Constructor.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -1299,7 +1300,7 @@ public static function parseName(string $formattedName, string $template = null)
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Google\ApiCore\InsecureCredentialsWrapper;
use Google\ApiCore\LongRunning\OperationsClient;
use Google\ApiCore\OperationResponse;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\PagedListResponse;
use Google\ApiCore\ResourceHelperTrait;
use Google\ApiCore\RetrySettings;
Expand Down Expand Up @@ -323,7 +324,7 @@ public static function parseName(string $formattedName, string $template = null)
* the API Endpoint to the value specified in the variable, as well as ensure that
* empty credentials are used in the transport layer.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -374,7 +375,7 @@ public static function parseName(string $formattedName, string $template = null)
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$options = $this->setDefaultEmulatorConfig($options);
$clientOptions = $this->buildClientOptions($options);
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/ProtoTests/Basic/out/src/Client/BasicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\InsecureCredentialsWrapper;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\RetrySettings;
use Google\ApiCore\Transport\TransportInterface;
use Google\ApiCore\ValidationException;
Expand Down Expand Up @@ -98,7 +99,7 @@ private static function getClientDefaults()
* the API Endpoint to the value specified in the variable, as well as ensure that
* empty credentials are used in the transport layer.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -149,7 +150,7 @@ private static function getClientDefaults()
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$options = $this->setDefaultEmulatorConfig($options);
$clientOptions = $this->buildClientOptions($options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\Transport\TransportInterface;
use Google\ApiCore\ValidationException;
use Google\Auth\FetchAuthTokenInterface;
Expand Down Expand Up @@ -81,7 +82,7 @@ private static function supportedTransports()
/**
* Constructor.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -129,7 +130,7 @@ private static function supportedTransports()
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Google\ApiCore\ApiException;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\RetrySettings;
use Google\ApiCore\Transport\TransportInterface;
use Google\ApiCore\ValidationException;
Expand Down Expand Up @@ -87,7 +88,7 @@ private static function getClientDefaults()
/**
* Constructor.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -138,7 +139,7 @@ private static function getClientDefaults()
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\OperationResponse;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\RetrySettings;
use Google\ApiCore\Transport\TransportInterface;
use Google\ApiCore\ValidationException;
Expand Down Expand Up @@ -174,7 +175,7 @@ private function createOperationsClient(array $options)
/**
* Constructor.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -222,7 +223,7 @@ private function createOperationsClient(array $options)
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Google\ApiCore\ApiException;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\RetrySettings;
use Google\ApiCore\Transport\TransportInterface;
use Google\ApiCore\ValidationException;
Expand Down Expand Up @@ -105,7 +106,7 @@ private static function supportedTransports()
/**
* Constructor.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -153,7 +154,7 @@ private static function supportedTransports()
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Google\ApiCore\ApiException;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\ResourceHelperTrait;
use Google\ApiCore\RetrySettings;
use Google\ApiCore\Transport\TransportInterface;
Expand Down Expand Up @@ -436,7 +437,7 @@ public static function parseName(string $formattedName, string $template = null)
/**
* Constructor.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -487,7 +488,7 @@ public static function parseName(string $formattedName, string $template = null)
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Google\ApiCore\ApiException;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\Options\ClientOptions;
use Google\ApiCore\RetrySettings;
use Google\ApiCore\Transport\TransportInterface;
use Google\ApiCore\ValidationException;
Expand Down Expand Up @@ -97,7 +98,7 @@ private static function getClientDefaults()
/**
* Constructor.
*
* @param array $options {
* @param array|ClientOptions $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -148,7 +149,7 @@ private static function getClientDefaults()
*
* @throws ValidationException
*/
public function __construct(array $options = [])
public function __construct(array|ClientOptions $options = [])
{
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
Expand Down
Loading