Skip to content

Commit

Permalink
feat: adding list API continued (#18)
Browse files Browse the repository at this point in the history
* feat: adding list API

* add test for large count param in list erase

* fix test assertion

* make sure all response type tests are notnull

Co-authored-by: Pete Gautier <pete@momentohq.com>
  • Loading branch information
pgautier404 and pgautier404 authored Oct 17, 2022
1 parent e570e35 commit c005907
Show file tree
Hide file tree
Showing 5 changed files with 792 additions and 15 deletions.
240 changes: 240 additions & 0 deletions src/Cache/CacheOperationTypes/CacheOperationTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use Cache_client\_GetResponse;
use Cache_client\_ListFetchResponse;
use Cache_client\_ListLengthResponse;
use Cache_client\_ListPopBackResponse;
use Cache_client\_ListPopFrontResponse;
use Control_client\_ListCachesResponse;
use Cache_client\_SetResponse;
use Momento\Cache\Errors\MomentoErrorCode;
Expand Down Expand Up @@ -48,6 +51,12 @@ public function message() : string
{
return $this->message;
}

public function __toString()
{
return parent::__toString() . ": " . $this->message;
}

}

abstract class ResponseBase
Expand Down Expand Up @@ -82,6 +91,11 @@ protected function isMiss() : bool
{
return get_class($this) == "{$this->baseType}Miss";
}

public function __toString()
{
return get_class($this);
}
}

abstract class CreateCacheResponse extends ResponseBase {
Expand Down Expand Up @@ -195,6 +209,12 @@ public function nextToken() : string
{
return $this->nextToken;
}

public function __toString()
{
$cacheNames = array_map(fn($i) => $i->name(), $this->caches);
return get_class($this) . ": " . join(', ', $cacheNames);
}
}

class ListCachesResponseError extends ListCachesResponse
Expand Down Expand Up @@ -241,6 +261,10 @@ public function value() : string {
return $this->value;
}

public function __toString()
{
return get_class($this) . ": key {$this->key} = {$this->value}";
}
}

class CacheSetResponseError extends CacheSetResponse
Expand Down Expand Up @@ -290,6 +314,10 @@ public function value() : string
return $this->value;
}

public function __toString()
{
return parent::__toString() . ": {$this->value}";
}
}

class CacheGetResponseMiss extends CacheGetResponse { }
Expand Down Expand Up @@ -358,6 +386,7 @@ class CacheListFetchResponseHit extends CacheListFetchResponse
{

private array $values = [];
private int $count;

public function __construct(_ListFetchResponse $response)
{
Expand All @@ -368,6 +397,7 @@ public function __construct(_ListFetchResponse $response)
{
$this->values[] = $value;
}
$this->count = count($this->values);
}
}

Expand All @@ -376,6 +406,10 @@ public function values() : array
return $this->values;
}

public function __toString()
{
return parent::__toString() . ": {$this->count} items";
}
}

class CacheListFetchResponseMiss extends CacheListFetchResponse { }
Expand Down Expand Up @@ -441,3 +475,209 @@ class CacheListPushBackResponseError extends CacheListPushBackResponse
use ErrorBody;
}

abstract class CacheListPopFrontResponse extends ResponseBase
{
public function asHit() : CacheListPopFrontResponseHit|null
{
if ($this->isHit())
{
return $this;
}
return null;
}

public function asMiss() : CacheListPopFrontResponseMiss|null
{
if ($this->isMiss())
{
return $this;
}
return null;
}

public function asError() : CacheListPopFrontResponseError|null
{
if ($this->isError())
{
return $this;
}
return null;
}
}

class CacheListPopFrontResponseHit extends CacheListPopFrontResponse {
private string $value;

public function __construct(_ListPopFrontResponse $response)
{
parent::__construct();
$this->value = $response->getFound()->getFront();
}

public function value() : string
{
return $this->value;
}

public function __toString()
{
return parent::__toString() . ": {$this->value}";
}
}

class CacheListPopFrontResponseMiss extends CacheListPopFrontResponse { }

class CacheListPopFrontResponseError extends CacheListPopFrontResponse
{
use ErrorBody;
}

abstract class CacheListPopBackResponse extends ResponseBase
{
public function asHit() : CacheListPopBackResponseHit|null
{
if ($this->isHit())
{
return $this;
}
return null;
}

public function asMiss() : CacheListPopBackResponseMiss|null
{
if ($this->isMiss())
{
return $this;
}
return null;
}

public function asError() : CacheListPopBackResponseError|null
{
if ($this->isError())
{
return $this;
}
return null;
}
}

class CacheListPopBackResponseHit extends CacheListPopBackResponse {
private string $value;

public function __construct(_ListPopBackResponse $response)
{
parent::__construct();
$this->value = $response->getFound()->getBack();
}

public function value() : string
{
return $this->value;
}

public function __toString()
{
return parent::__toString() . ": {$this->value}";
}
}

class CacheListPopBackResponseMiss extends CacheListPopBackResponse { }

class CacheListPopBackResponseError extends CacheListPopBackResponse
{
use ErrorBody;
}

abstract class CacheListRemoveValueResponse extends ResponseBase {
public function asSuccess(): CacheListRemoveValueResponseSuccess|null
{
if ($this->isSuccess()) {
return $this;
}
return null;
}

public function asError(): CacheListRemoveValueResponseError|null
{
if ($this->isError()) {
return $this;
}
return null;
}
}

class CacheListRemoveValueResponseSuccess extends CacheListRemoveValueResponse { }

class CacheListRemoveValueResponseError extends CacheListRemoveValueResponse
{
use ErrorBody;
}

abstract class CacheListLengthResponse extends ResponseBase {
public function asSuccess(): CacheListLengthResponseSuccess|null
{
if ($this->isSuccess()) {
return $this;
}
return null;
}

public function asError(): CacheListLengthResponseError|null
{
if ($this->isError()) {
return $this;
}
return null;
}
}

class CacheListLengthResponseSuccess extends CacheListLengthResponse {
private int $length;

public function __construct(_ListLengthResponse $response)
{
parent::__construct();
$this->length = $response->getFound() ? $response->getFound()->getLength() : 0;
}

public function length() : int
{
return $this->length;
}

public function __toString()
{
return parent::__toString() . ": {$this->length}";
}
}

class CacheListLengthResponseError extends CacheListLengthResponse
{
use ErrorBody;
}

abstract class CacheListEraseResponse extends ResponseBase {
public function asSuccess(): CacheListEraseResponseSuccess|null
{
if ($this->isSuccess()) {
return $this;
}
return null;
}

public function asError(): CacheListEraseResponseError|null
{
if ($this->isError()) {
return $this;
}
return null;
}
}

class CacheListEraseResponseSuccess extends CacheListEraseResponse { }

class CacheListEraseResponseError extends CacheListEraseResponse
{
use ErrorBody;
}
Loading

0 comments on commit c005907

Please sign in to comment.