Skip to content

Commit

Permalink
refactor: refactor enum projections (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
halildurmus committed Jul 29, 2023
1 parent d7e850d commit 8b2830c
Show file tree
Hide file tree
Showing 199 changed files with 273 additions and 655 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ enum LearningModelDeviceKind implements WinRTEnum {
const LearningModelDeviceKind(this.value);

factory LearningModelDeviceKind.from(int value) =>
LearningModelDeviceKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
LearningModelDeviceKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ enum LearningModelFeatureKind implements WinRTEnum {
const LearningModelFeatureKind(this.value);

factory LearningModelFeatureKind.from(int value) =>
LearningModelFeatureKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
LearningModelFeatureKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ enum LearningModelPixelRange implements WinRTEnum {
const LearningModelPixelRange(this.value);

factory LearningModelPixelRange.from(int value) =>
LearningModelPixelRange.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
LearningModelPixelRange.values.byValue(value);
}
5 changes: 1 addition & 4 deletions packages/windows_ai/lib/src/machinelearning/tensorkind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,5 @@ enum TensorKind implements WinRTEnum {

const TensorKind(this.value);

factory TensorKind.from(int value) =>
TensorKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
factory TensorKind.from(int value) => TensorKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ enum AppExecutionContext implements WinRTEnum {
const AppExecutionContext(this.value);

factory AppExecutionContext.from(int value) =>
AppExecutionContext.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
AppExecutionContext.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,5 @@ enum AppInstallerPolicySource implements WinRTEnum {
const AppInstallerPolicySource(this.value);

factory AppInstallerPolicySource.from(int value) =>
AppInstallerPolicySource.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
AppInstallerPolicySource.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ enum PackageContentGroupState implements WinRTEnum {
const PackageContentGroupState(this.value);

factory PackageContentGroupState.from(int value) =>
PackageContentGroupState.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
PackageContentGroupState.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ enum PackageRelationship implements WinRTEnum {
const PackageRelationship(this.value);

factory PackageRelationship.from(int value) =>
PackageRelationship.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
PackageRelationship.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ enum PackageSignatureKind implements WinRTEnum {
const PackageSignatureKind(this.value);

factory PackageSignatureKind.from(int value) =>
PackageSignatureKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
PackageSignatureKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ enum PackageUpdateAvailability implements WinRTEnum {
const PackageUpdateAvailability(this.value);

factory PackageUpdateAvailability.from(int value) =>
PackageUpdateAvailability.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
PackageUpdateAvailability.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ enum StartupTaskState implements WinRTEnum {
const StartupTaskState(this.value);

factory StartupTaskState.from(int value) =>
StartupTaskState.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
StartupTaskState.values.byValue(value);
}
4 changes: 1 addition & 3 deletions packages/windows_data/lib/src/json/jsonerrorstatus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ enum JsonErrorStatus implements WinRTEnum {
const JsonErrorStatus(this.value);

factory JsonErrorStatus.from(int value) =>
JsonErrorStatus.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
JsonErrorStatus.values.byValue(value);
}
5 changes: 1 addition & 4 deletions packages/windows_data/lib/src/json/jsonvaluetype.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,5 @@ enum JsonValueType implements WinRTEnum {

const JsonValueType(this.value);

factory JsonValueType.from(int value) =>
JsonValueType.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
factory JsonValueType.from(int value) => JsonValueType.values.byValue(value);
}
5 changes: 1 addition & 4 deletions packages/windows_data/lib/src/xml/dom/nodetype.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,5 @@ enum NodeType implements WinRTEnum {

const NodeType(this.value);

factory NodeType.from(int value) =>
NodeType.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
factory NodeType.from(int value) => NodeType.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ enum DisplayMonitorConnectionKind implements WinRTEnum {
const DisplayMonitorConnectionKind(this.value);

factory DisplayMonitorConnectionKind.from(int value) =>
DisplayMonitorConnectionKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DisplayMonitorConnectionKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ enum DisplayMonitorDescriptorKind implements WinRTEnum {
const DisplayMonitorDescriptorKind(this.value);

factory DisplayMonitorDescriptorKind.from(int value) =>
DisplayMonitorDescriptorKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DisplayMonitorDescriptorKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,5 @@ enum DisplayMonitorPhysicalConnectorKind implements WinRTEnum {
const DisplayMonitorPhysicalConnectorKind(this.value);

factory DisplayMonitorPhysicalConnectorKind.from(int value) =>
DisplayMonitorPhysicalConnectorKind.values.firstWhere(
(e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DisplayMonitorPhysicalConnectorKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ enum DisplayMonitorUsageKind implements WinRTEnum {
const DisplayMonitorUsageKind(this.value);

factory DisplayMonitorUsageKind.from(int value) =>
DisplayMonitorUsageKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DisplayMonitorUsageKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,5 @@ enum DeviceClass implements WinRTEnum {

const DeviceClass(this.value);

factory DeviceClass.from(int value) =>
DeviceClass.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
factory DeviceClass.from(int value) => DeviceClass.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@ enum DeviceInformationKind implements WinRTEnum {
const DeviceInformationKind(this.value);

factory DeviceInformationKind.from(int value) =>
DeviceInformationKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DeviceInformationKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ enum DevicePairingProtectionLevel implements WinRTEnum {
const DevicePairingProtectionLevel(this.value);

factory DevicePairingProtectionLevel.from(int value) =>
DevicePairingProtectionLevel.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DevicePairingProtectionLevel.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,5 @@ enum DevicePairingResultStatus implements WinRTEnum {
const DevicePairingResultStatus(this.value);

factory DevicePairingResultStatus.from(int value) =>
DevicePairingResultStatus.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DevicePairingResultStatus.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ enum DeviceUnpairingResultStatus implements WinRTEnum {
const DeviceUnpairingResultStatus(this.value);

factory DeviceUnpairingResultStatus.from(int value) =>
DeviceUnpairingResultStatus.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DeviceUnpairingResultStatus.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ enum DeviceWatcherEventKind implements WinRTEnum {
const DeviceWatcherEventKind(this.value);

factory DeviceWatcherEventKind.from(int value) =>
DeviceWatcherEventKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DeviceWatcherEventKind.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ enum DeviceWatcherStatus implements WinRTEnum {
const DeviceWatcherStatus(this.value);

factory DeviceWatcherStatus.from(int value) =>
DeviceWatcherStatus.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
DeviceWatcherStatus.values.byValue(value);
}
5 changes: 1 addition & 4 deletions packages/windows_devices/lib/src/enumeration/panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ enum Panel implements WinRTEnum {

const Panel(this.value);

factory Panel.from(int value) =>
Panel.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
factory Panel.from(int value) => Panel.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ enum AltitudeReferenceSystem implements WinRTEnum {
const AltitudeReferenceSystem(this.value);

factory AltitudeReferenceSystem.from(int value) =>
AltitudeReferenceSystem.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
AltitudeReferenceSystem.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ enum GeolocationAccessStatus implements WinRTEnum {
const GeolocationAccessStatus(this.value);

factory GeolocationAccessStatus.from(int value) =>
GeolocationAccessStatus.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
GeolocationAccessStatus.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,5 @@ enum GeoshapeType implements WinRTEnum {

const GeoshapeType(this.value);

factory GeoshapeType.from(int value) =>
GeoshapeType.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
factory GeoshapeType.from(int value) => GeoshapeType.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ enum PositionAccuracy implements WinRTEnum {
const PositionAccuracy(this.value);

factory PositionAccuracy.from(int value) =>
PositionAccuracy.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
PositionAccuracy.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@ enum PositionSource implements WinRTEnum {
const PositionSource(this.value);

factory PositionSource.from(int value) =>
PositionSource.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
PositionSource.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ enum PositionStatus implements WinRTEnum {
const PositionStatus(this.value);

factory PositionStatus.from(int value) =>
PositionStatus.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
PositionStatus.values.byValue(value);
}
5 changes: 1 addition & 4 deletions packages/windows_devices/lib/src/gpio/gpiopinedge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@ enum GpioPinEdge implements WinRTEnum {

const GpioPinEdge(this.value);

factory GpioPinEdge.from(int value) =>
GpioPinEdge.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
factory GpioPinEdge.from(int value) => GpioPinEdge.values.byValue(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ enum PedometerStepKind implements WinRTEnum {
const PedometerStepKind(this.value);

factory PedometerStepKind.from(int value) =>
PedometerStepKind.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
PedometerStepKind.values.byValue(value);
}
5 changes: 1 addition & 4 deletions packages/windows_foundation/lib/src/asyncstatus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,5 @@ enum AsyncStatus implements WinRTEnum {

const AsyncStatus(this.value);

factory AsyncStatus.from(int value) =>
AsyncStatus.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
factory AsyncStatus.from(int value) => AsyncStatus.values.byValue(value);
}
5 changes: 1 addition & 4 deletions packages/windows_foundation/lib/src/propertytype.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,5 @@ enum PropertyType implements WinRTEnum {

const PropertyType(this.value);

factory PropertyType.from(int value) =>
PropertyType.values.firstWhere((e) => e.value == value,
orElse: () => throw ArgumentError.value(
value, 'value', 'No enum value with that value'));
factory PropertyType.from(int value) => PropertyType.values.byValue(value);
}
13 changes: 13 additions & 0 deletions packages/windows_foundation/lib/src/winrt_enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ abstract class WinRTEnum {
_name != null ? '$runtimeType.$_name' : '$runtimeType(value: $value)';
}

extension WinRTEnumHelper<T extends WinRTEnum> on Iterable<T> {
/// Finds the enum value in this list with value [value].
///
/// Goes through this collection looking for an enum with value [value].
/// Returns the first value with the given value. Such a value must be found.
T byValue(int value) {
for (final enumValue in this) {
if (enumValue.value == value) return enumValue;
}
throw ArgumentError.value(value, 'value', 'No enum value with that value');
}
}

/// The base class that all WinRT Flags enumerations implement.
abstract base class WinRTFlagsEnum<T extends WinRTEnum> extends WinRTEnum {
const WinRTFlagsEnum(super.value, {super.name});
Expand Down
39 changes: 39 additions & 0 deletions packages/windows_foundation/test/winrt_enum_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023, Dart | Windows. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

@TestOn('windows')

import 'package:test/test.dart';
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/windows_foundation.dart';
import 'package:windows_storage/windows_storage.dart';

void main() {
test('WinRTEnum toString', () {
expect(AsyncStatus.completed.toString(), equals('AsyncStatus.completed'));
});

group('List<WinRTEnum> byValue', () {
test('finds enum values', () {
expect(AsyncStatus.values.byValue(0), equals(AsyncStatus.started));
expect(AsyncStatus.values.byValue(3), equals(AsyncStatus.error));
});

test('throws ArgumentError for invalid values', () {
expect(() => AsyncStatus.values.byValue(4), throwsArgumentError);
});
});

group('WinRTFlagsEnum toString', () {
test('(1)', () {
expect(FileAttributes.directory.toString(),
equals('FileAttributes.directory'));
});

test('(2)', () {
expect((FileAttributes.directory | FileAttributes.readOnly).toString(),
equals('FileAttributes(value: 17)'));
});
});
}
Loading

0 comments on commit 8b2830c

Please sign in to comment.