Skip to content
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 @@
## 2.2.3

* Removes `path_to_regexp` from the dependencies.

## 2.2.2

* Bumps example go_router version and migrate example code.
Expand Down
46 changes: 46 additions & 0 deletions packages/go_router_builder/lib/src/path_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.

final RegExp _parameterRegExp = RegExp(r':(\w+)(\((?:\\.|[^\\()])+\))?');

/// Extracts the path parameters from a [pattern] such as `/user/:id`.
///
/// The path parameters can be specified by prefixing them with `:`.
///
/// For example:
///
/// ```dart
/// final pattern = '/user/:id/book/:bookId';
/// final pathParameters = pathParametersFromPattern(pattern); // {'id', 'bookId'}
/// ```
Set<String> pathParametersFromPattern(String pattern) => <String>{
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern))
match[1]!,
};

/// Reconstructs the full path from a [pattern] and path parameters.
///
/// For example:
///
/// ```dart
/// final pattern = '/family/:id';
/// final path = patternToPath(pattern, {'id': 'family-id'}); // '/family/family-id'
/// ```
String patternToPath(String pattern, Map<String, String> pathParameters) {
final StringBuffer buffer = StringBuffer();
int start = 0;
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern)) {
if (match.start > start) {
buffer.write(pattern.substring(start, match.start));
}
final String name = match[1]!;
buffer.write(pathParameters[name]);
start = match.end;
}

if (start < pattern.length) {
buffer.write(pattern.substring(start));
}
return buffer.toString();
}
34 changes: 13 additions & 21 deletions packages/go_router_builder/lib/src/route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:path_to_regexp/path_to_regexp.dart';
import 'package:source_gen/source_gen.dart';
import 'package:source_helper/source_helper.dart';

import 'path_utils.dart';
import 'type_helpers.dart';

/// Custom [Iterable] implementation with extra info.
Expand Down Expand Up @@ -79,12 +79,8 @@ class GoRouteConfig extends RouteBaseConfig {
/// The name of the GoRoute to be created by this configuration.
final String? name;

late final Set<String> _pathParams = Set<String>.unmodifiable(_parsedPath
.whereType<ParameterToken>()
.map((ParameterToken e) => e.name));

late final List<Token> _parsedPath =
List<Token>.unmodifiable(parse(_rawJoinedPath));
late final Set<String> _pathParams =
pathParametersFromPattern(_rawJoinedPath);

String get _rawJoinedPath {
final List<String> pathSegments = <String>[];
Expand All @@ -103,22 +99,18 @@ class GoRouteConfig extends RouteBaseConfig {
// construct path bits using parent bits
// if there are any queryParam objects, add in the `queryParam` bits
String get _locationArgs {
final Iterable<String> pathItems = _parsedPath.map((Token e) {
if (e is ParameterToken) {
final Map<String, String> pathParameters = Map<String, String>.fromEntries(
_pathParams.map((String pathParameter) {
// Enum types are encoded using a map, so we need a nullability check
// here to ensure it matches Uri.encodeComponent nullability
final DartType? type = _field(e.name)?.returnType;
return '\${Uri.encodeComponent(${_encodeFor(e.name)}${type?.isEnum ?? false ? '!' : ''})}';
}
if (e is PathToken) {
return e.value;
}
throw UnsupportedError(
'$likelyIssueMessage '
'Token ($e) of type ${e.runtimeType} is not supported.',
);
});
return "'${pathItems.join()}'";
final DartType? type = _field(pathParameter)?.returnType;
final String value =
'\${Uri.encodeComponent(${_encodeFor(pathParameter)}${type?.isEnum ?? false ? '!' : ''})}';
return MapEntry<String, String>(pathParameter, value);
}),
);
final String location = patternToPath(_rawJoinedPath, pathParameters);
return "'$location'";
}

ParameterElement? get _extraParam => _ctor.parameters
Expand Down
3 changes: 1 addition & 2 deletions 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: 2.2.2
version: 2.2.3
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 All @@ -17,7 +17,6 @@ dependencies:
collection: ^1.14.0
meta: ^1.7.0
path: ^1.8.0
path_to_regexp: ^0.4.0
source_gen: ^1.0.0
source_helper: ^1.3.0

Expand Down
40 changes: 40 additions & 0 deletions packages/go_router_builder/test/path_utils_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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_builder/src/path_utils.dart';
import 'package:test/test.dart';

void main() {
group('pathParametersFromPattern', () {
test('It should return the parameters of the path', () {
expect(pathParametersFromPattern('/'), const <String>{});
expect(pathParametersFromPattern('/user'), const <String>{});
expect(pathParametersFromPattern('/user/:id'), const <String>{'id'});
expect(pathParametersFromPattern('/user/:id/book'), const <String>{'id'});
expect(
pathParametersFromPattern('/user/:id/book/:bookId'),
const <String>{'id', 'bookId'},
);
});
});

group('patternToPath', () {
test('It should replace the path parameters with their values', () {
expect(patternToPath('/', const <String, String>{}), '/');
expect(patternToPath('/user', const <String, String>{}), '/user');
expect(
patternToPath('/user/:id', const <String, String>{'id': 'user-id'}),
'/user/user-id');
expect(
patternToPath(
'/user/:id/book', const <String, String>{'id': 'user-id'}),
'/user/user-id/book');
expect(
patternToPath('/user/:id/book/:bookId',
const <String, String>{'id': 'user-id', 'bookId': 'book-id'}),
'/user/user-id/book/book-id',
);
});
});
}
1 change: 0 additions & 1 deletion script/configs/allowed_unpinned_deps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# cautious about adding to this list.
- build_verify
- google_maps
- path_to_regexp
- win32

## Allowed by default
Expand Down