-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[go_router_builder] Support extension types #9458
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
Changes from 4 commits
688ec24
bf334dc
8cf27d2
af33a0b
61901a3
53e30bb
1a84b3a
9b9afeb
4a4d892
bcc5d26
c57f3d0
57cf662
5910235
ffdb723
9fc1ec2
b8758e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| ## NEXT | ||
| ## 3.0.2 | ||
|
|
||
| - Restricts `build` to versions less than 2.5.0. | ||
| - Support `extension type`. | ||
|
|
||
| ## 3.0.1 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ const List<_TypeHelper> _helpers = <_TypeHelper>[ | |
| _TypeHelperDateTime(), | ||
| _TypeHelperDouble(), | ||
| _TypeHelperEnum(), | ||
| _TypeHelperExtensionType(), | ||
| _TypeHelperInt(), | ||
| _TypeHelperNum(), | ||
| _TypeHelperString(), | ||
|
|
@@ -263,6 +264,49 @@ class _TypeHelperEnum extends _TypeHelperWithHelper { | |
| bool _matchesType(DartType type) => type.isEnum; | ||
| } | ||
|
|
||
| /// A type helper for extension types. | ||
| class _TypeHelperExtensionType extends _TypeHelper { | ||
| const _TypeHelperExtensionType(); | ||
|
|
||
| @override | ||
| String _decode( | ||
| ParameterElement parameterElement, Set<String> pathParameters) { | ||
| final DartType paramType = parameterElement.type; | ||
| if (paramType.isNullableType && parameterElement.hasDefaultValue) { | ||
| throw NullableDefaultValueError(parameterElement); | ||
| } | ||
|
|
||
| final String stateValue = | ||
| 'state.${_stateValueAccess(parameterElement, pathParameters)}'; | ||
| final String castType; | ||
| if (paramType.isNullableType || parameterElement.hasDefaultValue) { | ||
| castType = '$paramType${paramType.isNullableType ? '' : '?'}'; | ||
| } else { | ||
| castType = '$paramType'; | ||
| } | ||
|
|
||
| final DartType extensionTypeErasure = paramType.extensionTypeErasure; | ||
| if (extensionTypeErasure.isDartCoreString) { | ||
| return '$stateValue as $castType'; | ||
| } | ||
|
|
||
| final String parseTypeName = | ||
|
||
| withoutNullability(extensionTypeErasure.getDisplayString()); | ||
| if (paramType.isNullableType || parameterElement.hasDefaultValue) { | ||
| return "$parseTypeName.tryParse($stateValue ?? '') as $castType"; | ||
| } else { | ||
| return '$parseTypeName.parse($stateValue) as $castType'; | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| String _encode(String fieldName, DartType type) => | ||
| '$fieldName${type.ensureNotNull}.toString()'; | ||
|
|
||
| @override | ||
| bool _matchesType(DartType type) => type != type.extensionTypeErasure; | ||
| } | ||
|
|
||
| class _TypeHelperInt extends _TypeHelperWithHelper { | ||
| const _TypeHelperInt(); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:go_router/go_router.dart'; | ||
|
|
||
| mixin _$ExtenstionTypeParam {} | ||
| mixin _$ExtenstionTypeStringParam {} | ||
| mixin _$ExtenstionTypeStringDefaultParam {} | ||
| mixin _$ExtenstionTypeIntParam {} | ||
| mixin _$ExtenstionTypeIntDefaultParam {} | ||
| mixin _$ExtenstionTypeDoubleParam {} | ||
| mixin _$ExtenstionTypeNumParam {} | ||
| mixin _$ExtenstionTypeBoolParam {} | ||
| mixin _$ExtenstionTypeBigIntParam {} | ||
| mixin _$ExtenstionTypeDateTimeParam {} | ||
|
|
||
| @TypedGoRoute<ExtenstionTypeParam>(path: '/', routes: <TypedRoute<RouteData>>[ | ||
| TypedGoRoute<ExtenstionTypeStringParam>(path: 'string/:s'), | ||
| TypedGoRoute<ExtenstionTypeStringDefaultParam>(path: 'string_default/:s'), | ||
| TypedGoRoute<ExtenstionTypeIntParam>(path: 'int/:x'), | ||
| TypedGoRoute<ExtenstionTypeIntDefaultParam>(path: 'int_default/:x'), | ||
| TypedGoRoute<ExtenstionTypeDoubleParam>(path: 'double/:d'), | ||
| TypedGoRoute<ExtenstionTypeNumParam>(path: 'num/:n'), | ||
| TypedGoRoute<ExtenstionTypeBoolParam>(path: 'bool/:b'), | ||
| TypedGoRoute<ExtenstionTypeBigIntParam>(path: 'bigint/:bi'), | ||
| TypedGoRoute<ExtenstionTypeDateTimeParam>(path: 'datetime/:dt'), | ||
| ]) | ||
| class ExtenstionTypeParam extends GoRouteData with _$ExtenstionTypeParam { | ||
| ExtenstionTypeParam(); | ||
| } | ||
|
|
||
| class ExtenstionTypeStringParam extends GoRouteData | ||
| with _$ExtenstionTypeStringParam { | ||
| ExtenstionTypeStringParam({ | ||
| required this.s, | ||
| required this.requiredValue, | ||
| this.optionalNullableValue, | ||
| this.optionalDefaultValue = const StringExtensionType('default'), | ||
| }); | ||
| final StringExtensionType s; | ||
| final StringExtensionType requiredValue; | ||
| final StringExtensionType? optionalNullableValue; | ||
| final StringExtensionType optionalDefaultValue; | ||
| } | ||
|
|
||
| class ExtenstionTypeStringDefaultParam extends GoRouteData | ||
| with _$ExtenstionTypeStringDefaultParam { | ||
| ExtenstionTypeStringDefaultParam({ | ||
| this.s = const StringExtensionType('default'), | ||
| }); | ||
| final StringExtensionType s; | ||
| } | ||
|
|
||
| class ExtenstionTypeIntParam extends GoRouteData with _$ExtenstionTypeIntParam { | ||
| ExtenstionTypeIntParam({ | ||
| required this.x, | ||
| required this.requiredValue, | ||
| this.optionalNullableValue, | ||
| this.optionalDefaultValue = const IntExtensionType(42), | ||
| }); | ||
| final IntExtensionType x; | ||
| final IntExtensionType requiredValue; | ||
| final IntExtensionType? optionalNullableValue; | ||
| final IntExtensionType optionalDefaultValue; | ||
| } | ||
|
|
||
| class ExtenstionTypeIntDefaultParam extends GoRouteData | ||
| with _$ExtenstionTypeIntDefaultParam { | ||
| ExtenstionTypeIntDefaultParam({ | ||
| this.x = const IntExtensionType(42), | ||
| }); | ||
| final IntExtensionType x; | ||
| } | ||
|
|
||
| class ExtenstionTypeDoubleParam extends GoRouteData | ||
| with _$ExtenstionTypeDoubleParam { | ||
| ExtenstionTypeDoubleParam({ | ||
| required this.d, | ||
| required this.requiredValue, | ||
| this.optionalNullableValue, | ||
| this.optionalDefaultValue = const DoubleExtensionType(3.14), | ||
| }); | ||
| final DoubleExtensionType d; | ||
| final DoubleExtensionType requiredValue; | ||
| final DoubleExtensionType? optionalNullableValue; | ||
| final DoubleExtensionType optionalDefaultValue; | ||
| } | ||
|
|
||
| class ExtenstionTypeNumParam extends GoRouteData with _$ExtenstionTypeNumParam { | ||
| ExtenstionTypeNumParam({ | ||
| required this.n, | ||
| required this.requiredValue, | ||
| this.optionalNullableValue, | ||
| this.optionalDefaultValue = const NumExtensionType(3.14), | ||
| }); | ||
| final NumExtensionType n; | ||
| final NumExtensionType requiredValue; | ||
| final NumExtensionType? optionalNullableValue; | ||
| final NumExtensionType optionalDefaultValue; | ||
| } | ||
|
|
||
| class ExtenstionTypeBoolParam extends GoRouteData | ||
| with _$ExtenstionTypeBoolParam { | ||
| ExtenstionTypeBoolParam({ | ||
| required this.b, | ||
| required this.requiredValue, | ||
| this.optionalNullableValue, | ||
| this.optionalDefaultValue = const BoolExtensionType(true), | ||
| }); | ||
| final BoolExtensionType b; | ||
| final BoolExtensionType requiredValue; | ||
| final BoolExtensionType? optionalNullableValue; | ||
| final BoolExtensionType optionalDefaultValue; | ||
| } | ||
|
|
||
| class ExtenstionTypeBigIntParam extends GoRouteData | ||
| with _$ExtenstionTypeBigIntParam { | ||
| ExtenstionTypeBigIntParam({ | ||
| required this.bi, | ||
| required this.requiredValue, | ||
| this.optionalValue, | ||
| this.optionalNullableValue, | ||
| }); | ||
| final BigIntExtensionType bi; | ||
| final BigIntExtensionType requiredValue; | ||
| final BigIntExtensionType? optionalValue; | ||
| final BigIntExtensionType? optionalNullableValue; | ||
| } | ||
|
|
||
| class ExtenstionTypeDateTimeParam extends GoRouteData | ||
| with _$ExtenstionTypeDateTimeParam { | ||
| ExtenstionTypeDateTimeParam({ | ||
| required this.dt, | ||
| required this.optionalValue, | ||
| this.optionalNullableValue, | ||
| }); | ||
| final DateTimeExtensionType dt; | ||
| final DateTimeExtensionType optionalValue; | ||
| final DateTimeExtensionType? optionalNullableValue; | ||
| } | ||
|
|
||
| extension type const StringExtensionType(String value) {} | ||
| extension type const IntExtensionType(int value) {} | ||
| extension type const DoubleExtensionType(double value) {} | ||
| extension type const NumExtensionType(num value) {} | ||
| extension type const BoolExtensionType(bool value) {} | ||
| extension type const BigIntExtensionType(BigInt value) {} | ||
| extension type const DateTimeExtensionType(DateTime value) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a new example in
example/folder