Skip to content

Commit

Permalink
chore: implement toString for non-error response classes (#89)
Browse files Browse the repository at this point in the history
* chore: implement toString for non-error response classes

* simplify toString to class name and override only as needed

* dart format and update flaky topics test time buffer

* missed ListCachesSuccess and fixed typo in base class

* restructure pubsub test to be less flaky

* dart format

* dart analyze

* rename NonErrorResponseBase to ResponseBase

* BaseResponse overrides should use super and truncation util functions

* dart format
  • Loading branch information
anitarua committed Jan 10, 2024
1 parent e0a1632 commit d8ff271
Show file tree
Hide file tree
Showing 22 changed files with 146 additions and 40 deletions.
8 changes: 6 additions & 2 deletions lib/src/internal/data_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ class DataClient implements AbstractDataClient {
options: CallOptions(metadata: {
'cache': cacheName,
}));
return ListConcatenateFrontSuccess(response.listLength);
return ListConcatenateFrontSuccess(
response.listLength,
);
} catch (e) {
if (e is GrpcError) {
return ListConcatenateFrontError(grpcStatusToSdkException(e));
Expand Down Expand Up @@ -271,7 +273,9 @@ class DataClient implements AbstractDataClient {
}));
switch (response.whichList()) {
case ListPopBackResponse__List.found:
return ListPopBackHit(response.found.back);
return ListPopBackHit(
response.found.back,
);
case ListPopBackResponse__List.missing:
return ListPopBackMiss();
default:
Expand Down
27 changes: 27 additions & 0 deletions lib/src/internal/utils/display.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const displaySizeLimit = 5;

String truncateString(String value, {int maxLength = 32}) {
if (value.length > maxLength) {
return '${value.substring(0, maxLength)}...';
} else {
return value;
}
}

List<String> truncateStringArrayToSize(List<String> stringArray,
{int length = displaySizeLimit}) {
if (stringArray.length <= length) {
return stringArray;
} else {
return stringArray.sublist(0, length).followedBy(['...']).toList();
}
}

List<String> truncateStringArray(List<String> stringArray,
{int length = displaySizeLimit}) {
final truncatedStringArray =
truncateStringArrayToSize(stringArray, length: length);
return truncatedStringArray.map((s) {
return truncateString(s);
}).toList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import 'package:momento/src/messages/responses/responses_base.dart';
sealed class CreateCacheResponse {}

/// Indicates a successful create cache request.
class CreateCacheSuccess implements CreateCacheResponse {}
class CreateCacheSuccess extends ResponseBase implements CreateCacheResponse {}

/// Indicates that the cache already exists, so there was nothing to do.
class CreateCacheAlreadyExists implements CreateCacheResponse {}
class CreateCacheAlreadyExists extends ResponseBase
implements CreateCacheResponse {}

/// Indicates that an error occurred during the create cache request.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:momento/src/messages/responses/responses_base.dart';
sealed class DeleteCacheResponse {}

/// Indicates a successful delete cache request.
class DeleteCacheSuccess implements DeleteCacheResponse {}
class DeleteCacheSuccess extends ResponseBase implements DeleteCacheResponse {}

/// Indicates that an error occurred during the delete cache request.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:momento/generated/controlclient.pb.dart';
import 'package:momento/src/internal/utils/display.dart';
import 'package:momento/src/messages/responses/responses_base.dart';

/// Represents information about a listed cache, such as its name.
Expand All @@ -23,7 +24,7 @@ class CacheInfo {
sealed class ListCachesResponse {}

/// Indicates a successful list caches request.
class ListCachesSuccess implements ListCachesResponse {
class ListCachesSuccess extends ResponseBase implements ListCachesResponse {
late final List<CacheInfo> caches;

ListCachesSuccess(List<Cache_> grpcCaches) {
Expand All @@ -35,6 +36,11 @@ class ListCachesSuccess implements ListCachesResponse {
}

List<String> get cacheNames => caches.map((cache) => cache.name).toList();

@override
String toString() {
return "${super.toString()}: ${truncateStringArray(cacheNames)}";
}
}

/// Indicates that an error occurred during the list caches request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ import '../../../responses_base.dart';
sealed class ListConcatenateBackResponse {}

/// Indicates that the request was successful and the updated length can be accessed by the field `length`.
class ListConcatenateBackSuccess implements ListConcatenateBackResponse {
class ListConcatenateBackSuccess extends ResponseBase
implements ListConcatenateBackResponse {
ListConcatenateBackSuccess(this._length);

final int _length;

int get length => _length;

@override
String toString() {
return "${super.toString()}: Length $length";
}
}

/// Indicates that an error occurred during the list concatenate back request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ import '../../../responses_base.dart';
sealed class ListConcatenateFrontResponse {}

/// Indicates that the request was successful and the updated length can be accessed by the field `length`.
class ListConcatenateFrontSuccess implements ListConcatenateFrontResponse {
class ListConcatenateFrontSuccess extends ResponseBase
implements ListConcatenateFrontResponse {
ListConcatenateFrontSuccess(this._length);

final int _length;

int get length => _length;

@override
String toString() {
return "${super.toString()}: Length $length";
}
}

/// Indicates that an error occurred during the list concatenate front request.
Expand Down
10 changes: 8 additions & 2 deletions lib/src/messages/responses/cache/data/list/list_fetch.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:momento/src/internal/utils/display.dart';
import 'package:momento/src/messages/responses/responses_base.dart';

/// Sealed class for a cache list fetch response.
Expand All @@ -18,7 +19,7 @@ import 'package:momento/src/messages/responses/responses_base.dart';
sealed class ListFetchResponse {}

/// Indicates that the requested list was not available in the cache.
class ListFetchMiss implements ListFetchResponse {}
class ListFetchMiss extends ResponseBase implements ListFetchResponse {}

/// Indicates that an error occurred during the list fetch request.
///
Expand All @@ -31,11 +32,16 @@ class ListFetchError extends ErrorResponseBase implements ListFetchResponse {
}

/// Indicates that the requested list was successfully retrieved from the cache and can be accessed by the fields `values` or `binaryValues`.
class ListFetchHit implements ListFetchResponse {
class ListFetchHit extends ResponseBase implements ListFetchResponse {
ListFetchHit(this._values);

final List<List<int>> _values;

Iterable<String> get values => _values.map((e) => utf8.decode(e));
Iterable<List<int>> get binaryValues => _values;

@override
String toString() {
return "${super.toString()}: ${truncateStringArray(values.toList())}";
}
}
9 changes: 7 additions & 2 deletions lib/src/messages/responses/cache/data/list/list_length.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import '../../../responses_base.dart';
sealed class ListLengthResponse {}

/// Indicates that the requested list was not available in the cache.
class ListLengthMiss implements ListLengthResponse {}
class ListLengthMiss extends ResponseBase implements ListLengthResponse {}

/// Indicates that an error occurred during the list length request.
///
Expand All @@ -29,10 +29,15 @@ class ListLengthError extends ErrorResponseBase implements ListLengthResponse {
}

/// Indicates that the requested list length was successfully retrieved from the cache and the length can be accessed by the field `length`.
class ListLengthHit implements ListLengthResponse {
class ListLengthHit extends ResponseBase implements ListLengthResponse {
ListLengthHit(this._length);

final int _length;

int get length => _length;

@override
String toString() {
return "${super.toString()}: Length $length";
}
}
11 changes: 9 additions & 2 deletions lib/src/messages/responses/cache/data/list/list_pop_back.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:convert';

import 'package:momento/src/internal/utils/display.dart';

import '../../../responses_base.dart';

/// Sealed class for a cache list pop back response.
Expand All @@ -18,7 +20,7 @@ import '../../../responses_base.dart';
sealed class ListPopBackResponse {}

/// Indicates that the requested list was not available in the cache.
class ListPopBackMiss implements ListPopBackResponse {}
class ListPopBackMiss extends ResponseBase implements ListPopBackResponse {}

/// Indicates that an error occurred during the list pop back request.
///
Expand All @@ -32,11 +34,16 @@ class ListPopBackError extends ErrorResponseBase
}

/// Indicates that the request was successful and the value can be accessed by the fields `value` or `binaryValue`.
class ListPopBackHit implements ListPopBackResponse {
class ListPopBackHit extends ResponseBase implements ListPopBackResponse {
ListPopBackHit(this._value);

final List<int> _value;

String get value => utf8.decode(_value);
List<int> get binaryValue => _value;

@override
String toString() {
return "${super.toString()}: ${truncateString(value)}";
}
}
11 changes: 9 additions & 2 deletions lib/src/messages/responses/cache/data/list/list_pop_front.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:convert';

import 'package:momento/src/internal/utils/display.dart';

import '../../../responses_base.dart';

/// Sealed class for a cache list pop front response.
Expand All @@ -18,7 +20,7 @@ import '../../../responses_base.dart';
sealed class ListPopFrontResponse {}

/// Indicates that the requested list was not available in the cache.
class ListPopFrontMiss implements ListPopFrontResponse {}
class ListPopFrontMiss extends ResponseBase implements ListPopFrontResponse {}

/// Indicates that an error occurred during the list pop front request.
///
Expand All @@ -32,11 +34,16 @@ class ListPopFrontError extends ErrorResponseBase
}

/// Indicates that the request was successful and the value can be accessed by the fields `value` or `binaryValue`.
class ListPopFrontHit implements ListPopFrontResponse {
class ListPopFrontHit extends ResponseBase implements ListPopFrontResponse {
ListPopFrontHit(this._value);

final List<int> _value;

String get value => utf8.decode(_value);
List<int> get binaryValue => _value;

@override
String toString() {
return "${super.toString()}: ${truncateString(value)}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ class ListPushBackError extends ErrorResponseBase
}

/// Indicates that the request was successful and the updated length can be accessed by the field `length`.
class ListPushBackSuccess implements ListPushBackResponse {
class ListPushBackSuccess extends ResponseBase implements ListPushBackResponse {
ListPushBackSuccess(this._length);

final int _length;

int get length => _length;

@override
String toString() {
return "${super.toString()}: Length $length";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ class ListPushFrontError extends ErrorResponseBase
}

/// Indicates that the request was successful and the updated length can be accessed by the field `length`.
class ListPushFrontSuccess implements ListPushFrontResponse {
class ListPushFrontSuccess extends ResponseBase
implements ListPushFrontResponse {
ListPushFrontSuccess(this._length);

final int _length;

int get length => _length;

@override
String toString() {
return "${super.toString()}: Length $length";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import '../../../responses_base.dart';
sealed class ListRemoveValueResponse {}

/// Indicates that the request was successful.
class ListRemoveValueSuccess implements ListRemoveValueResponse {}
class ListRemoveValueSuccess extends ResponseBase
implements ListRemoveValueResponse {}

/// Indicates that an error occurred during the list remove value request.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import '../../../responses_base.dart';
sealed class ListRetainResponse {}

/// Indicates that the request was successful.
class ListRetainSuccess implements ListRetainResponse {}
class ListRetainSuccess extends ResponseBase implements ListRetainResponse {}

/// Indicates that an error occurred during the list retain request.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:momento/src/messages/responses/responses_base.dart';
sealed class DeleteResponse {}

/// Indicates a successful delete cache item request.
class DeleteSuccess implements DeleteResponse {}
class DeleteSuccess extends ResponseBase implements DeleteResponse {}

/// Indicates that an error occurred during the delete cache item request.
///
Expand Down
10 changes: 8 additions & 2 deletions lib/src/messages/responses/cache/data/scalar/get_response.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:momento/src/internal/utils/display.dart';
import 'package:momento/src/messages/responses/responses_base.dart';

/// Sealed class for a get cache item request.
Expand All @@ -18,7 +19,7 @@ import 'package:momento/src/messages/responses/responses_base.dart';
sealed class GetResponse {}

/// Indicates that the requested data was not available in the cache.
class GetMiss implements GetResponse {}
class GetMiss extends ResponseBase implements GetResponse {}

/// Indicates that an error occurred during the get cache item request.
///
Expand All @@ -31,11 +32,16 @@ class GetError extends ErrorResponseBase implements GetResponse {
}

/// Indicates that the requested data was successfully retrieved from the cache and can be accessed by the fields `value` or `binaryValue`.
class GetHit implements GetResponse {
class GetHit extends ResponseBase implements GetResponse {
GetHit(this._value);

final List<int> _value;

String get value => utf8.decode(_value);
List<int> get binaryValue => _value;

@override
String toString() {
return "${super.toString()}: ${truncateString(value)}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:momento/src/messages/responses/responses_base.dart';
sealed class SetResponse {}

/// Indicates a successful set cache item request.
class SetSuccess implements SetResponse {}
class SetSuccess extends ResponseBase implements SetResponse {}

/// Indicates that an error occurred during the set cache item request.
///
Expand Down
7 changes: 7 additions & 0 deletions lib/src/messages/responses/responses_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ class ErrorResponseBase extends AbstractExceptionResponseBase
return "[${super.errorCode}] ${super.message}";
}
}

class ResponseBase {
@override
String toString() {
return "$runtimeType";
}
}
3 changes: 2 additions & 1 deletion lib/src/messages/responses/topics/topic_publish.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'package:momento/src/messages/responses/responses_base.dart';

sealed class TopicPublishResponse {}

class TopicPublishSuccess implements TopicPublishResponse {}
class TopicPublishSuccess extends ResponseBase
implements TopicPublishResponse {}

class TopicPublishError extends ErrorResponseBase
implements TopicPublishResponse {
Expand Down

0 comments on commit d8ff271

Please sign in to comment.