google / go-querystring

go-querystring is Go library for encoding structs into URL query strings.

Home Page:https://pkg.go.dev/github.com/google/go-querystring/query

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsing?

maliqq opened this issue · comments

Golang's standard net/url parse builds flat query params:

?a[]=1&a[]=2&b[c]=d

results:

map[string][]string{
"a[]": []string{"1", "2"},
"b[c]": []string{"d"}
}

what is needed:

map[string]interface{}{
"a": []string{"1", "2"},
"b": map[string]string{"c": "d"}
}

I think @ernesto-jimenez's work in #5 should address your use case. I just need to get around to getting that merged in.

@maliqq are you having problems parsing or encoding query parameters in that format?

This package only handles encoding query parameters.

PR #5 is to encode nested structs with the format you comment, but it doesn't cover parsing the query parameters in that format.

@ernesto-jimenez ok, I see, I'm having problems with parsing nested query params.

It would be nice to have options to encode/decode in both ways (query string -> structs, structs->query string). Do you have any plans on it?

@maliqq it's not my package, I just contributed a PR :)

@willnorris can probably answer that.

oh sorry, I misunderstood what you were after. I don't have immediate plans to support parsing, primarily because http://www.gorillatoolkit.org/pkg/schema does a pretty good job of that. I don't know if it supports your exact case.

As for unmarshaling, that's outside the initial scope of this library. It could of course be added, but at that point, why not just use http://www.gorillatoolkit.org/pkg/schema ?

I would like unmarshaling/parsing too. I don't think I can use gorilla/schema because:

Package gorilla/schema fills a struct with form values.

But I want to work with URL query parameters for GET requests.

My need for this right now is weak, I just have a // TODO: Automate this conversion process. comment. If my need becomes stronger, I will work on implementing this, unless I'm missing something and parsing isn't neccessary.

commented

@shurcooL - you should be able to use gorilla/schema with query params

This works for me:

import (
    "fmt"
    "net/http"
    "github.com/gorilla/schema"
)

type Person struct {
    Name  string `schema:"name"`  // custom name
    Admin bool   `schema:"-"`     // this field is never set
}

func MyHandler(w http.ResponseWriter, r *http.Request) {
    var person Person
    decoder := schema.NewDecoder()
    if err := decoder.Decode(&person, r.URL.Query()); err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(person)
}

Thanks for the heads up @abramovic, I will give that a shot.

Following up here. I gave the above a shot, and it seems to work well, see shurcooL/home#5.

The current description of the github.com/gorilla/schema package says:

Package gorilla/schema fills a struct with form values.

Which is why I didn't expect I could use it to parse queries. I think should be updated to mention that it works well on parsing query parameters as well as form values.

However, it turns out that package does not currently have support for the opposite direction, for encoding a struct, which is issue gorilla/schema#44. (There is a PR gorilla/schema#46 to implement that, which is close but not yet merged.)

So the current situation (edit: out of date by now, in 2018) is best described via the following table:

gorilla/schema google/go-querystring/query
Decode Form to Struct
Decode Query to Struct
Encode Struct to Form
Encode Struct to Query

As an observation, to me, it'd be valuable to have a library that can go in both directions officially, because of the implicit guarantee that it should be a 1:1 mapping without surprises when doing both encoding/decoding.

I'd be happy to just have go-querystring (or some variation of it) merged into gorilla/schema if they're open to it.

That sounds like a good idea to me. It would be nice to have one canonical library for this type of thing.

an URL Query string Encoder and Parser. It works well.

https://github.com/hetiansu5/urlquery

Closing this, since this library is basically done and I'm really only doing basic maintenance. I did add an alternatives section to the README, however, with links to other libraries that do this: https://github.com/google/go-querystring#alternatives