Skip to content

Commit

Permalink
fix(smithy): Proper enum support (aws-amplify#4)
Browse files Browse the repository at this point in the history
* fix: Proper enum fixes

* chore: Update goldens

* Add test

* Remove orElse from enum helpers

* Update goldens
  • Loading branch information
dnys1 authored and Dillon Nys committed Jul 28, 2022
1 parent e4b6ba1 commit 1b66d37
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
6 changes: 1 addition & 5 deletions packages/smithy/lib/src/types/enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ import 'package:smithy/smithy.dart';
/// }
/// }
/// ```
abstract class SmithyEnum<T extends SmithyEnum<T>>
with AWSEquatable<SmithyEnum<T>>, AWSSerializable {
abstract class SmithyEnum<T extends SmithyEnum<T>> with AWSSerializable {
const SmithyEnum(this.index, this.name, this.value);
const SmithyEnum.sdkUnknown(this.value)
: index = -1,
Expand All @@ -51,9 +50,6 @@ abstract class SmithyEnum<T extends SmithyEnum<T>>
final String name;
final String value;

@override
List<Object> get props => [T, index, name, value];

@override
String toJson() => value;

Expand Down
78 changes: 78 additions & 0 deletions packages/smithy/test/enum_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'package:smithy/smithy.dart';
import 'package:test/test.dart';

class _MyEnum extends SmithyEnum<_MyEnum> {
const _MyEnum._(int index, String name, String value)
: super(index, name, value);

const _MyEnum._sdkUnknown(String value) : super.sdkUnknown(value);

static const bar = _MyEnum._(0, 'BAR', 'Bar');

static const baz = _MyEnum._(1, 'BAZ', 'Baz');

static const foo = _MyEnum._(2, 'FOO', 'Foo');

/// All values of [_MyEnum].
static const values = <_MyEnum>[
_MyEnum.bar,
_MyEnum.baz,
_MyEnum.foo,
];

// ignore: unused_field
static const List<SmithySerializer<_MyEnum>> serializers = [
SmithyEnumSerializer(
'_MyEnum',
values: values,
sdkUnknown: _MyEnum._sdkUnknown,
supportedProtocols: [],
)
];
}

extension _MyEnumHelpers on List<_MyEnum> {
/// Returns the value of [_MyEnum] whose name matches [name].
///
/// Throws a `StateError` if no matching value is found.
_MyEnum byName(String name) =>
firstWhere((el) => el.name.toLowerCase() == name.toLowerCase());

/// Returns the value of [_MyEnum] whose value matches [value].
_MyEnum byValue(String value) => firstWhere((el) => el.value == value);
}

void main() {
group('SmithyEnum', () {
test('can switch', () {
_MyEnum getEnum() => _MyEnum.bar;
switch (getEnum()) {
case _MyEnum.bar:
break;
default:
fail('Did not switch correctly');
}
});

test('byName', () {
expect(
_MyEnum.values.byName('foo'),
equals(_MyEnum.foo),
);
});

test('byValue', () {
expect(
_MyEnum.values.byValue('Foo'),
equals(_MyEnum.foo),
);
});

test('byValue (unknown)', () {
expect(
() => _MyEnum.values.byValue('UNKNOWN'),
throwsStateError,
);
});
});
}

0 comments on commit 1b66d37

Please sign in to comment.