net/url: implement encoding.BinaryAppender for URL

For #62384

Change-Id: I61529efe3a59b13606479b74af6cbff61c9efb6e
GitHub-Last-Rev: f188b91978
GitHub-Pull-Request: golang/go#68763
Reviewed-on: https://go-review.googlesource.com/c/go/+/603815
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
apocelipes 2024-08-07 23:35:23 +00:00 committed by Gopher Robot
parent 48a1f6989c
commit db0b6a85c2
4 changed files with 8 additions and 1 deletions

View File

@ -2,3 +2,4 @@ pkg encoding, type BinaryAppender interface { AppendBinary } #62384
pkg encoding, type BinaryAppender interface, AppendBinary([]uint8) ([]uint8, error) #62384
pkg encoding, type TextAppender interface { AppendText } #62384
pkg encoding, type TextAppender interface, AppendText([]uint8) ([]uint8, error) #62384
pkg net/url, method (*URL) AppendBinary([]uint8) ([]uint8, error) #62384

View File

@ -0,0 +1 @@
[URL] now also implements the [encoding.BinaryAppender] interface.

View File

@ -1219,7 +1219,11 @@ func splitHostPort(hostPort string) (host, port string) {
// Would like to implement MarshalText/UnmarshalText but that will change the JSON representation of URLs.
func (u *URL) MarshalBinary() (text []byte, err error) {
return []byte(u.String()), nil
return u.AppendBinary(nil)
}
func (u *URL) AppendBinary(b []byte) ([]byte, error) {
return append(b, u.String()...), nil
}
func (u *URL) UnmarshalBinary(text []byte) error {

View File

@ -1866,6 +1866,7 @@ func TestURLHostnameAndPort(t *testing.T) {
var _ encodingPkg.BinaryMarshaler = (*URL)(nil)
var _ encodingPkg.BinaryUnmarshaler = (*URL)(nil)
var _ encodingPkg.BinaryAppender = (*URL)(nil)
func TestJSON(t *testing.T) {
u, err := Parse("https://www.google.com/x?y=z")