Skip to content
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

net/url: add a "SetQuery" helper function #24552

Closed
HaraldNordgren opened this issue Mar 27, 2018 · 10 comments
Closed

net/url: add a "SetQuery" helper function #24552

HaraldNordgren opened this issue Mar 27, 2018 · 10 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@HaraldNordgren
Copy link
Member

HaraldNordgren commented Mar 27, 2018

Hi, I want to create a new helper function called "SetQueryParameters" in net/url. Is this something the community wants too?

Already from my first experience with Go, I was confused about the round-about way that we set query parameters:

v := u.Query()
v.Set("a", 1)
v.Set("b", 2)
u.RawQuery = v.Encode()

So instead we could have a helper function

u.SetQueryParameters(url.Values{
	"a": {"1"},
	"b": {"2"},
})

to hopefully make life a tiny bit easier for me and other developers.

@ALTree ALTree changed the title Creating helper function "SetQueryParameters" net/url: add a SetQueryParameters helper function Mar 27, 2018
@ALTree ALTree added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Mar 27, 2018
@HaraldNordgren
Copy link
Member Author

Possible implementation: #24528

@mvdan
Copy link
Member

mvdan commented Mar 27, 2018

It seems to me like your second piece of code could be:

u.RawQuery = url.Values{
	"a": {"1"},
	"b": {"2"},
}.Encode()

What are you trying to solve here? If it's less code, I think both ways to do it currently are fine. If it's about the code being confusing, then perhaps better documentation or an example could help. Or perhaps even a proposal to improve a future version of net/url, such as replacing RawQuery with something else.

@HaraldNordgren
Copy link
Member Author

HaraldNordgren commented Mar 27, 2018

@mvdan I think the problem with using RawQuery and Encode() is the amount of voodoo involved.

It feels strange to me that the normal way to construct a URL with query parameters is to assign anything called Raw…. I would expect the internals to handle the "raw" stuff.

So I was trying to take something that confused and bewildered me about Go when I started writing it professionally a year ago (and still looks odd to me), and then simplify it for the next guy.

@ianlancetaylor
Copy link
Contributor

CC @bradfitz @tombergan

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Mar 28, 2018
@bradfitz
Copy link
Contributor

bradfitz commented Apr 3, 2018

There are no existing Setter functions on net/url.(*URL), so I'm somewhat reluctant to start.

But if we added one, I'd name it SetQuery, to match the naming of https://golang.org/pkg/net/url/#URL.Query

And the implementation could be simpler. I replied on https://go-review.googlesource.com/c/go/+/102476

/cc @bcmills

@bcmills
Copy link
Contributor

bcmills commented Apr 3, 2018

I don't have a strong opinion on whether we should add a SetQuery method, but there is a strange asymmetry between RawQuery and RawPath. If one assigns to RawPath without Path, it appears that the EscapedPath method will ignore it. That seems to support @HaraldNordgren's unease at setting the RawQuery field, even though it is safe to do.

That suggests a slightly broader change to bring Query and Path closer to parallel: two setters instead of one.

func (u *URL) SetEscapedPath(rawPath string) error {
	path, err := PathUnescape(path)
	if err != nil {
		return err
	}
	u.RawPath = rawPath
	u.Path = path
	return nil
}

func (u *URL) SetQuery(Values) {
	[…]
}

@HaraldNordgren HaraldNordgren changed the title net/url: add a SetQueryParameters helper function net/url: add a "SetQuery" helper function Apr 4, 2018
@HaraldNordgren
Copy link
Member Author

@bcmills Should there also be a path parameter to the SetEscapedPath function? I'm not exactly sure I follow the idea, but it seems like the underlying problem remains if you have to pass anything "raw" to the function to make it work.

@bcmills
Copy link
Contributor

bcmills commented Apr 5, 2018

@HaraldNordgren
There is no need for a path parameter, because there is only one valid Path for a given RawPath. (The relationship is 1∶N, not M∶N.)

Since the Path field overrides RawPath, if the user has an already-unescaped path they can simply set the Path field — no need for a method call.

@rsc
Copy link
Contributor

rsc commented Apr 9, 2018

Please, let's not start designing new API for url.URL. The URL struct is not set up for incremental modification. It's meant for reading URLs, not to be a kitchen sink for writing them. There's a reason that APIs that consume URLs typically take type string.

@bradfitz
Copy link
Contributor

bradfitz commented Apr 9, 2018

Yeah, I'd prefer we keep URL as it is, without any setters.

We can rethink the URL type for Go 2, perhaps.

@bradfitz bradfitz closed this as completed Apr 9, 2018
@golang golang locked and limited conversation to collaborators Apr 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

8 participants