-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add URI::Params#merge
, #merge!
and URI#update_query_params
#13415
Add URI::Params#merge
, #merge!
and URI#update_query_params
#13415
Conversation
This commit adds 2 methods for destructive and non-destructive merging of params. Specs are included.
This commit adds a `query_params` method on URI that yields a URI::Params of the query and commits any modifications to it.
Just a note about the implementation: I think it would be cleaner and more efficient to iterate |
Great call, I wasn't aware of block form of
Nevermind. It's been a bit since I looked at this PR, this doesn't make sense |
This refactor simplifies URI::Params#merge! by using Hash#merge! instead of #reduce
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.
There's a bug in merge!
where the array value needs to be duped.
I also made a couple of suggestions for improving the API docs.
src/uri/params.cr
Outdated
else | ||
add(key, value) | ||
if replace | ||
@raw_params.merge!(params.raw_params) |
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.
@raw_params.merge!(params.raw_params) | |
@raw_params.merge!(params.raw_params) { |_, first, second| second.dup } |
The array needs to be duped to prevent mutations on either self
or params
to reflect on the other.
a = URI::Params.parse("foo=bar")
b = URI::Params.parse("foo=baz")
b.merge!(a)
b.add("foo", "qux")
a.add("foo", "qax")
a # => URI::Params{"foo" => ["bar", "qux", "qax]}
b # => URI::Params{"foo" => ["bar", "qux", "qax]}
Could you add a spec for this please?
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 Hash#merge!
is causing state to leak, would the solve using reduce be more explicit?
I'll add these changes + spec, but I personally find it confusing to follow nested #dup
s
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.
Using Hash#merge!
is fine and safe. The only reason this dup
is necessary is that the Array
representation is an implementation detail of URI::Params
and not exposed.
* Updates docs * Fixes leaky state bug when merging params * Adds tests
Co-authored-by: Quinton Miller <nicetas.c@gmail.com>
URI#query_params
URI::Params#merge
, #merge!
and URI#update_query_params
This PR adds 3 new methods in the URI namespace.
2 utility methods on
URI::Params
for merging otherURI::Params
, destructively and non-destructively.merge(other : URI::Params, *, replace : Bool) : URI::Params
merge!(other : URI::Params, *, replace : Bool) : URI::Params
It also adds a method on
URI
to mutate query_params on the URI object in place.update_query_params(&) : URI::Params
Happy to speak more to it, modify, or close if needed.
closes #13399