Skip to content

Commit

Permalink
Merge pull request #1 from epicery/feat/add-const-for-query-path-params
Browse files Browse the repository at this point in the history
feat: add classes containing path params and query params as const
  • Loading branch information
meynety authored Aug 30, 2024
2 parents 1520ffb + f61c6d8 commit a844fb5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
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

0 comments on commit a844fb5

Please sign in to comment.