Skip to content

Commit

Permalink
Enhance Commit API readability
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 committed May 7, 2024
1 parent dd24b76 commit 2de6d58
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions Datastore/src/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,32 +689,8 @@ public function commit(array $mutations, array $options = [])
'databaseId' => $this->databaseId,
];

foreach ($mutations as &$mutation) {
$mutationType = array_keys($mutation)[0];
$data = $mutation[$mutationType];
if (isset($data['properties'])) {
foreach ($data['properties'] as &$property) {
list($type, $val) = $this->toGrpcValue($property);

$property[$type] = $val;
}
}

$mutation[$mutationType] = $data;

$mutation = $this->serializer->decodeMessage(new Mutation(), $mutation);
}

$options += [
'mode' => ($options['transaction']) ? Mode::TRANSACTIONAL : Mode::NON_TRANSACTIONAL,
'mutations' => $mutations,
'projectId' => $this->projectId,
];
$options = $this->parseCommitOptions($mutations, $options);

if (is_null($options['transaction'])) {
// Remove 'transaction' if set to `null` to avoid serialization error
unset($options['transaction']);
}
list($data, $optionalArgs) = $this->splitOptionalArgs($options);

// Remove redundant keys for request.
Expand Down Expand Up @@ -1235,4 +1211,36 @@ private function parseRunAggregationQueryArgs($runQueryObj, $options)

return $result;
}

private function parseCommitOptions($mutations, $options)
{
foreach ($mutations as &$mutation) {
$mutationType = array_keys($mutation)[0];
$data = $mutation[$mutationType];
if (isset($data['properties'])) {
foreach ($data['properties'] as &$property) {
list($type, $val) = $this->toGrpcValue($property);

$property[$type] = $val;
}
}

$mutation[$mutationType] = $data;

$mutation = $this->serializer->decodeMessage(new Mutation(), $mutation);
}

$options += [
'mode' => ($options['transaction']) ? Mode::TRANSACTIONAL : Mode::NON_TRANSACTIONAL,
'mutations' => $mutations,
'projectId' => $this->projectId,
];

if (is_null($options['transaction'])) {
// Remove 'transaction' if set to `null` to avoid serialization error
unset($options['transaction']);
}

return $options;
}
}

0 comments on commit 2de6d58

Please sign in to comment.