Parsing query strings into a map
betabandido opened this issue · comments
Parsing query strings into a struct is sufficient and works well in many situations. But, in situations where the input data contains dynamic field names it is not possible to match the input into a struct.
For instance, consider the following example where a set of filters are passed:
/test?filter[size]=large&filter[size]=small&filter[color]=red&filter[color]=blue
If the endpoint is always supposed to filter based on size
and color
it is then possible to decode the query strings into a URL. But, if size
and color
are just examples (and many other values can be used instead) decoding into a struct will not work.
In this case, using a map[string][]string
would work. But, there could be more generic cases (e.g., nested deep objects). See this discussion in the OpenAPI spec repo for some examples. (see also https://github.com/ljharb/qs for an implementation in nodejs)
Would you consider adding support for parsing query strings into a map? What about an even more generic solution like the ones being proposed in the OpenAPI spec repo?
I think /test?filter[size]=large&filter[size]=small&filter[color]=red&filter[color]=blue
is not a correct query string referred to map[string][]string
, the correct string is /test?filter[0][size]=large&filter[1][size]=small&filter[0][color]=red&filter[1][color]=blue
.
I had implemented an url query string parser, but it can not parse map[string][]string.
For map structure, it has so limit on reflect
, it works well in map[Basic Structure]Basic Strucure.
And I have a suggestion, you could use a struct to implement it.
type Query struct {
Filter []string `query:"filter"`
}
My Query string encoder and parser
https://github.com/hetiansu5/urlquery
I'm not planning to add support for parsing, but have added links to several projects that do in the README: https://github.com/google/go-querystring#alternatives