-
Notifications
You must be signed in to change notification settings - Fork 463
fix(spanner): do not drop Spanner and GAX options on Database and Transaction methods #9381
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
base: main
Are you sure you want to change the base?
Changes from all commits
bba16a0
5c099f0
c7e2ee6
b785bed
711d7a6
3ae10c6
c27cfae
f857d44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,4 +106,56 @@ public function validateOptions(array $options, array|Message|string ...$optionT | |
|
|
||
| return $splitOptions; | ||
| } | ||
|
|
||
| /** | ||
| * @var array | ||
| */ | ||
| private static array $allowedKeysCache = []; | ||
|
|
||
| /** | ||
| * Filter an array of options to only include those matching the supplied `$optionTypes`. | ||
| * | ||
| * @param array $options | ||
| * @param array|Message|string ...$optionTypes | ||
| * @return array | ||
| */ | ||
| public function stripUnknownOptions(array $options, array|Message|string ...$optionTypes): array | ||
| { | ||
| $cacheKey = serialize(array_map(function ($type) { | ||
| return is_object($type) ? get_class($type) : $type; | ||
| }, $optionTypes)); | ||
|
|
||
| if (isset(self::$allowedKeysCache[$cacheKey])) { | ||
| return array_intersect_key($options, array_flip(self::$allowedKeysCache[$cacheKey])); | ||
| } | ||
|
|
||
| $allowedKeys = []; | ||
| foreach ($optionTypes as $optionType) { | ||
| if (is_array($optionType)) { | ||
| $allowedKeys = array_merge($allowedKeys, $optionType); | ||
| } elseif ($optionType === CallOptions::class) { | ||
| $callOptionKeys = array_keys((new CallOptions([]))->toArray()); | ||
| $allowedKeys = array_merge($allowedKeys, $callOptionKeys); | ||
| } elseif ($optionType instanceof Message | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should only support message class name in this case. There's no reason to provide a message instance (as there is for |
||
| || (is_string($optionType) && is_subclass_of($optionType, Message::class)) | ||
| ) { | ||
| $messageKeys = array_map( | ||
| fn ($method) => lcfirst(substr($method, 3)), | ||
| array_filter( | ||
| get_class_methods($optionType), | ||
| fn ($m) => 0 === strpos($m, 'get') | ||
| ) | ||
| ); | ||
| $allowedKeys = array_merge($allowedKeys, $messageKeys); | ||
| } elseif (is_string($optionType)) { | ||
| $allowedKeys[] = $optionType; | ||
|
Comment on lines
+150
to
+151
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit but we probably don't need this... we can just say scalars should always be |
||
| } else { | ||
| throw new LogicException(sprintf('Invalid option type: %s', $optionType)); | ||
| } | ||
| } | ||
|
|
||
| self::$allowedKeysCache[$cacheKey] = $allowedKeys; | ||
|
|
||
| return array_intersect_key($options, array_flip($allowedKeys)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2025 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Core\Tests\Unit; | ||
|
|
||
| use Google\Protobuf\Internal\Message; | ||
|
|
||
| /** | ||
| * A dummy message for testing OptionsValidator message extraction. | ||
| */ | ||
| class DummyMessage extends Message | ||
| { | ||
| /** | ||
| * @return void | ||
| */ | ||
| public function getQueryOptions() | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @return void | ||
| */ | ||
| public function getRequestOptions() | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2025 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Core\Tests\Unit; | ||
|
|
||
| use Google\ApiCore\Options\CallOptions; | ||
| use Google\Cloud\Core\OptionsValidator; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * @group core | ||
| */ | ||
| class OptionsValidatorTest extends TestCase | ||
| { | ||
| private $validator; | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| $this->validator = new OptionsValidator(); | ||
| } | ||
|
|
||
| public function testStripUnknownOptions() | ||
| { | ||
| $options = [ | ||
| 'parameters' => ['a' => 1], | ||
| 'queryOptions' => ['optimizerVersion' => '1'], | ||
| 'requestOptions' => ['priority' => 1], | ||
| 'timeoutMillis' => 100, | ||
| 'unknown' => 'strip me' | ||
| ]; | ||
|
|
||
| $stripped = $this->validator->stripUnknownOptions( | ||
| $options, | ||
| ['parameters'], | ||
| CallOptions::class, | ||
| DummyMessage::class | ||
| ); | ||
|
|
||
| $this->assertArrayHasKey('parameters', $stripped); | ||
| $this->assertArrayHasKey('queryOptions', $stripped); // From DummyMessage | ||
| $this->assertArrayHasKey('requestOptions', $stripped); // From DummyMessage | ||
| $this->assertArrayHasKey('timeoutMillis', $stripped); // From CallOptions | ||
| $this->assertArrayNotHasKey('unknown', $stripped); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be at the top of the class between
use ArrayTrait;and__construct