Skip to content

Commit 05cdfb4

Browse files
committed
add validateOptions for options arrays
1 parent a5c7c74 commit 05cdfb4

17 files changed

+450
-483
lines changed

Core/src/ApiHelperTrait.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
use Google\ApiCore\ArrayTrait;
2121
use Google\ApiCore\Options\CallOptions;
22-
use Google\Protobuf\NullValue;
2322
use Google\Protobuf\Internal\Message;
23+
use Google\Protobuf\NullValue;
2424

2525
/**
2626
* @internal
@@ -272,25 +272,28 @@ private function splitOptionalArgs(array $input, array $extraAllowedKeys = []):
272272
* the CallOptions classname. Parameters are split and returned in the order
273273
* that the options types are provided.
274274
*/
275-
private function validateOptions(array $options, array|string ...$optionTypes): array
275+
private function validateOptions(array $options, array|Message|string ...$optionTypes): array
276276
{
277277
$splitOptions = [];
278278
foreach ($optionTypes as $optionType) {
279279
if (is_array($optionType)) {
280280
$splitOptions[] = $this->pluckArray($optionType, $options);
281-
} elseif (is_string($optionType)) {
282-
if (is_subclass_of($optionType, Message::class)) {
281+
} else {
282+
if ($optionType === CallOptions::class) {
283+
$callOptionKeys = array_keys((new CallOptions([]))->toArray());
284+
$splitOptions[] = $this->pluckArray($callOptionKeys, $options);
285+
} else {
283286
$messageKeys = array_map(
284287
fn ($method) => lcfirst(substr($method, 3)),
285288
array_filter(
286289
get_class_methods($optionType),
287290
fn ($m) => 0 === strpos($m, 'get')
288291
)
289292
);
290-
$splitOptions[] = $this->pluckArray($messageKeys, $options);
291-
} elseif ($optionType === CallOptions::class) {
292-
$callOptionKeys = array_keys((new CallOptions([]))->toArray());
293-
$splitOptions[] = $this->pluckArray($callOptionKeys, $options);
293+
$messageOptions = $this->pluckArray($messageKeys, $options);
294+
$splitOptions[] = $optionType instanceof Message
295+
? $this->serializer->decodeMessage($optionType, $messageOptions)
296+
: $messageOptions;
294297
}
295298
}
296299
}

Core/tests/Unit/ApiHelperTraitTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
namespace Google\Cloud\Core\Tests\Unit;
1919

2020
use Google\ApiCore\Options\CallOptions;
21+
use Google\ApiCore\Serializer;
22+
use Google\ApiCore\Testing\MockRequest;
2123
use Google\Cloud\Core\Duration;
2224
use Google\Cloud\Core\Testing\GrpcTestTrait;
2325
use Google\Cloud\Core\Tests\Unit\Stubs\ApiHelpersTraitImpl;
24-
use Google\Cloud\Core\Tests\Unit\Stubs\TestMessage;
2526
use PHPUnit\Framework\TestCase;
2627
use Prophecy\PhpUnit\ProphecyTrait;
2728

@@ -38,6 +39,7 @@ class ApiHelperTraitTest extends TestCase
3839
public function setUp(): void
3940
{
4041
$this->implementation = new ApiHelpersTraitImpl();
42+
$this->implementation->serializer = new Serializer();
4143
}
4244

4345
public function testFormatsTimestamp()
@@ -294,18 +296,18 @@ public function validateOptionsProvider()
294296
],
295297
[
296298
[
297-
'baz' => 'bat',
299+
'pageToken' => 'bat',
298300
'qux' => 'quux',
299301
'timeoutMillis' => 123,
300302
],
301303
[
302304
CallOptions::class,
303-
TestMessage::class,
305+
MockRequest::class,
304306
['qux'],
305307
],
306308
[
307309
['timeoutMillis' => 123],
308-
['baz' => 'bat'],
310+
['pageToken' => 'bat'],
309311
['qux' => 'quux'],
310312
]
311313
],
@@ -315,7 +317,7 @@ public function validateOptionsProvider()
315317
],
316318
[
317319
['baz'],
318-
TestMessage::class,
320+
MockRequest::class,
319321
CallOptions::class,
320322
],
321323
[
@@ -324,6 +326,20 @@ public function validateOptionsProvider()
324326
[],
325327
]
326328
],
329+
[
330+
[
331+
'baz' => 'bat',
332+
'pageToken' => 'foo1',
333+
],
334+
[
335+
['baz'],
336+
new MockRequest(),
337+
],
338+
[
339+
['baz' => 'bat'],
340+
(new MockRequest())->setPageToken('foo1'),
341+
]
342+
],
327343
];
328344
}
329345

Core/tests/Unit/Stubs/ApiHelpersTraitImpl.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ class ApiHelpersTraitImpl
3333
unpackValue as public;
3434
validateOptions as public;
3535
}
36+
37+
public $serializer;
3638
}

Spanner/MIGRATING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,8 @@ $lro->pollUntilComplete();
113113
$lro->cancel();
114114
$lro->delete();
115115
```
116+
117+
### Removed Methods
118+
119+
- `Operation::createTransaction` => use `Operation::transaction` instead
120+
- `Operation::createSnapshot` => use `Operation::snapshot` instead

Spanner/src/Batch/BatchClient.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,20 @@ public function snapshotFromString($identifier)
219219

220220
$session = $this->operation->session($data['sessionName']);
221221

222-
/** @var BatchSnapshot */
223-
return $this->operation->createSnapshot($session, [
224-
'id' => $data['transactionId'],
225-
'readTimestamp' => $data['readTimestamp']
226-
], BatchSnapshot::class);
222+
if ($data['readTimestamp']) {
223+
if (!($data['readTimestamp'] instanceof Timestamp)) {
224+
$time = $this->parseTimeString($data['readTimestamp']);
225+
$data['readTimestamp'] = new Timestamp($time[0], $time[1]);
226+
}
227+
}
228+
return new BatchSnapshot(
229+
$this->operation,
230+
$session,
231+
[
232+
'id' => $data['transactionId'],
233+
'readTimestamp' => $data['readTimestamp']
234+
]
235+
);
227236
}
228237

229238
/**

Spanner/src/Database.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ public function transaction(array $options = []): Transaction
805805
throw new \BadMethodCallException('Nested transactions are not supported by this client.');
806806
}
807807

808-
$options['transactionOptions'] = $this->configureReadWriteTransactionOptions();
808+
$options['transactionOptions'] = $this->initReadWriteTransactionOptions();
809809

810810
$session = $this->selectSession(
811811
SessionPoolInterface::CONTEXT_READWRITE,
@@ -920,7 +920,7 @@ public function runTransaction(callable $operation, array $options = []): mixed
920920
$maxRetries = $retrySettings['maxRetries'];
921921
}
922922

923-
// There isn't anything configurable here.
923+
// Configure necessary readWrite nested and base options
924924
$options['transactionOptions'] = $this->configureReadWriteTransactionOptions(
925925
$options['transactionOptions'] ?? []
926926
);
@@ -932,7 +932,6 @@ public function runTransaction(callable $operation, array $options = []): mixed
932932

933933
$attempt = 0;
934934
$startTransactionFn = function ($session, $options) use (&$attempt) {
935-
936935
// Initial attempt requires to set `begin` options (ILB).
937936
if ($attempt === 0) {
938937
// Partitioned DML does not support ILB.

0 commit comments

Comments
 (0)