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

Marshal & Unmarshal functions

olebedev opened this issue · comments

Hi guys!
I would like to implement Marshal & Unmarshal functions like in encoding/json package.
Please, let me know what you think.

what would the signature of the Marshal method that you're proposing look like?

As for unmarshalling, 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 ?

Thanks for the tip! I knew about the library but did not pay enough attention. It seems this is what I need.
My idea is to have both operations in a single library and similar agreement for the tags.

It is very simple, of course. And Marshal function is very simple and may look something like this:

// Marshal returns the encoded string of v.
func Marshal(v interface{}) (string, error) {
    var err error
    var values url.Values
    if values, err = Values(v); err != nil {
        return "", err
    }
    return values.Encode(), nil
}

But main work here need to implement the Unmarshal function with the signature:

func Unmarshal(data string, v interface{}) error

But probably there is no need.

as you show above, it's literally one line of code to go from the returned url.Values to a string, so I don't think adding a new Marshal function provides much value.