Skip to content

Commit a52f65c

Browse files
committed
feat!: Spanner V2
1 parent 1209965 commit a52f65c

File tree

159 files changed

+8943
-11946
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

+8943
-11946
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
@@ -260,8 +260,8 @@ private function splitOptionalArgs(array $input, array $extraAllowedKeys = []):
260260
$callOptionFields = array_keys((new CallOptions([]))->toArray());
261261
$keys = array_merge($callOptionFields, $extraAllowedKeys);
262262

263-
$optionalArgs = $this->pluckArray($keys, $input);
263+
$callOptions = $this->pluckArray($keys, $input);
264264

265-
return [$input, $optionalArgs];
265+
return [$input, $callOptions];
266266
}
267267
}

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: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Core\LongRunning;
19+
20+
use Google\ApiCore\OperationResponse;
21+
use Google\ApiCore\Serializer;
22+
use Google\Cloud\Core\RequestProcessorTrait;
23+
use Google\LongRunning\Operation;
24+
use Google\LongRunning\ListOperationsRequest;
25+
use Google\LongRunning\GetOperationRequest;
26+
use Google\LongRunning\CancelOperationRequest;
27+
use Google\LongRunning\DeleteOperationRequest;
28+
use Google\Protobuf\Any;
29+
30+
/**
31+
* Defines the calls required to manage Long Running Operations using a GAPIC
32+
* generated client.
33+
*
34+
* @internal
35+
*/
36+
class LongRunningGapicConnection implements LongRunningConnectionInterface
37+
{
38+
use RequestProcessorTrait;
39+
40+
public function __construct(
41+
private $gapicClient,
42+
private Serializer $serializer
43+
) {
44+
}
45+
46+
/**
47+
* @param array $args
48+
*/
49+
public function get(array $args)
50+
{
51+
$operationResponse = $this->gapicClient->resumeOperation($args['name']);
52+
53+
return $this->operationResponseToArray($operationResponse);
54+
}
55+
56+
/**
57+
* @param array $args
58+
*/
59+
public function cancel(array $args)
60+
{
61+
$operationResponse = $this->gapicClient->resumeOperation(
62+
$args['name'],
63+
$args['method'] ?? null
64+
);
65+
$operationResponse->cancel();
66+
67+
return $this->operationResponseToArray($operationResponse);
68+
}
69+
70+
/**
71+
* @param array $args
72+
*/
73+
public function delete(array $args)
74+
{
75+
$operationResponse = $this->gapicClient->resumeOperation(
76+
$args['name'],
77+
$args['method'] ?? null
78+
);
79+
$operationResponse->cancel();
80+
81+
return $this->operationResponseToArray($operationResponse);
82+
}
83+
84+
/**
85+
* @param array $args
86+
*/
87+
public function operations(array $args)
88+
{
89+
$request = ListOperationsRequest::build($args['name'], $args['filter'] ?? null);
90+
$response = $this->gapicClient->getOperationsClient()->listOperations($request);
91+
92+
return $this->handleResponse($response);
93+
}
94+
95+
private function operationResponseToArray(OperationResponse $operationResponse)
96+
{
97+
$response = $this->handleResponse($operationResponse->getLastProtoResponse());
98+
$metaType = $response['metadata']['typeUrl'];
99+
100+
// unpack result Any type
101+
$result = $operationResponse->getResult();
102+
if ($result instanceof Any) {
103+
// For some reason we aren't doing this in GAX OperationResponse (but we should)
104+
$result = $result->unpack();
105+
}
106+
$response['response'] = $this->handleResponse($result);
107+
108+
// unpack error Any type
109+
$response['error'] = $this->handleResponse($operationResponse->getError());
110+
111+
$metadata = $operationResponse->getMetadata();
112+
if ($metadata instanceof Any) {
113+
// For some reason we aren't doing this in GAX OperationResponse (but we should)
114+
$metadata = $metadata->unpack();
115+
}
116+
$response['metadata'] = $this->handleResponse($metadata);
117+
118+
// Used in LongRunningOperation to invoke callables
119+
$response['metadata'] += ['typeUrl' => $metaType];
120+
121+
return $response;
122+
}
123+
}

Core/src/LongRunning/LongRunningOperation.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,11 @@ public function reload(array $options = [])
252252

253253
$this->result = null;
254254
$this->error = null;
255-
if (isset($res['done']) && $res['done']) {
255+
256+
if (isset($res['done']) && $res['done'] && isset($res['metadata']['typeUrl'])) {
256257
$type = $res['metadata']['typeUrl'];
257258
$this->result = $this->executeDoneCallback($type, $res['response']);
258-
$this->error = (isset($res['error']))
259-
? $res['error']
260-
: null;
259+
$this->error = $res['error'] ?? null;
261260
}
262261

263262
return $this->info = $res;

Core/src/RequestHandler.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ class RequestHandler
4242
*/
4343
private Serializer $serializer;
4444

45-
private array $clients;
45+
private array $clients = [];
4646

4747
/**
4848
* @param Serializer $serializer
49-
* @param array $clientClasses
49+
* @param array<string|object> $clientClasses
5050
* @param array $clientConfig
5151
*/
5252
public function __construct(
@@ -75,9 +75,12 @@ public function __construct(
7575
//@codeCoverageIgnoreEnd
7676

7777
// Initialize the client classes and store them in memory
78-
$this->clients = [];
79-
foreach ($clientClasses as $className) {
80-
$this->clients[$className] = new $className($clientConfig);
78+
foreach ($clientClasses as $client) {
79+
if (is_object($client)) {
80+
$this->clients[get_class($client)] = $client;
81+
} else {
82+
$this->clients[$client] = new $client($clientConfig);
83+
}
8184
}
8285
}
8386

Core/src/RequestProcessorTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Google\Rpc\BadRequest;
2626
use Google\Rpc\Code;
2727
use Google\Rpc\RetryInfo;
28+
use GuzzleHttp\Promise\PromiseInterface;
2829

2930
/**
3031
* @internal

Core/src/ServiceBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function pubsub(array $config = [])
254254
*
255255
* Example:
256256
* ```
257-
* $spanner = $cloud->spanner();
257+
* $spanner = $cloud->spanner(['projectId' => 'my-project']);
258258
* ```
259259
*
260260
* @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()

0 commit comments

Comments
 (0)