Skip to content

Commit 5099c41

Browse files
feat: Add union type to support the ClientOptions class in the client constructor (#725)
1 parent f7ed14f commit 5099c41

File tree

17 files changed

+79
-30
lines changed

17 files changed

+79
-30
lines changed

src/Generation/GapicClientV2Generator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ private function generateImpl(): PhpFile
7373
{
7474
// TODO(vNext): Remove the forced addition of these `use` clauses.
7575
$this->ctx->type(Type::fromName(\Google\ApiCore\PathTemplate::class));
76+
$this->ctx->type(Type::fromName(\Google\ApiCore\Options\ClientOptions::class));
7677
$this->ctx->type(Type::fromName(RequestParamsHeaderDescriptor::class));
7778
$this->ctx->type(Type::fromName(RetrySettings::class));
7879
if ($this->serviceDetails->hasLro) {
@@ -561,7 +562,14 @@ private function construct(): PhpClassMember
561562
$buildClientOptions = AST::method('buildClientOptions');
562563
$setClientOptions = AST::method('setClientOptions');
563564
$options = AST::var('options');
564-
$optionsParam = AST::param(ResolvedType::array(), $options, AST::array([]));
565+
$optionsParam = AST::param(
566+
ResolvedType::union(
567+
Type::array(),
568+
Type::fromName(\Google\ApiCore\Options\ClientOptions::class)
569+
),
570+
$options,
571+
AST::array([])
572+
);
565573
$clientOptions = AST::var('clientOptions');
566574
$transportType = $this->serviceDetails->transportType;
567575

src/Utils/ResolvedType.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function self(): ResolvedType
6767
}
6868

6969
/**
70-
* The 'union' built-in type for multiple types
70+
* The 'generic' built-in type for multiple types
7171
*
7272
* @return ResolvedType
7373
*/
@@ -81,6 +81,24 @@ public static function generic(Type $genericType, Type $typeArgument): ResolvedT
8181
);
8282
}
8383

