-
Notifications
You must be signed in to change notification settings - Fork 44
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
Feature/query parameter router matching #140
Feature/query parameter router matching #140
Conversation
FIX route query param handling
SwiftLint found issuesWarnings
Generated by 🚫 danger |
Ty Max! considering query params only when they are there might make sense but (didn't check yet the implementation) the order of query params is usually not guaranteed so if you have: router.get(URL, {...}).when { (params, request) in
//check params
return true
} That way the server can easily check if the URL matches and the condition is true |
you might be right, i thought it's just easier to add it to the route directly. however, i just realized that adding wildcard functionality is superflous, as all query parameters are added to the request anyway ... |
Let's also wait for @joanromano opinion and @zzarcon but I think the order shouldn't matter otherwise is quite error prone. Let's decide how to tackle it before |
} | ||
|
||
var hashValue: Int { | ||
return path.hashValue ^ method.hashValue | ||
return queryParameters.reduce(path.hashValue ^ method.hashValue) { $0.0 ^ $0.1.hashValue} << 8 |
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.
If now the hash is affected by query parameters it means that if there is an extra parameter it won't match, right?
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.
Not sure what you mean.. matching is done in matchRoute(...)
here it means en extra query param will result in a different hash, so a separate Route. /user?param=1
will not replace /user
, but be separate. (Where we run into prioritization issues ... which route to execute when calling www.myapi.com/user?param=1
?
Current coverage is 98.87% (diff: 94.66%)@@ master #140 diff @@
==========================================
Files 10 10
Lines 481 535 +54
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 479 529 +50
- Misses 2 6 +4
Partials 0 0
|
How about this? Forget about the syntax errors :) router.get(URL) { request in
if (request.components["relevantQueryParam"]) {
return fakeResponse;
} else {
return request.continue();
}
} The point is to have a request.continue method, which is nothing but giving the user the ability to continue the request whenever he wants AKA the method implementation is just making the same request since we have all the info we need there. I think this is quite powerful and doesn't require any kind of boilerplate + keeps the routing logic in the same place. Thoughts? @Landsi @MP0w @joanromano |
@zzarcon this means that the Probably I am wrong, but I am afraid that's not possible. |
@joanromano exactly! that's what my suggestion is about, but yeah I have no idea if that is even possible :( So, if I understood properly, the problem is that we don't all the info we need available in the Router, like the handlers for instance right? If that's the case my suggestion will not be possible as you said 😭 |
Hey guys with the holidays in the middle we forgot this. |
As another option I would prefer the more generic approach: router.get(url) {
return ...
}.when { (request) in
....
} which requires some changes (syntax TBD) but is more generic and could be used easily with some currying like stuff: // your convenience func that support partial apply
func hasParams(_ params: String...) -> (Request) -> Bool {
return { (request) in
return params.contains(request)
}
}
....
// the proposed addition
func when(_ condition: (Request) -> Bool) {
}
// usage
router.get(url) {
return ...
}.when(hasParams("foo", "bar")) IMHO it's quite clean, opinions? @devlucky/kakapo-core @Landsi |
@joanromano Hector proposal is actually really easy to implement, we should probably do it:
|
@MP0w I was actually wrong when I mentioned the success/error handling scenarios that are handled by the client, true. |
As we discussed f2f (weeks ago) appears we don't want to add this because:
Generic filteringFor more generic filtering (just closed 2 WIP PRs one from me and @joanromano ) Provide ways to perform original requestThis was the proposed approach from hez which is doable but not sure if we should add it to the stdlib If we feel like we should do something and/or use one of the 3 approach, or someone else needs something similar we can of course consider doing something but for the moment I will just close. |
I recently stumbled upon a Service API, where the routing was done exclusively with the query parameters.
While that is not that pretty in my opinion, I still needed to mock it for my unit tests.
This PR includes Router matching of query parameters including wildcard values.
Basically, this route registration is now possible: