Skip to content

Commit

Permalink
Add Snapshot and Seek support to Cloud Pub/Sub (#428)
Browse files Browse the repository at this point in the history
* Add Snapshot and Seek support to Cloud Pub/Sub

* Add rest support for snapshot and seek

* Add unit test coverage

* Add snippet tests

* Update pubsub table of contents

* Add snapshot system test

* Address code review
  • Loading branch information
jdpedrie authored Apr 7, 2017
1 parent e783a84 commit a16ccf1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
18 changes: 18 additions & 0 deletions GrpcTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,22 @@ private function formatValueForApi($value)
return ['list_value' => $this->formatListForApi($value)];
}
}

/**
* Format a timestamp for the API with nanosecond precision.
*
* @param string $value
* @return array
*/
private function formatTimestampForApi($value)
{
preg_match('/\.(\d{1,9})Z/', $value, $matches);
$value = preg_replace('/\.(\d{1,9})Z/', '.000000Z', $value);
$dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', $value);
$nanos = (isset($matches[1])) ? $matches[1] : 0;
return [
'seconds' => (int)$dt->format('U'),
'nanos' => (int)$nanos
];
}
}
12 changes: 10 additions & 2 deletions PhpArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ class PhpArray extends Protobuf\Codec\PhpArray
*/
private $customFilters;

/**
* @var bool
*/
private $useCamelCase;

/**
* @param array $customFilters A set of callbacks to apply to properties in
* a gRPC response.
* @param bool $useCamelCase Whether to convert key casing to camelCase.
* }
*/
public function __construct(array $customFilters = [])
public function __construct(array $customFilters = [], $useCamelCase = true)
{
$this->customFilters = $customFilters;
$this->useCamelCase = $useCamelCase;
}

/**
Expand Down Expand Up @@ -96,7 +104,7 @@ protected function encodeMessage(Protobuf\Message $message)
$v = $this->filterValue($v, $field);
}

$key = $this->toCamelCase($key);
$key = ($this->useCamelCase) ? $this->toCamelCase($key) : $key;

if (isset($this->customFilters[$key])) {
$v = call_user_func($this->customFilters[$key], $v);
Expand Down

0 comments on commit a16ccf1

Please sign in to comment.