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

[draft] [go_router] wrap path_utils content in RoutePattern #7706

Closed
wants to merge 2 commits into from

Conversation

cedvdb
Copy link
Contributor

@cedvdb cedvdb commented Sep 25, 2024

part of flutter/flutter#155567

This is a draft PR to see if some part of go_router can be refactored to be more readable.
Currently it is hard to know if a parameter / variable is a pattern such as /books/:bookId or a path such as /book/123 when reading the code of a given function. This PR is a stab on improving the readability on that front.

It wraps the content of path_utils in a RoutePattern class.

@chunhtai this does not compile but I'm interested in knowing whether this is a direction the team wants to go or not, I'll just close this if not.

I'll probably wait for my other PR's to go through before going any further anyway.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

assert(pathParameters.containsKey(paramName),
'missing param "$paramName" for $path');
'missing param "$paramName" for ${route.pattern}');
Copy link
Contributor Author

@cedvdb cedvdb Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

( RoutePattern overrides toString )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may be a breaking change, this could be an easy migration if you write rules fix_data.yaml

Also in this library, path means /book/:bookId, the filled string like book/123 is called location.

@cedvdb cedvdb changed the title [draft][go_router] wrap path_utils content in RoutePattern [draft] [go_router] wrap path_utils content in RoutePattern Sep 25, 2024
///
/// Builds the absolute path for the route, by concatenating the paths of the
/// route and all its ancestors.
String? locationForRoute(RouteBase route) =>
Copy link
Contributor Author

@cedvdb cedvdb Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll readd this method later as it is a Public API, but for the time being, the signature has changed for the internals.

It doesn't seem like this should be part of the Public API though ?


/// The path parameters in this route.
@internal
final List<String> pathParameters = <String>[];
List<String> get pathParameters => pattern.parameters;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the pattern should be accessed directly (route.pattern.parameters) or if the route should facade to the pattern.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we expose the pattern, than they should probably be accessed directly to avoid duplication

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to expose pattern though?

Copy link
Contributor Author

@cedvdb cedvdb Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently no, the idea was to have RoutePattern used internally, (I have to make it private or internal in the future) and have the public representation still String path, ie: GoRoute(path: '/some-path'). The RoutePattern is supposed to be used internally everywhere so we don't end up with a String path, where we are unsure whether path is /books/:bookId or /books/123 without looking at the wider context. Although I'm not a fan of this kind of constructor conversion, in this case I think it's worth it.

However, the method toPath should be public GoRoute().toPath because it's useful when building a redirect link for a third party and it's currently not exposed I think (from path_utils). This brings ambiguosity though, because there is already GoRoute('').path. I may have to rename RoutePattern into RoutePath, and toPath() into reverse(), it may be less ambiguous that way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that case the RoutePattern should be kept in as private variable in GoRoute class.

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapping path_utils does make it more readable. but not sure about changing the naming convention is worthing the breaking change.

assert(pathParameters.containsKey(paramName),
'missing param "$paramName" for $path');
'missing param "$paramName" for ${route.pattern}');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may be a breaking change, this could be an easy migration if you write rules fix_data.yaml

Also in this library, path means /book/:bookId, the filled string like book/123 is called location.


/// The path parameters in this route.
@internal
final List<String> pathParameters = <String>[];
List<String> get pathParameters => pattern.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we expose the pattern, than they should probably be accessed directly to avoid duplication


/// The path parameters in this route.
@internal
final List<String> pathParameters = <String>[];
List<String> get pathParameters => pattern.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to expose pattern though?

@cedvdb
Copy link
Contributor Author

cedvdb commented Sep 30, 2024

wrapping path_utils does make it more readable. but not sure about changing the naming convention is worthing the breaking change.

I believe it's possible to make it only internal without breaking changes, that's the requirement I'm doing this under

@chunhtai
Copy link
Contributor

if we want to rename

path -> pattern
location -> path

I would like us to change the public facing API such as
GoRoute.path -> GoRoute.pattern etc.

but i am not sure if this worth it.

As for wrapping path_util, I think we can do it as less as breaking change as possible, and I am onboard with this.

@cedvdb
Copy link
Contributor Author

cedvdb commented Sep 30, 2024

@chunhtai So the convention used is that path is a /books/:bookId and location is /books/123 ? I had a feeling that was the case but wasn't sure. I don't think renaming is necessary.

@chunhtai
Copy link
Contributor

chunhtai commented Sep 30, 2024

@chunhtai So the convention used is that path is a /books/:bookId and location is /books/123 ? I had a feeling that was the case but wasn't sure. I don't think renaming is necessary.

yes. that is the current naming convention.

@chunhtai
Copy link
Contributor

(triage) Hi @cedvdb are you still planning on coming back to this pr?

@cedvdb
Copy link
Contributor Author

cedvdb commented Nov 21, 2024

@chunhtai This is not my priority, so I don't think I will in the near future at least. So you can close, I'll reopen if I happen to come back to this

@chunhtai
Copy link
Contributor

Thanks for reply, I will close this pr for now, feel free to reopen if you have time in the future

@chunhtai chunhtai closed this Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants