Skip to content

Commit

Permalink
Use pattern matching to replace if-else
Browse files Browse the repository at this point in the history
  • Loading branch information
lamnhan066 committed Feb 8, 2025
1 parent d633340 commit 5edd7ee
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions lib/src/models/isolate_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,23 @@ sealed class IsolateType extends Object with EquatableMixin {
///
/// Throw [UnimplementedError] if containing any un supported types.
static R encode<R extends IsolateType>(Object? object) {
if (object is IsolateType) {
return object as R;
}

if (object is num?) {
return IsolateNum(object) as R;
} else if (object is String?) {
return IsolateString(object as String?) as R;
} else if (object is bool?) {
return IsolateBool(object as bool?) as R;
} else if (object is List<Object?>?) {
return IsolateList((object as List?)?.map(IsolateType.encode).toList())
as R;
} else if (object is Map<Object?, Object?>?) {
return IsolateMap(
(object as Map<Object?, Object?>?)?.map(
(k, v) => MapEntry(IsolateType.encode(k), IsolateType.encode(v)),
if (object is IsolateType) return object as R;

return switch (object) {
final num? n => IsolateNum(n),
final String? s => IsolateString(s),
final bool? b => IsolateBool(b),
final List<Object?>? list =>
IsolateList(list?.map(IsolateType.encode).toList()),
final Map<Object?, Object?>? map => IsolateMap(
map?.map(
(k, v) => MapEntry(IsolateType.encode(k), IsolateType.encode(v)),
),
),
) as R;
}

return throw UnimplementedError(
'Contains unsupported type ${object.runtimeType} when encoding an IsolateType',
);
_ => throw UnimplementedError(
'Contains unsupported type ${object.runtimeType} when encoding an IsolateType',
),
} as R;
}

/// Convert to normal types.
Expand Down

1 comment on commit 5edd7ee

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

94.03%

Coverage Report for Changed Files
FileStmtsBranchesFuncsLinesUncovered Lines
lib/src/base/contactor/isolate_contactor
   isolate_contactor_stub.dart100%100%100%100%
lib/src/base/contactor/isolate_contactor/web_platform
   isolate_contactor_web.dart95%100%100%95%79, 92
   isolate_contactor_web_worker.dart87.18%100%100%87.18%109, 111, 79, 82–83
lib/src/models
   isolate_types.dart100%100%100%100%
   isolate_types.dart100%100%100%100%

Please sign in to comment.