Add support for multimaps in query strings #2139
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, Gin supports parsing maps in query strings using
GetQueryMap
. The current implementation, however, is limited to simple maps where values are strings. A simple change to the code enables parsing more complex maps where values are string arrays. For instance, the following URL contains such a map:/test?map[key1]=value11&map[key1]=value12&map[key2]=value2
We have use-cases where we need to parse such URLs. Given the change is relatively minor, and it is backwards compatible, it would be nice to have this feature added.
I chose
multimap
to refer to a map containing string arrays (see https://en.wikipedia.org/wiki/Multimap). But, there might be a better name.I will document the new methods in the README if you think it is a feature you want to add to Gin.
I decided not to duplicate the
get
method to reduce code duplication. The tradeoff is a slight performance reduction. I did some performance evaluation and the results were:Original GetQueryMap: 216 ns / call
Modified GetQueryMap: 419 ns / call
I believe this is a negligible increase in performance, but you should have a better idea on the latency breakdown across the whole lifespan of a process request. So, let me know if you think it is better not to pay the extra latency.