84+
/**
85+
* The 'union' built-in type for multiple types
86+
*
87+
* @return ResolvedType
88+
*/
89+
public static function union(Type ...$types): ResolvedType
90+
{
91+
$typesString = implode(
92+
'|',
93+
array_map(fn (Type $type) => $type->name, $types)
94+
);
95+
96+
return new ResolvedType(
97+
Type::union($typesString),
98+
fn () => $typesString
99+
);
100+
}
101+
84102
/**
85103
* Construct a ResolvedType.
86104
*

src/Utils/Type.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ public static function stdClass(): Type
100100
return new Type(Vector::new([]), 'stdClass');
101101
}
102102

103+
/** Combines multiple types into a single union type */
104+
public static function union(string $types): Type
105+
{
106+
return new Type(
107+
null,
108+
$types
109+
);
110+
}
111+
103112
/** An array of the specified type, for PhpDoc use only. */
104113
public static function arrayOf(Type $elementType): Type
105114
{

tests/Integration/goldens/functions/src/V1/Client/CloudFunctionsServiceClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Google\ApiCore\CredentialsWrapper;
2929
use Google\ApiCore\GapicClientTrait;
3030
use Google\ApiCore\OperationResponse;
31+
use Google\ApiCore\Options\ClientOptions;
3132
use Google\ApiCore\PagedListResponse;
3233
use Google\ApiCore\ResourceHelperTrait;
3334
use Google\ApiCore\RetrySettings;
@@ -283,7 +284,7 @@ public static function parseName(string $formattedName, ?string $template = null
283284
/**
284285
* Constructor.
285286
*
286-
* @param array $options {
287+
* @param array|ClientOptions $options {
287288
* Optional. Options for configuring the service API wrapper.
288289
*
289290
* @type string $apiEndpoint
@@ -345,7 +346,7 @@ public static function parseName(string $formattedName, ?string $template = null
345346
*
346347
* @throws ValidationException
347348
*/
348-
public function __construct(array $options = [])
349+
public function __construct(array|ClientOptions $options = [])
349350
{
350351
$clientOptions = $this->buildClientOptions($options);
351352
$this->setClientOptions($clientOptions);

tests/Integration/goldens/redis/src/V1/Client/CloudRedisClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Google\ApiCore\CredentialsWrapper;
2929
use Google\ApiCore\GapicClientTrait;
3030
use Google\ApiCore\OperationResponse;
31+
use Google\ApiCore\Options\ClientOptions;
3132
use Google\ApiCore\PagedListResponse;
3233
use Google\ApiCore\ResourceHelperTrait;
3334
use Google\ApiCore\RetrySettings;
@@ -260,7 +261,7 @@ public static function parseName(string $formattedName, ?string $template = null
260261
/**
261262
* Constructor.
262263
*
263-
* @param array $options {
264+
* @param array|ClientOptions $options {
264265
* Optional. Options for configuring the service API wrapper.
265266
*
266267
* @type string $apiEndpoint
@@ -319,7 +320,7 @@ public static function parseName(string $formattedName, ?string $template = null
319320
*
320321
* @throws ValidationException
321322
*/
322-
public function __construct(array $options = [])
323+
public function __construct(array|ClientOptions $options = [])
323324
{
324325
$clientOptions = $this->buildClientOptions($options);
325326
$this->setClientOptions($clientOptions);

tests/Integration/goldens/securitycenter/src/V1/Client/SecurityCenterClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Google\ApiCore\GapicClientTrait;
3030
use Google\ApiCore\LongRunning\OperationsClient;
3131
use Google\ApiCore\OperationResponse;
32+
use Google\ApiCore\Options\ClientOptions;
3233
use Google\ApiCore\PagedListResponse;
3334
use Google\ApiCore\ResourceHelperTrait;
3435
use Google\ApiCore\RetrySettings;
@@ -1251,7 +1252,7 @@ public static function parseName(string $formattedName, ?string $template = null
12511252
/**
12521253
* Constructor.
12531254
*
1254-
* @param array $options {
1255+
* @param array|ClientOptions $options {
12551256
* Optional. Options for configuring the service API wrapper.
12561257
*
12571258
* @type string $apiEndpoint
@@ -1313,7 +1314,7 @@ public static function parseName(string $formattedName, ?string $template = null
13131314
*
13141315
* @throws ValidationException
13151316
*/
1316-
public function __construct(array $options = [])
1317+
public function __construct(array|ClientOptions $options = [])
13171318
{
13181319
$clientOptions = $this->buildClientOptions($options);
13191320
$this->setClientOptions($clientOptions);

tests/Integration/goldens/spanner/src/V1/Client/DatabaseAdminClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Google\ApiCore\InsecureCredentialsWrapper;
3131
use Google\ApiCore\LongRunning\OperationsClient;
3232
use Google\ApiCore\OperationResponse;
33+
use Google\ApiCore\Options\ClientOptions;
3334
use Google\ApiCore\PagedListResponse;
3435
use Google\ApiCore\ResourceHelperTrait;
3536
use Google\ApiCore\RetrySettings;
@@ -324,7 +325,7 @@ public static function parseName(string $formattedName, ?string $template = null
324325
* the API Endpoint to the value specified in the variable, as well as ensure that
325326
* empty credentials are used in the transport layer.
326327
*
327-
* @param array $options {
328+
* @param array|ClientOptions $options {
328329
* Optional. Options for configuring the service API wrapper.
329330
*
330331
* @type string $apiEndpoint
@@ -386,7 +387,7 @@ public static function parseName(string $formattedName, ?string $template = null
386387
*
387388
* @throws ValidationException
388389
*/
389-
public function __construct(array $options = [])
390+
public function __construct(array|ClientOptions $options = [])
390391
{
391392
$options = $this->setDefaultEmulatorConfig($options);
392393
$clientOptions = $this->buildClientOptions($options);

tests/Unit/ProtoTests/Basic/out/src/Client/BasicClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Google\ApiCore\CredentialsWrapper;
2929
use Google\ApiCore\GapicClientTrait;
3030
use Google\ApiCore\InsecureCredentialsWrapper;
31+
use Google\ApiCore\Options\ClientOptions;
3132
use Google\ApiCore\RetrySettings;
3233
use Google\ApiCore\Transport\TransportInterface;
3334
use Google\ApiCore\ValidationException;
@@ -99,7 +100,7 @@ private static function getClientDefaults()
99100
* the API Endpoint to the value specified in the variable, as well as ensure that
100101
* empty credentials are used in the transport layer.
101102
*
102-
* @param array $options {
103+
* @param array|ClientOptions $options {
103104
* Optional. Options for configuring the service API wrapper.
104105
*
105106
* @type string $apiEndpoint
@@ -161,7 +162,7 @@ private static function getClientDefaults()
161162
*
162163
* @throws ValidationException
163164
*/
164-
public function __construct(array $options = [])
165+
public function __construct(array|ClientOptions $options = [])
165166
{
166167
$options = $this->setDefaultEmulatorConfig($options);
167168
$clientOptions = $this->buildClientOptions($options);

tests/Unit/ProtoTests/BasicExplicitPaginated/out/src/Client/BasicExplicitPaginatedClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Google\ApiCore\ApiException;
2828
use Google\ApiCore\CredentialsWrapper;
2929
use Google\ApiCore\GapicClientTrait;
30+
use Google\ApiCore\Options\ClientOptions;
3031
use Google\ApiCore\PagedListResponse;
3132
use Google\ApiCore\RetrySettings;
3233
use Google\ApiCore\Transport\TransportInterface;
@@ -88,7 +89,7 @@ private static function getClientDefaults()
8889
/**
8990
* Constructor.
9091
*
91-
* @param array $options {
92+
* @param array|ClientOptions $options {
9293
* Optional. Options for configuring the service API wrapper.
9394
*
9495
* @type string $apiEndpoint
@@ -150,7 +151,7 @@ private static function getClientDefaults()
150151
*
151152
* @throws ValidationException
152153
*/
153-
public function __construct(array $options = [])
154+
public function __construct(array|ClientOptions $options = [])
154155
{
155156
$clientOptions = $this->buildClientOptions($options);
156157
$this->setClientOptions($clientOptions);

tests/Unit/ProtoTests/BasicGrpcOnly/out/src/Client/BasicGrpcOnlyClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use Google\ApiCore\CredentialsWrapper;
2828
use Google\ApiCore\GapicClientTrait;
29+
use Google\ApiCore\Options\ClientOptions;
2930
use Google\ApiCore\Transport\TransportInterface;
3031
use Google\ApiCore\ValidationException;
3132
use Google\Auth\FetchAuthTokenInterface;
@@ -82,7 +83,7 @@ private static function supportedTransports()
8283
/**
8384
* Constructor.
8485
*
85-
* @param array $options {
86+
* @param array|ClientOptions $options {
8687
* Optional. Options for configuring the service API wrapper.
8788
*
8889
* @type string $apiEndpoint
@@ -141,7 +142,7 @@ private static function supportedTransports()
141142
*
142143
* @throws ValidationException
143144
*/
144-
public function __construct(array $options = [])
145+
public function __construct(array|ClientOptions $options = [])
145146
{
146147
$clientOptions = $this->buildClientOptions($options);
147148
$this->setClientOptions($clientOptions);

0 commit comments

Comments
 (0)