Skip to content

Commit fd840b9

Browse files
committed
feat!: Spanner V2
1 parent eeba775 commit fd840b9

File tree

159 files changed

+8655
-19204
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+8655
-19204
lines changed

.github/workflows/system-tests-spanner-emulator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
- name: Install dependencies
5050
run: |
5151
# ensure composer uses local Core instead of pulling from packagist
52-
composer config repositories.local --json '{"type":"path", "url": "../Core", "options": {"versions": {"google/cloud-core": "1.100"}}}' -d Spanner
52+
# composer config repositories.local --json '{"type":"path", "url": "../Core", "options": {"versions": {"google/cloud-core": "1.100"}}}' -d Spanner
5353
composer update --prefer-dist --no-interaction --no-suggest -d Spanner/
5454
5555
- name: Run system tests

Core/src/ApiHelperTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ private function splitOptionalArgs(array $input, array $extraAllowedKeys = []) :
261261
$callOptionFields = array_keys((new CallOptions([]))->toArray());
262262
$keys = array_merge($callOptionFields, $extraAllowedKeys);
263263

264-
$optionalArgs = $this->pluckArray($keys, $input);
264+
$callOptions = $this->pluckArray($keys, $input);
265265

266-
return [$input, $optionalArgs];
266+
return [$input, $callOptions];
267267
}
268268
}

Core/src/Iam/Iam.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* use Google\Cloud\Spanner\SpannerClient;
3636
*
37-
* $spanner = new SpannerClient();
37+
* $spanner = new SpannerClient(['projectId' => 'my-project']);
3838
* $instance = $spanner->instance('my-new-instance');
3939
*
4040
* $iam = $instance->iam();
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/*
3+
* Copyright 2024 Google LLC
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are
8+
* met:
9+
*
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following disclaimer
14+
* in the documentation and/or other materials provided with the
15+
* distribution.
16+
* * Neither the name of Google Inc. nor the names of its
17+
* contributors may be used to endorse or promote products derived from
18+
* this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
namespace Google\Cloud\Core\Middleware;
34+
35+
use Google\ApiCore\ApiException;
36+
use Google\ApiCore\BidiStream;
37+
use Google\ApiCore\Call;
38+
use Google\ApiCore\ClientStream;
39+
use Google\ApiCore\Middleware\MiddlewareInterface;
40+
use Google\ApiCore\ServerStream;
41+
use Google\Cloud\Core\RequestProcessorTrait;
42+
use GuzzleHttp\Promise\PromiseInterface;
43+
use Throwable;
44+
45+
/**
46+
* Middleware that wraps any Api Exception to a `Google\Cloud\Core\Exception`
47+
* exception class. This is primarily to maintain backwards compatibility with
48+
* previous Spanner versions.
49+
*
50+
* @internal
51+
*/
52+
class ExceptionMiddleware implements MiddlewareInterface
53+
{
54+
use RequestProcessorTrait;
55+
56+
/** @var callable */
57+
private $nextHandler;
58+
59+
public function __construct(callable $nextHandler) {
60+
$this->nextHandler = $nextHandler;
61+
}
62+
63+
/**
64+
* @param Call $call
65+
* @param array $options
66+
*
67+
* @return PromiseInterface|ClientStream|ServerStream|BidiStream
68+
*/
69+
public function __invoke(Call $call, array $options)
70+
{
71+
$response = ($this->nextHandler)($call, $options);
72+
if ($response instanceof PromiseInterface) {
73+
return $response->then(null, function ($value) {
74+
if ($value instanceof \Google\ApiCore\ApiException) {
75+
throw $this->convertToGoogleException($value);
76+
}
77+
if ($value instanceof Throwable) {
78+
throw $value;
79+
}
80+
});
81+
}
82+
// this can also be a Stream
83+
return $response;
84+
}
85+
}
86+

Core/src/RequestHandler.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class RequestHandler
4545
*/
4646
private Serializer $serializer;
4747

48-
private array $clients;
48+
private array $clients = [];
4949

