Skip to content

Commit

Permalink
fix: Replace string with stringBuffer in verb builders
Browse files Browse the repository at this point in the history
  • Loading branch information
sitaram-kalluri committed Dec 10, 2023
1 parent ca79ebf commit 970f3c3
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 89 deletions.
16 changes: 7 additions & 9 deletions packages/at_commons/lib/src/verb/config_verb_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,25 @@ class ConfigVerbBuilder implements VerbBuilder {
String? configType;
String? operation;
List<String>? atSigns;

@override
String buildCommand() {
var command = 'config:';
command += '$configType:';
StringBuffer serverCommandBuffer = StringBuffer('config:$configType:');
if (operation == 'show') {
command += operation!;
serverCommandBuffer.write(operation!);
} else {
command += '$operation:';
serverCommandBuffer.write('$operation:');
}
if (atSigns != null && atSigns!.isNotEmpty) {
for (var atSign in atSigns!) {
command += '${VerbUtil.formatAtSign(atSign)}';
serverCommandBuffer.write('${VerbUtil.formatAtSign(atSign)}');
}
}

return '${command.trim()}\n';
return '${serverCommandBuffer.toString().trim()}\n';
}

@override
bool checkParams() {
// TODO: implement checkParams
return true;
return (configType != null && operation != null);
}
}
11 changes: 7 additions & 4 deletions packages/at_commons/lib/src/verb/delete_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'package:at_commons/at_commons.dart';
import 'package:at_commons/src/at_constants.dart';
import 'package:at_commons/src/exception/at_client_exceptions.dart';
import 'package:at_commons/src/utils/string_utils.dart';
import 'package:at_commons/src/verb/abstract_verb_builder.dart';
import 'package:at_commons/src/verb/syntax.dart';
import 'package:at_commons/src/verb/verb_util.dart';

