switch statement cleanup (#148382)
This PR is step 12 in the journey to solve issue #136139 and make the entire Flutter repo more readable. Most of it involves implementing switch expressions, and there's also a few other random things that I wanted to clean up a bit.
This commit is contained in:
@@ -22,18 +22,17 @@ import 'wait.dart';
|
||||
mixin DeserializeFinderFactory {
|
||||
/// Deserializes the finder from JSON generated by [SerializableFinder.serialize].
|
||||
SerializableFinder deserializeFinder(Map<String, String> json) {
|
||||
final String? finderType = json['finderType'];
|
||||
switch (finderType) {
|
||||
case 'ByType': return ByType.deserialize(json);
|
||||
case 'ByValueKey': return ByValueKey.deserialize(json);
|
||||
case 'ByTooltipMessage': return ByTooltipMessage.deserialize(json);
|
||||
case 'BySemanticsLabel': return BySemanticsLabel.deserialize(json);
|
||||
case 'ByText': return ByText.deserialize(json);
|
||||
case 'PageBack': return const PageBack();
|
||||
case 'Descendant': return Descendant.deserialize(json, this);
|
||||
case 'Ancestor': return Ancestor.deserialize(json, this);
|
||||
}
|
||||
throw DriverError('Unsupported search specification type $finderType');
|
||||
return switch (json['finderType']) {
|
||||
'ByType' => ByType.deserialize(json),
|
||||
'ByValueKey' => ByValueKey.deserialize(json),
|
||||
'ByTooltipMessage' => ByTooltipMessage.deserialize(json),
|
||||
'BySemanticsLabel' => BySemanticsLabel.deserialize(json),
|
||||
'ByText' => ByText.deserialize(json),
|
||||
'PageBack' => const PageBack(),
|
||||
'Descendant' => Descendant.deserialize(json, this),
|
||||
'Ancestor' => Ancestor.deserialize(json, this),
|
||||
_ => throw DriverError('Unsupported search specification type ${json['finderType']}'),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,33 +40,31 @@ mixin DeserializeFinderFactory {
|
||||
mixin DeserializeCommandFactory {
|
||||
/// Deserializes the finder from JSON generated by [Command.serialize] or [CommandWithTarget.serialize].
|
||||
Command deserializeCommand(Map<String, String> params, DeserializeFinderFactory finderFactory) {
|
||||
final String? kind = params['command'];
|
||||
switch (kind) {
|
||||
case 'get_health': return GetHealth.deserialize(params);
|
||||
case 'get_layer_tree': return GetLayerTree.deserialize(params);
|
||||
case 'get_render_tree': return GetRenderTree.deserialize(params);
|
||||
case 'enter_text': return EnterText.deserialize(params);
|
||||
case 'send_text_input_action': return SendTextInputAction.deserialize(params);
|
||||
case 'get_text': return GetText.deserialize(params, finderFactory);
|
||||
case 'request_data': return RequestData.deserialize(params);
|
||||
case 'scroll': return Scroll.deserialize(params, finderFactory);
|
||||
case 'scrollIntoView': return ScrollIntoView.deserialize(params, finderFactory);
|
||||
case 'set_frame_sync': return SetFrameSync.deserialize(params);
|
||||
case 'set_semantics': return SetSemantics.deserialize(params);
|
||||
case 'set_text_entry_emulation': return SetTextEntryEmulation.deserialize(params);
|
||||
case 'tap': return Tap.deserialize(params, finderFactory);
|
||||
case 'waitFor': return WaitFor.deserialize(params, finderFactory);
|
||||
case 'waitForAbsent': return WaitForAbsent.deserialize(params, finderFactory);
|
||||
case 'waitForTappable': return WaitForTappable.deserialize(params, finderFactory);
|
||||
case 'waitForCondition': return WaitForCondition.deserialize(params);
|
||||
case 'waitUntilNoTransientCallbacks': return WaitForCondition.deserialize(params);
|
||||
case 'waitUntilNoPendingFrame': return WaitForCondition.deserialize(params);
|
||||
case 'waitUntilFirstFrameRasterized': return WaitForCondition.deserialize(params);
|
||||
case 'get_semantics_id': return GetSemanticsId.deserialize(params, finderFactory);
|
||||
case 'get_offset': return GetOffset.deserialize(params, finderFactory);
|
||||
case 'get_diagnostics_tree': return GetDiagnosticsTree.deserialize(params, finderFactory);
|
||||
}
|
||||
|
||||
throw DriverError('Unsupported command kind $kind');
|
||||
return switch (params['command']) {
|
||||
'get_health' => GetHealth.deserialize(params),
|
||||
'get_layer_tree' => GetLayerTree.deserialize(params),
|
||||
'get_render_tree' => GetRenderTree.deserialize(params),
|
||||
'enter_text' => EnterText.deserialize(params),
|
||||
'send_text_input_action' => SendTextInputAction.deserialize(params),
|
||||
'get_text' => GetText.deserialize(params, finderFactory),
|
||||
'request_data' => RequestData.deserialize(params),
|
||||
'scroll' => Scroll.deserialize(params, finderFactory),
|
||||
'scrollIntoView' => ScrollIntoView.deserialize(params, finderFactory),
|
||||
'set_frame_sync' => SetFrameSync.deserialize(params),
|
||||
'set_semantics' => SetSemantics.deserialize(params),
|
||||
'set_text_entry_emulation' => SetTextEntryEmulation.deserialize(params),
|
||||
'tap' => Tap.deserialize(params, finderFactory),
|
||||
'waitFor' => WaitFor.deserialize(params, finderFactory),
|
||||
'waitForAbsent' => WaitForAbsent.deserialize(params, finderFactory),
|
||||
'waitForTappable' => WaitForTappable.deserialize(params, finderFactory),
|
||||
'waitForCondition' => WaitForCondition.deserialize(params),
|
||||
'waitUntilNoTransientCallbacks' => WaitForCondition.deserialize(params),
|
||||
'waitUntilNoPendingFrame' => WaitForCondition.deserialize(params),
|
||||
'waitUntilFirstFrameRasterized' => WaitForCondition.deserialize(params),
|
||||
'get_semantics_id' => GetSemanticsId.deserialize(params, finderFactory),
|
||||
'get_offset' => GetOffset.deserialize(params, finderFactory),
|
||||
'get_diagnostics_tree' => GetDiagnosticsTree.deserialize(params, finderFactory),
|
||||
final String? kind => throw DriverError('Unsupported command kind $kind'),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,15 +231,11 @@ class ByValueKey extends SerializableFinder {
|
||||
/// Deserializes the finder from JSON generated by [serialize].
|
||||
static ByValueKey deserialize(Map<String, String> json) {
|
||||
final String keyValueString = json['keyValueString']!;
|
||||
final String keyValueType = json['keyValueType']!;
|
||||
switch (keyValueType) {
|
||||
case 'int':
|
||||
return ByValueKey(int.parse(keyValueString));
|
||||
case 'String':
|
||||
return ByValueKey(keyValueString);
|
||||
default:
|
||||
throw _createInvalidKeyValueTypeError(keyValueType);
|
||||
}
|
||||
return switch (json['keyValueType']!) {
|
||||
'int' => ByValueKey(int.parse(keyValueString)),
|
||||
'String' => ByValueKey(keyValueString),
|
||||
final String keyValueType => throw _createInvalidKeyValueTypeError(keyValueType),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,27 +33,17 @@ import 'wait.dart';
|
||||
mixin CreateFinderFactory {
|
||||
/// Creates the flutter widget finder from [SerializableFinder].
|
||||
Finder createFinder(SerializableFinder finder) {
|
||||
final String finderType = finder.finderType;
|
||||
switch (finderType) {
|
||||
case 'ByText':
|
||||
return _createByTextFinder(finder as ByText);
|
||||
case 'ByTooltipMessage':
|
||||
return _createByTooltipMessageFinder(finder as ByTooltipMessage);
|
||||
case 'BySemanticsLabel':
|
||||
return _createBySemanticsLabelFinder(finder as BySemanticsLabel);
|
||||
case 'ByValueKey':
|
||||
return _createByValueKeyFinder(finder as ByValueKey);
|
||||
case 'ByType':
|
||||
return _createByTypeFinder(finder as ByType);
|
||||
case 'PageBack':
|
||||
return _createPageBackFinder();
|
||||
case 'Ancestor':
|
||||
return _createAncestorFinder(finder as Ancestor);
|
||||
case 'Descendant':
|
||||
return _createDescendantFinder(finder as Descendant);
|
||||
default:
|
||||
throw DriverError('Unsupported search specification type $finderType');
|
||||
}
|
||||
return switch (finder.finderType) {
|
||||
'ByText' => _createByTextFinder(finder as ByText),
|
||||
'ByTooltipMessage' => _createByTooltipMessageFinder(finder as ByTooltipMessage),
|
||||
'BySemanticsLabel' => _createBySemanticsLabelFinder(finder as BySemanticsLabel),
|
||||
'ByValueKey' => _createByValueKeyFinder(finder as ByValueKey),
|
||||
'ByType' => _createByTypeFinder(finder as ByType),
|
||||
'PageBack' => _createPageBackFinder(),
|
||||
'Ancestor' => _createAncestorFinder(finder as Ancestor),
|
||||
'Descendant' => _createDescendantFinder(finder as Descendant),
|
||||
final String type => throw DriverError('Unsupported search specification type $type'),
|
||||
};
|
||||
}
|
||||
|
||||
Finder _createByTextFinder(ByText arguments) {
|
||||
@@ -87,14 +77,11 @@ mixin CreateFinderFactory {
|
||||
}
|
||||
|
||||
Finder _createByValueKeyFinder(ByValueKey arguments) {
|
||||
switch (arguments.keyValueType) {
|
||||
case 'int':
|
||||
return find.byKey(ValueKey<int>(arguments.keyValue as int));
|
||||
case 'String':
|
||||
return find.byKey(ValueKey<String>(arguments.keyValue as String));
|
||||
default:
|
||||
throw UnimplementedError('Unsupported ByValueKey type: ${arguments.keyValueType}');
|
||||
}
|
||||
return switch (arguments.keyValueType) {
|
||||
'int' => find.byKey(ValueKey<int>(arguments.keyValue as int)),
|
||||
'String' => find.byKey(ValueKey<String>(arguments.keyValue as String)),
|
||||
_ => throw UnimplementedError('Unsupported ByValueKey type: ${arguments.keyValueType}'),
|
||||
};
|
||||
}
|
||||
|
||||
Finder _createByTypeFinder(ByType arguments) {
|
||||
@@ -155,33 +142,33 @@ mixin CommandHandlerFactory {
|
||||
|
||||
/// Deserializes the finder from JSON generated by [Command.serialize] or [CommandWithTarget.serialize].
|
||||
Future<Result> handleCommand(Command command, WidgetController prober, CreateFinderFactory finderFactory) {
|
||||
switch (command.kind) {
|
||||
case 'get_health': return _getHealth(command);
|
||||
case 'get_layer_tree': return _getLayerTree(command);
|
||||
case 'get_render_tree': return _getRenderTree(command);
|
||||
case 'enter_text': return _enterText(command);
|
||||
case 'send_text_input_action': return _sendTextInputAction(command);
|
||||
case 'get_text': return _getText(command, finderFactory);
|
||||
case 'request_data': return _requestData(command);
|
||||
case 'scroll': return _scroll(command, prober, finderFactory);
|
||||
case 'scrollIntoView': return _scrollIntoView(command, finderFactory);
|
||||
case 'set_frame_sync': return _setFrameSync(command);
|
||||
case 'set_semantics': return _setSemantics(command);
|
||||
case 'set_text_entry_emulation': return _setTextEntryEmulation(command);
|
||||
case 'tap': return _tap(command, prober, finderFactory);
|
||||
case 'waitFor': return _waitFor(command, finderFactory);
|
||||
case 'waitForAbsent': return _waitForAbsent(command, finderFactory);
|
||||
case 'waitForTappable': return _waitForTappable(command, finderFactory);
|
||||
case 'waitForCondition': return _waitForCondition(command);
|
||||
case 'waitUntilNoTransientCallbacks': return _waitUntilNoTransientCallbacks(command);
|
||||
case 'waitUntilNoPendingFrame': return _waitUntilNoPendingFrame(command);
|
||||
case 'waitUntilFirstFrameRasterized': return _waitUntilFirstFrameRasterized(command);
|
||||
case 'get_semantics_id': return _getSemanticsId(command, finderFactory);
|
||||
case 'get_offset': return _getOffset(command, finderFactory);
|
||||
case 'get_diagnostics_tree': return _getDiagnosticsTree(command, finderFactory);
|
||||
}
|
||||
return switch (command.kind) {
|
||||
'get_health' => _getHealth(command),
|
||||
'get_layer_tree' => _getLayerTree(command),
|
||||
'get_render_tree' => _getRenderTree(command),
|
||||
'enter_text' => _enterText(command),
|
||||
'send_text_input_action' => _sendTextInputAction(command),
|
||||
'get_text' => _getText(command, finderFactory),
|
||||
'request_data' => _requestData(command),
|
||||
'scroll' => _scroll(command, prober, finderFactory),
|
||||
'scrollIntoView' => _scrollIntoView(command, finderFactory),
|
||||
'set_frame_sync' => _setFrameSync(command),
|
||||
'set_semantics' => _setSemantics(command),
|
||||
'set_text_entry_emulation' => _setTextEntryEmulation(command),
|
||||
'tap' => _tap(command, prober, finderFactory),
|
||||
'waitFor' => _waitFor(command, finderFactory),
|
||||
'waitForAbsent' => _waitForAbsent(command, finderFactory),
|
||||
'waitForTappable' => _waitForTappable(command, finderFactory),
|
||||
'waitForCondition' => _waitForCondition(command),
|
||||
'waitUntilNoTransientCallbacks' => _waitUntilNoTransientCallbacks(command),
|
||||
'waitUntilNoPendingFrame' => _waitUntilNoPendingFrame(command),
|
||||
'waitUntilFirstFrameRasterized' => _waitUntilFirstFrameRasterized(command),
|
||||
'get_semantics_id' => _getSemanticsId(command, finderFactory),
|
||||
'get_offset' => _getOffset(command, finderFactory),
|
||||
'get_diagnostics_tree' => _getDiagnosticsTree(command, finderFactory),
|
||||
final String kind => throw DriverError('Unsupported command kind $kind'),
|
||||
};
|
||||
|
||||
throw DriverError('Unsupported command kind ${command.kind}');
|
||||
}
|
||||
|
||||
Future<Health> _getHealth(Command command) async => const Health(HealthStatus.ok);
|
||||
@@ -342,19 +329,13 @@ mixin CommandHandlerFactory {
|
||||
final Finder finder = await waitForElement(finderFactory.createFinder(getOffsetCommand.finder));
|
||||
final Element element = finder.evaluate().single;
|
||||
final RenderBox box = (element.renderObject as RenderBox?)!;
|
||||
Offset localPoint;
|
||||
switch (getOffsetCommand.offsetType) {
|
||||
case OffsetType.topLeft:
|
||||
localPoint = Offset.zero;
|
||||
case OffsetType.topRight:
|
||||
localPoint = box.size.topRight(Offset.zero);
|
||||
case OffsetType.bottomLeft:
|
||||
localPoint = box.size.bottomLeft(Offset.zero);
|
||||
case OffsetType.bottomRight:
|
||||
localPoint = box.size.bottomRight(Offset.zero);
|
||||
case OffsetType.center:
|
||||
localPoint = box.size.center(Offset.zero);
|
||||
}
|
||||
final Offset localPoint = switch (getOffsetCommand.offsetType) {
|
||||
OffsetType.topLeft => Offset.zero,
|
||||
OffsetType.topRight => box.size.topRight(Offset.zero),
|
||||
OffsetType.bottomLeft => box.size.bottomLeft(Offset.zero),
|
||||
OffsetType.bottomRight => box.size.bottomRight(Offset.zero),
|
||||
OffsetType.center => box.size.center(Offset.zero),
|
||||
};
|
||||
final Offset globalPoint = box.localToGlobal(localPoint);
|
||||
return GetOffsetResult(dx: globalPoint.dx, dy: globalPoint.dy);
|
||||
}
|
||||
@@ -363,13 +344,10 @@ mixin CommandHandlerFactory {
|
||||
final GetDiagnosticsTree diagnosticsCommand = command as GetDiagnosticsTree;
|
||||
final Finder finder = await waitForElement(finderFactory.createFinder(diagnosticsCommand.finder));
|
||||
final Element element = finder.evaluate().single;
|
||||
DiagnosticsNode diagnosticsNode;
|
||||
switch (diagnosticsCommand.diagnosticsType) {
|
||||
case DiagnosticsType.renderObject:
|
||||
diagnosticsNode = element.renderObject!.toDiagnosticsNode();
|
||||
case DiagnosticsType.widget:
|
||||
diagnosticsNode = element.toDiagnosticsNode();
|
||||
}
|
||||
final DiagnosticsNode diagnosticsNode = switch (diagnosticsCommand.diagnosticsType) {
|
||||
DiagnosticsType.renderObject => element.renderObject!.toDiagnosticsNode(),
|
||||
DiagnosticsType.widget => element.toDiagnosticsNode(),
|
||||
};
|
||||
return DiagnosticsTreeResult(diagnosticsNode.toJsonMap(DiagnosticsSerializationDelegate(
|
||||
subtreeDepth: diagnosticsCommand.subtreeDepth,
|
||||
includeProperties: diagnosticsCommand.includeProperties,
|
||||
|
||||
@@ -195,19 +195,14 @@ class CombinedCondition extends SerializableWaitCondition {
|
||||
|
||||
/// Parses a [SerializableWaitCondition] or its subclass from the given [json] map.
|
||||
SerializableWaitCondition _deserialize(Map<String, String> json) {
|
||||
final String conditionName = json['conditionName']!;
|
||||
switch (conditionName) {
|
||||
case 'NoTransientCallbacksCondition':
|
||||
return NoTransientCallbacks.deserialize(json);
|
||||
case 'NoPendingFrameCondition':
|
||||
return NoPendingFrame.deserialize(json);
|
||||
case 'FirstFrameRasterizedCondition':
|
||||
return FirstFrameRasterized.deserialize(json);
|
||||
case 'NoPendingPlatformMessagesCondition':
|
||||
return NoPendingPlatformMessages.deserialize(json);
|
||||
case 'CombinedCondition':
|
||||
return CombinedCondition.deserialize(json);
|
||||
}
|
||||
throw SerializationException(
|
||||
'Unsupported wait condition $conditionName in the JSON string $json');
|
||||
return switch (json['conditionName']!) {
|
||||
'NoTransientCallbacksCondition' => NoTransientCallbacks.deserialize(json),
|
||||
'NoPendingFrameCondition' => NoPendingFrame.deserialize(json),
|
||||
'FirstFrameRasterizedCondition' => FirstFrameRasterized.deserialize(json),
|
||||
'NoPendingPlatformMessagesCondition' => NoPendingPlatformMessages.deserialize(json),
|
||||
'CombinedCondition' => CombinedCondition.deserialize(json),
|
||||
final String condition => throw SerializationException(
|
||||
'Unsupported wait condition $condition in the JSON string $json',
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -121,16 +121,12 @@ class ProfilingSummarizer {
|
||||
}
|
||||
|
||||
static ProfileType _getProfileType(String? eventName) {
|
||||
switch (eventName) {
|
||||
case _kCpuProfile:
|
||||
return ProfileType.CPU;
|
||||
case _kGpuProfile:
|
||||
return ProfileType.GPU;
|
||||
case _kMemoryProfile:
|
||||
return ProfileType.Memory;
|
||||
default:
|
||||
throw Exception('Invalid profiling event: $eventName.');
|
||||
}
|
||||
return switch (eventName) {
|
||||
_kCpuProfile => ProfileType.CPU,
|
||||
_kGpuProfile => ProfileType.GPU,
|
||||
_kMemoryProfile => ProfileType.Memory,
|
||||
_ => throw Exception('Invalid profiling event: $eventName.'),
|
||||
};
|
||||
}
|
||||
|
||||
double _getProfileValue(ProfileType profileType, TimelineEvent e) {
|
||||
|
||||
@@ -601,19 +601,17 @@ const Duration _kPauseBetweenIsolateRefresh = Duration(milliseconds: 100);
|
||||
// See `timeline_streams` in
|
||||
// https://github.com/dart-lang/sdk/blob/main/runtime/vm/timeline.cc
|
||||
List<String> _timelineStreamsToString(List<TimelineStream> streams) {
|
||||
return streams.map<String>((TimelineStream stream) {
|
||||
switch (stream) {
|
||||
case TimelineStream.all: return 'all';
|
||||
case TimelineStream.api: return 'API';
|
||||
case TimelineStream.compiler: return 'Compiler';
|
||||
case TimelineStream.compilerVerbose: return 'CompilerVerbose';
|
||||
case TimelineStream.dart: return 'Dart';
|
||||
case TimelineStream.debugger: return 'Debugger';
|
||||
case TimelineStream.embedder: return 'Embedder';
|
||||
case TimelineStream.gc: return 'GC';
|
||||
case TimelineStream.isolate: return 'Isolate';
|
||||
case TimelineStream.vm: return 'VM';
|
||||
}
|
||||
return streams.map<String>((TimelineStream stream) => switch (stream) {
|
||||
TimelineStream.all => 'all',
|
||||
TimelineStream.api => 'API',
|
||||
TimelineStream.dart => 'Dart',
|
||||
TimelineStream.debugger => 'Debugger',
|
||||
TimelineStream.embedder => 'Embedder',
|
||||
TimelineStream.gc => 'GC',
|
||||
TimelineStream.isolate => 'Isolate',
|
||||
TimelineStream.vm => 'VM',
|
||||
TimelineStream.compiler => 'Compiler',
|
||||
TimelineStream.compilerVerbose => 'CompilerVerbose',
|
||||
}).toList();
|
||||
}
|
||||
|
||||
|
||||
@@ -177,18 +177,12 @@ class _InternalCombinedCondition implements WaitCondition {
|
||||
/// Parses a [WaitCondition] or its subclass from the given serializable [waitCondition].
|
||||
WaitCondition deserializeCondition(SerializableWaitCondition waitCondition) {
|
||||
final String conditionName = waitCondition.conditionName;
|
||||
switch (conditionName) {
|
||||
case 'NoTransientCallbacksCondition':
|
||||
return _InternalNoTransientCallbacksCondition.deserialize(waitCondition);
|
||||
case 'NoPendingFrameCondition':
|
||||
return _InternalNoPendingFrameCondition.deserialize(waitCondition);
|
||||
case 'FirstFrameRasterizedCondition':
|
||||
return _InternalFirstFrameRasterizedCondition.deserialize(waitCondition);
|
||||
case 'NoPendingPlatformMessagesCondition':
|
||||
return _InternalNoPendingPlatformMessagesCondition.deserialize(waitCondition);
|
||||
case 'CombinedCondition':
|
||||
return _InternalCombinedCondition.deserialize(waitCondition);
|
||||
}
|
||||
throw SerializationException(
|
||||
'Unsupported wait condition $conditionName in ${waitCondition.serialize()}');
|
||||
return switch (conditionName) {
|
||||
'NoTransientCallbacksCondition' => _InternalNoTransientCallbacksCondition.deserialize(waitCondition),
|
||||
'NoPendingFrameCondition' => _InternalNoPendingFrameCondition.deserialize(waitCondition),
|
||||
'FirstFrameRasterizedCondition' => _InternalFirstFrameRasterizedCondition.deserialize(waitCondition),
|
||||
'NoPendingPlatformMessagesCondition' => _InternalNoPendingPlatformMessagesCondition.deserialize(waitCondition),
|
||||
'CombinedCondition' => _InternalCombinedCondition.deserialize(waitCondition),
|
||||
_ => throw SerializationException('Unsupported wait condition $conditionName in ${waitCondition.serialize()}'),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user