-
Notifications
You must be signed in to change notification settings - Fork 124
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
Remove Rspotify default parameters and add parameter macros #202
Conversation
Great job! I totally agree to your point, the default parameter in this crate is a little messy. Good to see the improvement. Since the parameters in endpoints are mostly passed by value, then we are working on the function signature, shall we update the parameters to pass them by reference? |
I don't really know. For now I've only set those parameters that I needed by reference, and left the rest by value. I guess it would make sense, yeah. This PR is now ready for review when you have time, by the way. It ended up much bigger than I thought it would haha. Let me know if you have any doubts/suggestions regarding the macros. |
I think it will make the endpoint inconsistency, some parameters passed by reference, and the others by value. I am not sure if it's a chance to modify them all.
Yes, I will. |
I'd say go for it, it makes sense to me. |
Something that's inconsistent about the if let Some(ref name) = name {
params.insert("name", name);
} But if this were a params.insert("name", name); The effect of this is that you can pass a A way to make this consistent would be that required parameters were also taken as a reference: params.insert("name", &name); But this won't work for |
Or another idea for the macro syntax, which solves the previous issue. What if it worked like this instead, imitating pattern matching? let params = build_map! {
name => name, // this is required
Some(ref limit) => limit, // this is optional, and taken as a reference
Some(limit) => limit, // this is optional, and taken as value
Some(ref market) => market.as_ref() // you can still add a specific value
}; We don't need the And the last option is: let params = build_map! {
required "name": name, // will always be inserted
"name": name, // we could remove the `required` keyword
optional "limit": limit,
optional "market": market.map(|m| m.as_ref()), // this is like the `=>` expressions
}; Which would expand to: let mut map = HashMap::with_capacity(3);
map.insert("name", name);
if let Some(val) = limit {
map.insert("limit", val);
}
if let Some(val) = market.map(|m| m.as_ref()) {
map.insert("market", val);
} This is the most intuitive because it uses literals as the keys. The downside is that it requires more code for optional members. |
I think that the best macro syntax is the last one I mentioned, because I dislike magic macros that make you look their documentation in order to understand them. And in this case it's pretty much self-descriptive because it doesn't really hide anything, unlike the other two macros. What do you think? Any other macro you can come up with? Maybe I'm overthinking it? Sorry for thinking about other syntaxes after imlementing the macro, I didn't really notice the downsides of the current one until it was fully implemented. |
I personally prefer the last one with same reason as you do. The other macro syntaxes are a little subtle and hide something into a black box. If you want to know about how it works, you have dig deep into it. let params = build_map! {
required "name": name, // will always be inserted
"name": name, // we could remove the `required` keyword
optional "limit": limit,
optional "market": market.map(|m| m.as_ref()), // this is like the `=>` expressions
}; And I think we could remove the |
So, do you think the endpoints look better like this? In that case I think this is ready for a merge. |
Merged
Yep, both macros and endpoints look better than before :) |
Description
.unwrap_or
Option<T>
instead ofInto<Opton<T>>
now. If you don't like this please let me know, but I didn't get any more feedback so I went for the simpler option.Motivation and Context
Dependencies
None
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce.
Please also list any relevant details for your test configuration