Skip to content

Commit

Permalink
avoid long ternary chains for dartfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
a14n committed Mar 20, 2019
1 parent 618fb0e commit 8d1f9b7
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 246 deletions.
17 changes: 6 additions & 11 deletions lib/src/enums/delivery_mode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ class DeliveryMode extends Enum<int> {
const DeliveryMode._(int value) : super(value);

static DeliveryMode valueOf(int value) {
DeliveryMode fromValue = value == TRANSIENT._value
? TRANSIENT
: value == PERSISTENT._value ? PERSISTENT : null;

if (fromValue == null) {
throw ArgumentError("Invalid delivery mode value ${value}");
}
return fromValue;
if (value == TRANSIENT.value) return TRANSIENT;
if (value == PERSISTENT.value) return PERSISTENT;
throw ArgumentError("Invalid delivery mode value $value");
}

static String nameOf(DeliveryMode value) {
return value == TRANSIENT
? "TRANSIENT"
: value == PERSISTENT ? "PERSISTENT" : null;
if (value == TRANSIENT) return "TRANSIENT";
if (value == PERSISTENT) return "PERSISTENT";
return null;
}
}
6 changes: 2 additions & 4 deletions lib/src/enums/enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ part of dart_amqp.enums;

/// An abstract class for modeling enums
abstract class Enum<T> {
final T _value;
final T value;

const Enum(this._value);

T get value => _value;
const Enum(this.value);
}
135 changes: 38 additions & 97 deletions lib/src/enums/error_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,105 +29,46 @@ class ErrorType extends Enum<int> {
String toString() => "${value}";

static ErrorType valueOf(int value) {
ErrorType fromValue = value == SUCCESS._value
? SUCCESS
: value == CONTENT_TOO_LARGE._value
? CONTENT_TOO_LARGE
: value == NO_CONSUMERS._value
? NO_CONSUMERS
: value == CONNECTION_FORCED._value
? CONNECTION_FORCED
: value == INVALID_PATH._value
? INVALID_PATH
: value == ACCESS_REFUSED._value
? ACCESS_REFUSED
: value == NOT_FOUND._value
? NOT_FOUND
: value == RESOURCE_LOCKED._value
? RESOURCE_LOCKED
: value == PRECONDITION_FAILED._value
? PRECONDITION_FAILED
: value == FRAME_ERROR._value
? FRAME_ERROR
: value == SYNTAX_ERROR._value
? SYNTAX_ERROR
: value ==
COMMAND_INVALID._value
? COMMAND_INVALID
: value ==
CHANNEL_ERROR._value
? CHANNEL_ERROR
: value ==
UNEXPECTED_FRAME
._value
? UNEXPECTED_FRAME
: value ==
RESOURCE_ERROR
._value
? RESOURCE_ERROR
: value ==
NOT_ALLOWED
._value
? NOT_ALLOWED
: value ==
NOT_IMPLEMENTED
._value
? NOT_IMPLEMENTED
: value ==
INTERNAL_ERROR._value
? INTERNAL_ERROR
: null;

if (fromValue == null) {
throw ArgumentError("Invalid error type value ${value}");
}
return fromValue;
if (value == SUCCESS.value) return SUCCESS;
if (value == CONTENT_TOO_LARGE.value) return CONTENT_TOO_LARGE;
if (value == NO_CONSUMERS.value) return NO_CONSUMERS;
if (value == CONNECTION_FORCED.value) return CONNECTION_FORCED;
if (value == INVALID_PATH.value) return INVALID_PATH;
if (value == ACCESS_REFUSED.value) return ACCESS_REFUSED;
if (value == NOT_FOUND.value) return NOT_FOUND;
if (value == RESOURCE_LOCKED.value) return RESOURCE_LOCKED;
if (value == PRECONDITION_FAILED.value) return PRECONDITION_FAILED;
if (value == FRAME_ERROR.value) return FRAME_ERROR;
if (value == SYNTAX_ERROR.value) return SYNTAX_ERROR;
if (value == COMMAND_INVALID.value) return COMMAND_INVALID;
if (value == CHANNEL_ERROR.value) return CHANNEL_ERROR;
if (value == UNEXPECTED_FRAME.value) return UNEXPECTED_FRAME;
if (value == RESOURCE_ERROR.value) return RESOURCE_ERROR;
if (value == NOT_ALLOWED.value) return NOT_ALLOWED;
if (value == NOT_IMPLEMENTED.value) return NOT_IMPLEMENTED;
if (value == INTERNAL_ERROR.value) return INTERNAL_ERROR;
throw ArgumentError("Invalid error type value $value");
}

static String nameOf(ErrorType value) {
String name = value == SUCCESS
? "SUCCESS"
: value == CONTENT_TOO_LARGE
? "CONTENT_TOO_LARGE"
: value == NO_CONSUMERS
? "NO_CONSUMERS"
: value == CONNECTION_FORCED
? "CONNECTION_FORCED"
: value == INVALID_PATH
? "INVALID_PATH"
: value == ACCESS_REFUSED
? "ACCESS_REFUSED"
: value == NOT_FOUND
? "NOT_FOUND"
: value == RESOURCE_LOCKED
? "RESOURCE_LOCKED"
: value == PRECONDITION_FAILED
? "PRECONDITION_FAILED"
: value == FRAME_ERROR
? "FRAME_ERROR"
: value == SYNTAX_ERROR
? "SYNTAX_ERROR"
: value == COMMAND_INVALID
? "COMMAND_INVALID"
: value == CHANNEL_ERROR
? "CHANNEL_ERROR"
: value ==
UNEXPECTED_FRAME
? "UNEXPECTED_FRAME"
: value ==
RESOURCE_ERROR
? "RESOURCE_ERROR"
: value ==
NOT_ALLOWED
? "NOT_ALLOWED"
: value ==
NOT_IMPLEMENTED
? "NOT_IMPLEMENTED"
: value ==
INTERNAL_ERROR
? "INTERNAL_ERROR"
: null;

return name;
if (value == SUCCESS) return "SUCCESS";
if (value == CONTENT_TOO_LARGE) return "CONTENT_TOO_LARGE";
if (value == NO_CONSUMERS) return "NO_CONSUMERS";
if (value == CONNECTION_FORCED) return "CONNECTION_FORCED";
if (value == INVALID_PATH) return "INVALID_PATH";
if (value == ACCESS_REFUSED) return "ACCESS_REFUSED";
if (value == NOT_FOUND) return "NOT_FOUND";
if (value == RESOURCE_LOCKED) return "RESOURCE_LOCKED";
if (value == PRECONDITION_FAILED) return "PRECONDITION_FAILED";
if (value == FRAME_ERROR) return "FRAME_ERROR";
if (value == SYNTAX_ERROR) return "SYNTAX_ERROR";
if (value == COMMAND_INVALID) return "COMMAND_INVALID";
if (value == CHANNEL_ERROR) return "CHANNEL_ERROR";
if (value == UNEXPECTED_FRAME) return "UNEXPECTED_FRAME";
if (value == RESOURCE_ERROR) return "RESOURCE_ERROR";
if (value == NOT_ALLOWED) return "NOT_ALLOWED";
if (value == NOT_IMPLEMENTED) return "NOT_IMPLEMENTED";
if (value == INTERNAL_ERROR) return "INTERNAL_ERROR";
return null;
}
}
33 changes: 11 additions & 22 deletions lib/src/enums/exchange_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,20 @@ class ExchangeType extends BaseExchange<String> {
String toString() => "${value}";

static ExchangeType valueOf(String value) {
ExchangeType fromValue = value == FANOUT._value
? FANOUT
: value == DIRECT._value
? DIRECT
: value == TOPIC._value
? TOPIC
: value == HEADERS._value
? HEADERS
: ExchangeType.custom(value);

return fromValue;
if (value == FANOUT.value) return FANOUT;
if (value == DIRECT.value) return DIRECT;
if (value == TOPIC.value) return TOPIC;
if (value == HEADERS.value) return HEADERS;
return ExchangeType.custom(value);
}

static String nameOf(ExchangeType value) {
String name = value == FANOUT
? "FANOUT"
: value == DIRECT
? "DIRECT"
: value == TOPIC
? "TOPIC"
: value == HEADERS
? "HEADERS"
: value.isCustom ? "CUSTOM" : null;

return name;
if (value == FANOUT) return "FANOUT";
if (value == DIRECT) return "DIRECT";
if (value == TOPIC) return "TOPIC";
if (value == HEADERS) return "HEADERS";
if (value.isCustom) return "CUSTOM";
return null;
}
}

Expand Down
131 changes: 38 additions & 93 deletions lib/src/enums/field_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,101 +23,46 @@ class FieldType extends Enum<int> {
const FieldType._(int value) : super(value);

static FieldType valueOf(int value) {
FieldType fromValue = value == BOOLEAN._value
? BOOLEAN
: value == SHORT_SHORT_INT._value
? SHORT_SHORT_INT
: value == SHORT_SHORT_UINT._value
? SHORT_SHORT_UINT
: value == SHORT_INT._value
? SHORT_INT
: value == SHORT_UINT._value
? SHORT_UINT
: value == LONG_INT._value
? LONG_INT
: value == LONG_UINT._value
? LONG_UINT
: value == LONG_LONG_INT._value
? LONG_LONG_INT
: value == LONG_LONG_UINT._value
? LONG_LONG_UINT
: value == FLOAT._value
? FLOAT
: value == DOUBLE._value
? DOUBLE
: value == DECIMAL._value
? DECIMAL
: value ==
SHORT_STRING._value
? SHORT_STRING
: value ==
LONG_STRING
._value
? LONG_STRING
: value ==
FIELD_ARRAY
._value
? FIELD_ARRAY
: value ==
TIMESTAMP
._value
? TIMESTAMP
: value ==
FIELD_TABLE
._value
? FIELD_TABLE
: value ==
VOID._value
? VOID
: null;

if (fromValue == null) {
throw ArgumentError("Invalid field type value ${value}");
}
return fromValue;
if (value == BOOLEAN.value) return BOOLEAN;
if (value == SHORT_SHORT_INT.value) return SHORT_SHORT_INT;
if (value == SHORT_SHORT_UINT.value) return SHORT_SHORT_UINT;
if (value == SHORT_INT.value) return SHORT_INT;
if (value == SHORT_UINT.value) return SHORT_UINT;
if (value == LONG_INT.value) return LONG_INT;
if (value == LONG_UINT.value) return LONG_UINT;
if (value == LONG_LONG_INT.value) return LONG_LONG_INT;
if (value == LONG_LONG_UINT.value) return LONG_LONG_UINT;
if (value == FLOAT.value) return FLOAT;
if (value == DOUBLE.value) return DOUBLE;
if (value == DECIMAL.value) return DECIMAL;
if (value == SHORT_STRING.value) return SHORT_STRING;
if (value == LONG_STRING.value) return LONG_STRING;
if (value == FIELD_ARRAY.value) return FIELD_ARRAY;
if (value == TIMESTAMP.value) return TIMESTAMP;
if (value == FIELD_TABLE.value) return FIELD_TABLE;
if (value == VOID.value) return VOID;
throw ArgumentError("Invalid field type value $value");
}

static String nameOf(FieldType value) {
return value == BOOLEAN
? "BOOLEAN"
: value == SHORT_SHORT_INT
? "SHORT_SHORT_INT"
: value == SHORT_SHORT_UINT
? "SHORT_SHORT_UINT"
: value == SHORT_INT
? "SHORT_INT"
: value == SHORT_UINT
? "SHORT_UINT"
: value == LONG_INT
? "LONG_INT"
: value == LONG_UINT
? "LONG_UINT"
: value == LONG_LONG_INT
? "LONG_LONG_INT"
: value == LONG_LONG_UINT
? "LONG_LONG_UINT"
: value == FLOAT
? "FLOAT"
: value == DOUBLE
? "DOUBLE"
: value == DECIMAL
? "DECIMAL"
: value == SHORT_STRING
? "SHORT_STRING"
: value == LONG_STRING
? "LONG_STRING"
: value ==
FIELD_ARRAY
? "FIELD_ARRAY"
: value ==
TIMESTAMP
? "TIMESTAMP"
: value ==
FIELD_TABLE
? "FIELD_TABLE"
: value ==
VOID
? "VOID"
: null;
if (value == BOOLEAN) return "BOOLEAN";
if (value == SHORT_SHORT_INT) return "SHORT_SHORT_INT";
if (value == SHORT_SHORT_UINT) return "SHORT_SHORT_UINT";
if (value == SHORT_INT) return "SHORT_INT";
if (value == SHORT_UINT) return "SHORT_UINT";
if (value == LONG_INT) return "LONG_INT";
if (value == LONG_UINT) return "LONG_UINT";
if (value == LONG_LONG_INT) return "LONG_LONG_INT";
if (value == LONG_LONG_UINT) return "LONG_LONG_UINT";
if (value == FLOAT) return "FLOAT";
if (value == DOUBLE) return "DOUBLE";
if (value == DECIMAL) return "DECIMAL";
if (value == SHORT_STRING) return "SHORT_STRING";
if (value == LONG_STRING) return "LONG_STRING";
if (value == FIELD_ARRAY) return "FIELD_ARRAY";
if (value == TIMESTAMP) return "TIMESTAMP";
if (value == FIELD_TABLE) return "FIELD_TABLE";
if (value == VOID) return "VOID";
return null;
}
}
Loading

0 comments on commit 8d1f9b7

Please sign in to comment.