1
0
mirror of https://github.com/golang/go synced 2024-11-22 15:44:53 -07:00

net: make IP implement encoding.MarshalerText, encoding.UnmarshalerText

See golang.org/s/go12encoding for design.

R=golang-dev, bradfitz, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/12705043
This commit is contained in:
Russ Cox 2013-08-14 00:33:20 -04:00
parent 56ce83f7ba
commit 413d4c6c11
2 changed files with 38 additions and 0 deletions

View File

@ -12,6 +12,8 @@
package net package net
import "errors"
// IP address lengths (bytes). // IP address lengths (bytes).
const ( const (
IPv4len = 4 IPv4len = 4
@ -310,6 +312,27 @@ func (ip IP) String() string {
return s return s
} }
// MarshalText implements the encoding.TextMarshaler interface.
// The encoding is the same as returned by String.
func (ip IP) MarshalText() ([]byte, error) {
if len(ip) != IPv4len && len(ip) != IPv6len {
return nil, errors.New("invalid IP address")
}
return []byte(ip.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The IP address is expected in a form accepted by ParseIP.
func (ip *IP) UnmarshalText(text []byte) error {
s := string(text)
x := ParseIP(s)
if x == nil {
return &ParseError{"IP address", s}
}
*ip = x
return nil
}
// Equal returns true if ip and x are the same IP address. // Equal returns true if ip and x are the same IP address.
// An IPv4 address and that same address in IPv6 form are // An IPv4 address and that same address in IPv6 form are
// considered to be equal. // considered to be equal.

View File

@ -32,6 +32,11 @@ func TestParseIP(t *testing.T) {
if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) { if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out) t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
} }
var out IP
if err := out.UnmarshalText([]byte(tt.in)); !reflect.DeepEqual(out, tt.out) || (tt.out == nil) != (err != nil) {
t.Errorf("IP.UnmarshalText(%q) = %v, %v, want %v", tt.in, out, err, tt.out)
}
} }
} }
@ -47,6 +52,7 @@ var ipStringTests = []struct {
{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0}, "2001:db8:0:0:1::"}, {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0}, "2001:db8:0:0:1::"},
{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1:0:0:1"}, {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1:0:0:1"},
{IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, "2001:db8::a:b:c:d"}, {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, "2001:db8::a:b:c:d"},
{IPv4(192, 168, 0, 1), "192.168.0.1"},
{nil, "<nil>"}, {nil, "<nil>"},
} }
@ -55,6 +61,15 @@ func TestIPString(t *testing.T) {
if out := tt.in.String(); out != tt.out { if out := tt.in.String(); out != tt.out {
t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.out) t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.out)
} }
if tt.in != nil {
if out, err := tt.in.MarshalText(); string(out) != tt.out || err != nil {
t.Errorf("IP.MarshalText(%v) = %q, %v, want %q, nil", out, err, tt.out)
}
} else {
if _, err := tt.in.MarshalText(); err == nil {
t.Errorf("IP.MarshalText(nil) succeeded, want failure")
}
}
} }
} }