5050
/**
5151
* @param Serializer $serializer
52-
* @param array $clientClasses
52+
* @param array<string|object> $clientClasses
5353
* @param array $clientConfig
5454
*/
5555
public function __construct(
@@ -76,11 +76,14 @@ public function __construct(
7676
);
7777
}
7878
//@codeCoverageIgnoreEnd
79-
79+
8080
// Initialize the client classes and store them in memory
81-
$this->clients = [];
82-
foreach ($clientClasses as $className) {
83-
$this->clients[$className] = new $className($clientConfig);
81+
foreach ($clientClasses as $client) {
82+
if (is_object($client)) {
83+
$this->clients[get_class($client)] = $client;
84+
} else {
85+
$this->clients[$client] = new $client($clientConfig);
86+
}
8487
}
8588
}
8689

Core/src/ServiceBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public function pubsub(array $config = [])
249249
*
250250
* Example:
251251
* ```
252-
* $spanner = $cloud->spanner();
252+
* $spanner = $cloud->spanner(['projectId' => 'my-project']);
253253
* ```
254254
*
255255
* @param array $config [optional] {

Core/src/TimeTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ private function formatTimeAsString(\DateTimeInterface $dateTime, $ns)
9292
* $dateTime will be used instead.
9393
* @return array
9494
*/
95-
private function formatTimeAsArray(\DateTimeInterface $dateTime, $ns)
95+
private function formatTimeAsArray(\DateTimeInterface $dateTime, $ns = null)
9696
{
9797
if ($ns === null) {
98-
$ns = $dateTime->format('u');
98+
$ns = $this->convertFractionToNanoSeconds($dateTime->format('u'));
9999
}
100100
return [
101101
'seconds' => (int) $dateTime->format('U'),

Core/tests/Snippet/Iam/IamTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase;
2121
use Google\Cloud\Core\Iam\Iam;
22+
use Google\Cloud\Core\Iam\IamManager;
2223
use Google\Cloud\Core\Iam\IamConnectionInterface;
2324
use Google\Cloud\Core\Testing\TestHelpers;
2425
use Google\Cloud\Spanner\SpannerClient;
@@ -55,7 +56,7 @@ public function testClass()
5556
]);
5657
$res = $snippet->invoke('iam');
5758

58-
$this->assertInstanceOf(Iam::class, $res->returnVal());
59+
$this->assertInstanceOf(IamManager::class, $res->returnVal());
5960
}
6061

6162
public function testPolicy()

Core/tests/Unit/LongRunning/OperationResponseTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
namespace Google\Cloud\Core\Tests\Unit\LongRunning;
1919

2020
use Google\ApiCore\OperationResponse;
21+
use Google\ApiCore\Serializer;
2122
use Google\Cloud\Core\LongRunning\OperationResponseTrait;
2223
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2324
use Google\Cloud\Core\LongRunning\LongRunningConnectionInterface;
24-
use Google\ApiCore\Serializer;
2525
use Prophecy\Argument;
2626
use Google\Cloud\Audit\RequestMetadata;
2727
use Google\Cloud\Audit\AuthorizationInfo;

Core/tests/Unit/ServiceBuilderTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use Google\Cloud\Firestore\FirestoreClient;
2525
use Google\Cloud\Language\LanguageClient;
2626
use Google\Cloud\Logging\LoggingClient;
27-
use Google\Cloud\Spanner\SpannerClient;
2827
use Google\Cloud\Speech\SpeechClient;
2928
use Google\Cloud\Storage\StorageClient;
3029
use Google\Cloud\Core\Tests\Unit\Fixtures;
@@ -175,11 +174,6 @@ public function serviceProvider()
175174
], [
176175
'language',
177176
LanguageClient::class
178-
], [
179-
'spanner',
180-
SpannerClient::class,
181-
[],
182-
[$this, 'checkAndSkipGrpcTests']
183177
], [
184178
'speech',
185179
SpeechClient::class,

0 commit comments

Comments
 (0)