Skip to content
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

feat: add classes containing path params and query params as const #1

Merged
merged 3 commits into from
Aug 30, 2024
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
54 changes: 53 additions & 1 deletion packages/go_router_builder/lib/src/route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class GoRouteConfig extends RouteBaseConfig {
if (conditions.isNotEmpty) {
line = 'if (${conditions.join(' && ')}) ';
}
line += '${escapeDartString(parameterName.kebab)}: '
line += '${formatQueryParams(parameterName)}: '
'${_encodeFor(parameterName)},';

buffer.writeln(line);
Expand Down Expand Up @@ -361,6 +361,8 @@ class GoRouteConfig extends RouteBaseConfig {
Iterable<String> classDeclarations() => <String>[
_extensionDefinition,
..._enumDeclarations(),
if (_ctorQueryParams.isNotEmpty) _queryParamsDefinition,
if (_ctorParams.isNotEmpty) _pathParamsDefinition,
];

String get _extensionDefinition => '''
Expand All @@ -383,6 +385,56 @@ extension $_extensionName on $_className {
}
''';

/// Returns a [String] representing a class named [_className]QueryParam containing every query param as const
String get _queryParamsDefinition => '''
class ${_className}QueryParam {
${_createQueryParamsConst()}
}
''';

/// Returns a [String] representing every query param of route as const named with param name
/// and with param generated name as value
///
/// ex: "static const String shopName = 'shop-name';\nstatic const String shopId = 'shop-id';"
String _createQueryParamsConst() {
String content = '';
for (final ParameterElement param in _ctorQueryParams) {
content +=
'static const String ${param.name} = ${formatQueryParams(param.name)};\n';
}
return content;
}

/// Returns a [String] representing a class named [_className]PathParam containing every path param as const
String get _pathParamsDefinition => '''
class ${_className}PathParam {
${_createPathParamsConst()}
}
''';

/// Returns a [String] representing every path param of route as const named with param name
/// and with param generated name as value
///
/// ex: "static const String shopName = 'shopName';\nstatic const String shopId = 'shopId';"
String _createPathParamsConst() {
String content = '';
for (final ParameterElement param in _ctorParams) {
content +=
'static const String ${param.name} = ${formatPathParams(param.name)};\n';
}
return content;
}

/// Returns query param [paramName] formatted in kebab case
static String formatQueryParams(String paramName) {
return escapeDartString(paramName.kebab);
}

/// Returns path param [paramName] formatted
static String formatPathParams(String paramName) {
return escapeDartString(paramName);
}

/// Returns code representing the constant maps that contain the `enum` to
/// [String] mapping for each referenced enum.
Iterable<String> _enumDeclarations() {
Expand Down
13 changes: 8 additions & 5 deletions packages/go_router_builder/lib/src/type_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:source_gen/source_gen.dart';
import 'package:source_helper/source_helper.dart';

import 'route_config.dart';

/// The name of the generated, private helper for converting [String] to [bool].
const String boolConverterHelperName = r'_$boolConverter';

Expand Down Expand Up @@ -97,9 +99,10 @@ String _stateValueAccess(ParameterElement element, Set<String> pathParameters) {

late String access;
if (pathParameters.contains(element.name)) {
access = 'pathParameters[${escapeDartString(element.name)}]';
access = 'pathParameters[${GoRouteConfig.formatPathParams(element.name)}]';
} else {
access = 'uri.queryParameters[${escapeDartString(element.name.kebab)}]';
access =
'uri.queryParameters[${GoRouteConfig.formatQueryParams(element.name)}]';
}
if (pathParameters.contains(element.name) ||
(!element.type.isNullableType && !element.hasDefaultValue)) {
Expand Down Expand Up @@ -283,11 +286,11 @@ class _TypeHelperIterable extends _TypeHelper {

return '''
state.uri.queryParametersAll[
${escapeDartString(parameterElement.name.kebab)}]
${GoRouteConfig.formatQueryParams(parameterElement.name)}]
?.map($entriesTypeDecoder)$iterableCaster''';
}
return '''
state.uri.queryParametersAll[${escapeDartString(parameterElement.name.kebab)}]''';
state.uri.queryParametersAll[${GoRouteConfig.formatQueryParams(parameterElement.name)}]''';
}

@override
Expand Down Expand Up @@ -331,7 +334,7 @@ abstract class _TypeHelperWithHelper extends _TypeHelper {
if (!pathParameters.contains(parameterName) &&
(paramType.isNullableType || parameterElement.hasDefaultValue)) {
return '$convertMapValueHelperName('
'${escapeDartString(parameterName.kebab)}, '
'${GoRouteConfig.formatQueryParams(parameterName)}, '
'state.uri.queryParameters, '
'${helperName(paramType)})';
}
Expand Down