/// Delete verb builder generates a command to delete a [atKey] from the secondary server.
/// ```
Expand All @@ -11,9 +15,8 @@ import 'package:at_commons/src/verb/abstract_verb_builder.dart';
class DeleteVerbBuilder extends AbstractVerbBuilder {
@override
String buildCommand() {
var command = 'delete:';
command += '${buildKey()}\n';
return command;
StringBuffer serverCommandBuffer = StringBuffer('delete:${buildKey()}\n');
return serverCommandBuffer.toString();
}

/// Returns a builder instance from a delete command
Expand Down
10 changes: 5 additions & 5 deletions packages/at_commons/lib/src/verb/from_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';

import 'package:at_commons/at_builders.dart';
import 'package:at_commons/at_commons.dart';
import 'package:at_commons/src/at_constants.dart';

class FromVerbBuilder implements VerbBuilder {
late String atSign;
Expand All @@ -11,13 +11,13 @@ class FromVerbBuilder implements VerbBuilder {

@override
String buildCommand() {
var command = 'from:$atSign';
StringBuffer serverCommandBuffer = StringBuffer('from:$atSign');
if (clientConfig.isNotEmpty) {
var clientConfigStr = jsonEncode(clientConfig);
command += ':${AtConstants.clientConfig}:$clientConfigStr';
serverCommandBuffer.write(':${AtConstants.clientConfig}:$clientConfigStr');
}
command += '\n';
return command;
serverCommandBuffer.write('\n');
return serverCommandBuffer.toString();
}

@override
Expand Down
8 changes: 4 additions & 4 deletions packages/at_commons/lib/src/verb/llookup_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:at_commons/at_commons.dart';
import 'package:at_commons/src/verb/abstract_verb_builder.dart';

/// Local lookup verb builder generates a command to lookup value of [atKey] stored in the secondary server.
Expand Down Expand Up @@ -62,11 +61,12 @@ class LLookupVerbBuilder extends AbstractVerbBuilder {

@override
String buildCommand() {
var command = 'llookup:';
StringBuffer serverCommandBuffer = StringBuffer('llookup:');
if (operation != null) {
command += '$operation:';
serverCommandBuffer.write('$operation:');
}
return command += '${buildKey()}\n';
serverCommandBuffer.write('${buildKey()}\n');
return serverCommandBuffer.toString();
}

@override
Expand Down
10 changes: 5 additions & 5 deletions packages/at_commons/lib/src/verb/lookup_verb_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class LookupVerbBuilder extends AbstractVerbBuilder {

@override
String buildCommand() {
String command = 'lookup:';
StringBuffer serverCommandBuffer = StringBuffer('lookup:');
if (bypassCache == true) {
command += 'bypassCache:$bypassCache:';
serverCommandBuffer.write('bypassCache:$bypassCache:');
}
if (operation != null) {
command += '$operation:';
serverCommandBuffer.write('$operation:');
}
command += atKey.toString();
return '$command\n';
serverCommandBuffer.write('${atKey.toString()}\n');
return serverCommandBuffer.toString();
}

@override
Expand Down
26 changes: 14 additions & 12 deletions packages/at_commons/lib/src/verb/notify_all_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:at_commons/at_commons.dart';
import 'package:at_commons/src/verb/abstract_verb_builder.dart';
import 'package:at_commons/src/verb/operation_enum.dart';
import 'package:at_commons/src/verb/verb_util.dart';

class NotifyAllVerbBuilder extends AbstractVerbBuilder {
/// Value of the key typically in string format. Images, files, etc.,
Expand All @@ -13,36 +14,37 @@ class NotifyAllVerbBuilder extends AbstractVerbBuilder {

@override
String buildCommand() {
var command = 'notify:';
StringBuffer serverCommandBuffer = StringBuffer('notify:');

if (operation != null) {
command += '${getOperationName(operation)}:';
serverCommandBuffer.write('${getOperationName(operation)}:');
}
if (atKey.metadata.ttl != null) {
command += 'ttl:${atKey.metadata.ttl}:';
serverCommandBuffer.write('ttl:${atKey.metadata.ttl}:');
}
if (atKey.metadata.ttb != null) {
command += 'ttb:${atKey.metadata.ttb}:';
serverCommandBuffer.write('ttb:${atKey.metadata.ttb}:');
}
if (atKey.metadata.ttr != null) {
atKey.metadata.ccd ??= false;
command += 'ttr:${atKey.metadata.ttr}:ccd:${atKey.metadata.ccd}:';
serverCommandBuffer
.write('ttr:${atKey.metadata.ttr}:ccd:${atKey.metadata.ccd}:');
}
if (sharedWithList != null && sharedWithList!.isNotEmpty) {
var sharedWith = sharedWithList!.join(',');
command += '${VerbUtil.formatAtSign(sharedWith)}:';
serverCommandBuffer.write('${VerbUtil.formatAtSign(sharedWith)}:');
}
if (atKey.metadata.isPublic == true) {
command += 'public:';
serverCommandBuffer.write('public:');
}
command += atKey.toString();
serverCommandBuffer.write(atKey.toString());
if (atKey.sharedBy != null) {
command += '${VerbUtil.formatAtSign(atKey.sharedBy)}';
serverCommandBuffer.write('${VerbUtil.formatAtSign(atKey.sharedBy)}');
}
if (value != null) {
command += ':$value';
serverCommandBuffer.write(':$value');
}
return '$command\n';
return (serverCommandBuffer..write('\n')).toString();
}

@override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:at_commons/at_commons.dart';
import 'package:at_commons/src/exception/at_exceptions.dart';
import 'package:at_commons/src/verb/verb_builder.dart';

/// NotifyFetchVerbBuilder generated the command to fetches the notification
Expand All @@ -18,7 +18,7 @@ class NotifyFetchVerbBuilder implements VerbBuilder {

@override
String buildCommand() {
var command = 'notify:fetch:';
StringBuffer serverCommandBuffer = StringBuffer('notify:fetch:');
try {
if (notificationId.isEmpty) {
throw InvalidSyntaxException(
Expand All @@ -28,8 +28,8 @@ class NotifyFetchVerbBuilder implements VerbBuilder {
throw InvalidSyntaxException(
'Notification-id is not set. Notification-id is mandatory');
}
command += '$notificationId\n';
return command;
serverCommandBuffer.write('$notificationId\n');
return serverCommandBuffer.toString();
}

@override
Expand Down
11 changes: 6 additions & 5 deletions packages/at_commons/lib/src/verb/notify_list_verb_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ class NotifyListVerbBuilder implements VerbBuilder {

@override
String buildCommand() {
var command = 'notify:list';
StringBuffer serverCommandBuffer = StringBuffer('notify:list');

if (fromDate != null) {
command += ':$fromDate';
serverCommandBuffer.write(':$fromDate');
}
if (toDate != null) {
command += ':$toDate';
serverCommandBuffer.write(':$toDate');
}
if (regex != null) {
command += ':$regex';
serverCommandBuffer.write(':$regex');
}
return command += '\n';
return (serverCommandBuffer..write('\n')).toString();
}

@override
Expand Down
17 changes: 8 additions & 9 deletions packages/at_commons/lib/src/verb/notify_status_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:at_commons/src/exception/at_exceptions.dart';
import 'package:at_commons/src/utils/string_utils.dart';
import 'package:at_commons/src/verb/verb_builder.dart';

class NotifyStatusVerbBuilder implements VerbBuilder {
Expand All @@ -6,19 +8,16 @@ class NotifyStatusVerbBuilder implements VerbBuilder {

@override
String buildCommand() {
var command = 'notify:status:';
if (notificationId != null) {
command += '$notificationId\n';
StringBuffer serverCommandBuffer = StringBuffer('notify:status:');
if (notificationId.isNullOrEmpty) {
throw InvalidAtKeyException('NotificationId cannot be null or empty');
}
return command;
serverCommandBuffer.write('$notificationId\n');
return serverCommandBuffer.toString();
}

@override
bool checkParams() {
var isValid = true;
if (notificationId == null) {
isValid = false;
}
return isValid;
return notificationId.isNotNullOrEmpty;
}
}
12 changes: 5 additions & 7 deletions packages/at_commons/lib/src/verb/pkam_verb_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ class PkamVerbBuilder implements VerbBuilder {

@override
String buildCommand() {
var command = 'pkam';
StringBuffer serverCommandBuffer = StringBuffer('pkam');
if (signingAlgo != null && signingAlgo!.isNotEmpty) {
command += ':signingAlgo:$signingAlgo';
serverCommandBuffer.write(':signingAlgo:$signingAlgo');
}
if (hashingAlgo != null && hashingAlgo!.isNotEmpty) {
command += ':hashingAlgo:$hashingAlgo';
serverCommandBuffer.write(':hashingAlgo:$hashingAlgo');
}
if (enrollmentlId != null && enrollmentlId!.isNotEmpty) {
command += ':enrollmentId:$enrollmentlId';
serverCommandBuffer.write(':enrollmentId:$enrollmentlId');
}
command += ':$signature';
command += '\n';
return command;
return (serverCommandBuffer..write(':$signature\n')).toString();
}

@override
Expand Down
13 changes: 7 additions & 6 deletions packages/at_commons/lib/src/verb/plookup_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:at_commons/at_commons.dart';
import 'package:at_commons/src/verb/abstract_verb_builder.dart';
import 'package:at_commons/src/verb/verb_util.dart';

Expand All @@ -16,15 +15,17 @@ class PLookupVerbBuilder extends AbstractVerbBuilder {

@override
String buildCommand() {
String command = 'plookup:';
StringBuffer serverCommandBuffer = StringBuffer('plookup:');
if (bypassCache == true) {
command += 'bypassCache:$bypassCache:';
serverCommandBuffer.write('bypassCache:$bypassCache:');
}
if (operation != null) {
command += '$operation:';
serverCommandBuffer.write('$operation:');
}
command += atKey.key;
return '$command${VerbUtil.formatAtSign(atKey.sharedBy)}\n';
serverCommandBuffer.write(atKey.key);
return (serverCommandBuffer
..write('${VerbUtil.formatAtSign(atKey.sharedBy)}\n'))
.toString();
}

@override
Expand Down
15 changes: 8 additions & 7 deletions packages/at_commons/lib/src/verb/scan_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:at_commons/at_commons.dart';
import 'package:at_commons/src/at_constants.dart';
import 'package:at_commons/src/verb/verb_builder.dart';
import 'package:at_commons/src/verb/verb_util.dart';

/// Scan verb builder generates a command to scan keys of current atSign(with ot without auth).
/// If a [regex] is set, keys matching the regex are returned.
Expand Down Expand Up @@ -40,18 +41,18 @@ class ScanVerbBuilder implements VerbBuilder {

@override
String buildCommand() {
var scanCommand = 'scan';
StringBuffer serverCommandBuffer = StringBuffer('scan');

if (showHiddenKeys) {
scanCommand += ':${AtConstants.showHidden}:true';
serverCommandBuffer.write(':${AtConstants.showHidden}:true');
}
if (sharedBy != null) {
scanCommand += ':${VerbUtil.formatAtSign(sharedBy)}';
serverCommandBuffer.write(':${VerbUtil.formatAtSign(sharedBy)}');
}
if (regex != null) {
scanCommand += ' $regex';
serverCommandBuffer.write(' $regex');
}
scanCommand += '\n';
return scanCommand;
return (serverCommandBuffer..write('\n')).toString();
}

@override
Expand Down
9 changes: 4 additions & 5 deletions packages/at_commons/lib/src/verb/stats_verb_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ class StatsVerbBuilder implements VerbBuilder {

@override
String buildCommand() {
var statsCommand = 'stats';
StringBuffer serverCommandBuffer = StringBuffer('stats');
if (statIds != null) {
statsCommand += ':$statIds';
serverCommandBuffer.write(':$statIds');
if (regex != null) {
statsCommand += ':$regex';
serverCommandBuffer.write(':$regex');
}
}
statsCommand += '\n';
return statsCommand;
return (serverCommandBuffer..write('\n')).toString();
}

@override
Expand Down
13 changes: 6 additions & 7 deletions packages/at_commons/lib/src/verb/sync_verb_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ class SyncVerbBuilder implements VerbBuilder {

@override
String buildCommand() {
var command = 'sync:';
StringBuffer serverCommandBuffer = StringBuffer('sync:');
if (isPaginated) {
command += 'from:';
serverCommandBuffer.write('from:');
}
command += '$commitId';
serverCommandBuffer.write('$commitId');
if (isPaginated) {
command += ':limit:$limit';
serverCommandBuffer.write(':limit:$limit');
}
if (regex != null && regex!.isNotEmpty) {
command += ':$regex';
serverCommandBuffer.write(':$regex');
}
command += '\n';
return command;
return (serverCommandBuffer..write('\n')).toString();
}

@override
Expand Down

0 comments on commit 970f3c3

Please sign in to comment.