Skip to content

[go_router_builder] Generate the enum map for enum used in iterable #3415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.6

* Generates the const enum map for enums used in `List`, `Set` and `Iterable`.

## 1.1.5

* Replaces unnecessary Flutter SDK constraint with corresponding Dart
Expand Down
7 changes: 7 additions & 0 deletions packages/go_router_builder/example/lib/all_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,19 @@ class IterableRoute extends GoRouteData {
this.stringIterableField,
this.boolIterableField,
this.enumIterableField,
this.enumOnlyInIterableField,
this.intListField,
this.doubleListField,
this.stringListField,
this.boolListField,
this.enumListField,
this.enumOnlyInListField,
this.intSetField,
this.doubleSetField,
this.stringSetField,
this.boolSetField,
this.enumSetField,
this.enumOnlyInSetField,
});

final Iterable<int>? intIterableField;
Expand All @@ -329,6 +332,10 @@ class IterableRoute extends GoRouteData {
final List<SportDetails>? enumListField;
final Set<SportDetails>? enumSetField;

final Iterable<CookingRecipe>? enumOnlyInIterableField;
final List<CookingRecipe>? enumOnlyInListField;
final Set<CookingRecipe>? enumOnlyInSetField;

@override
Widget build(BuildContext context, GoRouterState state) =>
const BasePage<String>(
Expand Down
27 changes: 27 additions & 0 deletions packages/go_router_builder/example/lib/all_types.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/go_router_builder/example/lib/shared/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ enum SportDetails {
final bool hasNet;
}

/// An enum used only in iterables.
enum CookingRecipe {
burger,
pizza,
tacos,
}

/// sample Person class
class Person {
Person({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@ void main() {
expect(find.text('Query param: https://dart.dev'), findsOneWidget);

IterableRoute(
enumIterableField: <SportDetails>[SportDetails.football],
intListField: <int>[1, 2, 3],
enumOnlyInSetField: <CookingRecipe>{
CookingRecipe.burger,
CookingRecipe.pizza,
},
).go(scaffoldState.context);
await tester.pumpAndSettle();
expect(find.text('IterableRoute'), findsOneWidget);
expect(
find.text(
'/iterable-route?int-list-field=1&int-list-field=2&int-list-field=3'),
'/iterable-route?enum-iterable-field=football&int-list-field=1&int-list-field=2&int-list-field=3&enum-only-in-set-field=burger&enum-only-in-set-field=pizza'),
findsOneWidget);
});
}
11 changes: 9 additions & 2 deletions packages/go_router_builder/lib/src/route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,15 @@ GoRoute get $_routeGetterName => ${_routeDefinition()};
...routeDef._ctorParams,
...routeDef._ctorQueryParams,
]) {
if (ctorParam.type.isEnum) {
enumParamTypes.add(ctorParam.type as InterfaceType);
DartType potentialEnumType = ctorParam.type;
if (potentialEnumType is ParameterizedType &&
(ctorParam.type as ParameterizedType).typeArguments.isNotEmpty) {
potentialEnumType =
(ctorParam.type as ParameterizedType).typeArguments.first;
}

if (potentialEnumType.isEnum) {
enumParamTypes.add(potentialEnumType as InterfaceType);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: go_router_builder
description: >-
A builder that supports generated strongly-typed route helpers for
package:go_router
version: 1.1.5
version: 1.1.6
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22

Expand Down
1 change: 1 addition & 0 deletions packages/go_router_builder/test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ const Set<String> _expectedAnnotatedTests = <String>{
'EnumParam',
'DefaultValueRoute',
'NullableDefaultValueRoute',
'IterableWithEnumRoute'
};
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,57 @@ class NullableDefaultValueRoute extends GoRouteData {
NullableDefaultValueRoute({this.param = 0});
final int? param;
}

@ShouldGenerate(r'''
GoRoute get $iterableWithEnumRoute => GoRouteData.$route(
path: '/iterable-with-enum',
factory: $IterableWithEnumRouteExtension._fromState,
);

extension $IterableWithEnumRouteExtension on IterableWithEnumRoute {
static IterableWithEnumRoute _fromState(GoRouterState state) =>
IterableWithEnumRoute(
param: state.queryParametersAll['param']
?.map(_$EnumOnlyUsedInIterableEnumMap._$fromName),
);

String get location => GoRouteData.$location(
'/iterable-with-enum',
queryParams: {
if (param != null)
'param':
param?.map((e) => _$EnumOnlyUsedInIterableEnumMap[e]).toList(),
},
);

void go(BuildContext context) => context.go(location);

void push(BuildContext context) => context.push(location);

void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
}

const _$EnumOnlyUsedInIterableEnumMap = {
EnumOnlyUsedInIterable.a: 'a',
EnumOnlyUsedInIterable.b: 'b',
EnumOnlyUsedInIterable.c: 'c',
};

extension<T extends Enum> on Map<T, String> {
T _$fromName(String value) =>
entries.singleWhere((element) => element.value == value).key;
}
''')
@TypedGoRoute<IterableWithEnumRoute>(path: '/iterable-with-enum')
class IterableWithEnumRoute extends GoRouteData {
IterableWithEnumRoute({this.param});

final Iterable<EnumOnlyUsedInIterable>? param;
}

enum EnumOnlyUsedInIterable {
a,
b,
c,
}