net/url: implement encoding.BinaryAppender for URL

For #62384
This commit is contained in:
apocelipes 2024-08-08 00:48:38 +08:00
parent 9177e12ccc
commit f188b91978
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")