Multiaddr.String no error

The error in String should not actually ocurr, as the multiaddr
should have been valid to be constructed successfully, and thus
should be encoded back to its string rep correctly.

This will be bolstered by creating an interface (to prevent messing
with the internal bytes)
This commit is contained in:
Juan Batiz-Benet 2014-10-06 02:38:56 -07:00
parent cca60aba88
commit bd30912d08
3 changed files with 13 additions and 21 deletions

View File

@ -26,8 +26,12 @@ func (m *Multiaddr) Equal(m2 *Multiaddr) bool {
}
// String returns the string representation of a Multiaddr
func (m *Multiaddr) String() (string, error) {
return bytesToString(m.Bytes)
func (m *Multiaddr) String() string {
s, err := bytesToString(m.Bytes)
if err != nil {
panic("multiaddr failed to convert back to string. corrupted?")
}
return s
}
// Protocols returns the list of protocols this Multiaddr has.
@ -63,16 +67,8 @@ func (m *Multiaddr) Encapsulate(o *Multiaddr) *Multiaddr {
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) {
s1, err := m.String()
if err != nil {
return nil, err
}
s2, err := o.String()
if err != nil {
return nil, err
}
s1 := m.String()
s2 := o.String()
i := strings.LastIndex(s1, s2)
if i < 0 {
return nil, fmt.Errorf("%s not contained in %s", s2, s1)
@ -86,11 +82,7 @@ func (m *Multiaddr) DialArgs() (string, string, error) {
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
}
str, err := m.String()
if err != nil {
return "", "", err
}
str := m.String()
parts := strings.Split(str, "/")[1:]
network := parts[2]

View File

@ -117,7 +117,7 @@ func TestEncapsulate(t *testing.T) {
}
b := m.Encapsulate(m2)
if s, _ := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
if s := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
t.Error("encapsulate /ip4/127.0.0.1/udp/1234/udp/5678 failed.", s)
}
@ -127,7 +127,7 @@ func TestEncapsulate(t *testing.T) {
t.Error("decapsulate /udp failed.", err)
}
if s, _ := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
if s := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
t.Error("decapsulate /udp failed.", "/ip4/127.0.0.1/udp/1234", s)
}
@ -137,7 +137,7 @@ func TestEncapsulate(t *testing.T) {
t.Error("decapsulate /ip4 failed.", err)
}
if s, _ := d.String(); s != "" {
if s := d.String(); s != "" {
t.Error("decapsulate /ip4 failed.", "/", s)
}
}

View File

@ -13,7 +13,7 @@ func testConvert(t *testing.T, s string, gen GenFunc) {
t.Fatal("failed to generate.")
}
if s2, _ := m.String(); err != nil || s2 != s {
if s2 := m.String(); err != nil || s2 != s {
t.Fatal("failed to convert: " + s + " != " + s2)
}
}