Skip to content

Commit

Permalink
chore(Core, Spanner): Add removeObsoleteArgs helper method and use …
Browse files Browse the repository at this point in the history
…in Spanner
  • Loading branch information
yash30201 committed May 7, 2024
1 parent fad4022 commit 84a4411
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
13 changes: 13 additions & 0 deletions Core/src/ApiHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,17 @@ private function splitOptionalArgs(array $input, array $extraAllowedKeys = []) :

return [$input, $optionalArgs];
}

/**
* Helper method to remove args already processed by request proto (thus obsolete).
*
* @return array
*/
private function removeObsoleteArgs(array $input, array $keys = []): array
{
foreach ($keys as $key) {
if (array_key_exists($key, $input)) unset($input[$key]);
}
return $input;
}
}
7 changes: 7 additions & 0 deletions Core/tests/Unit/ApiHelperTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ public function testUnpackValue($expected, $value)
$this->assertEquals($expected, $this->implementation->unpackValue($value));
}

public function testRemoveObsoleteArgs()
{
$input = ['a' => 1, 'b' => 2, 'c' => 3];
$res = $this->implementation->removeObsoleteArgs($input, ['b', 'c']);
$this->assertEquals(['a' => 1], $res);
}

public function unpackValueProvider()
{
return [
Expand Down
1 change: 1 addition & 0 deletions Core/tests/Unit/Stubs/ApiHelpersTraitImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ class ApiHelpersTraitImpl
formatDurationForApi as public;
formatValueForApi as public;
unpackValue as public;
removeObsoleteArgs as public;
}
}
36 changes: 16 additions & 20 deletions Datastore/src/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ public function beginTransaction($transactionOptions, array $options = [])
'transactionOptions' => $transactionOptions,
];

$data = $this->convertDataToProtos($data, ['transactionOptions' => TransactionOptions::class]);
$request = $this->serializer->decodeMessage(new BeginTransactionRequest(), $data);

$res = $this->requestHandler->sendRequest(
Expand Down Expand Up @@ -455,19 +454,17 @@ public function lookup(array $keys, array $options = [])
$serviceKeys[] = $key->keyObject();
});

list($data, $optionalArgs) = $this->splitOptionalArgs($options, [
'transaction',
'className',
'sort',
'readTime',
'readConsistency'
]);
list($data, $optionalArgs) = $this->splitOptionalArgs($options);
$data += $this->readOptions($options) + [
'projectId' => $this->projectId,
'databaseId' => $this->databaseId,
'keys' => $this->keysList($serviceKeys),
];

$data = $this->removeObsoleteArgs($data, [
'transaction', 'className', 'sort', 'readTime', 'readConsistency'
]);

$request = $this->serializer->decodeMessage(new LookupRequest(), $data);

$res = $this->requestHandler->sendRequest(
Expand Down Expand Up @@ -586,13 +583,12 @@ public function runQuery(QueryInterface $query, array $options = [])
$runQueryObj->queryKey() => $requestQueryArr,
] + $this->readOptions($options) + $options;

list($data, $optionalArgs) = $this->splitOptionalArgs($req, [
'className',
'namespaceId',
'readTime',
'readConsistency',
'transaction'
list($data, $optionalArgs) = $this->splitOptionalArgs($req);

$data = $this->removeObsoleteArgs($data, [
'className', 'namespaceId', 'readTime', 'readConsistency', 'transaction'
]);

if (isset($data['query'])) {
$data['query'] = $this->parseQuery($data['query']);
}
Expand Down Expand Up @@ -680,13 +676,12 @@ public function runAggregationQuery(AggregationQuery $runQueryObj, array $option
),
] + $requestQueryArr + $this->readOptions($options) + $options;

list($data, $optionalArgs) = $this->splitOptionalArgs($req, [
'namespaceId',
'readTime',
'readConsistency',
'transaction'
$req = $this->removeObsoleteArgs($req, [
'namespaceId', 'readTime', 'readConsistency', 'transaction'
]);

list($data, $optionalArgs) = $this->splitOptionalArgs($req);

if (isset($data['aggregationQuery'])) {
if (isset($data['aggregationQuery']['nestedQuery'])) {
$data['aggregationQuery']['nestedQuery'] = $this->parseQuery(
Expand Down Expand Up @@ -765,7 +760,8 @@ public function commit(array $mutations, array $options = [])
// Remove 'transaction' if set to `null` to avoid serialization error
unset($options['transaction']);
}
list($data, $optionalArgs) = $this->splitOptionalArgs($options, ['allowOverwrite', 'baseVersion']);
list($data, $optionalArgs) = $this->splitOptionalArgs($options);
$data = $this->removeObsoleteArgs($data, ['allowOverwrite', 'baseVersion']);
$request = $this->serializer->decodeMessage(new CommitRequest(), $data);
$res = $this->requestHandler->sendRequest(
DatastoreClient::class,
Expand Down

0 comments on commit 84a4411

Please sign in to comment.