From bd30912d08f50d5374c68334d29da8140995f702 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Mon, 6 Oct 2014 02:38:56 -0700 Subject: [PATCH] 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) --- index.go | 26 +++++++++----------------- multiaddr_test.go | 6 +++--- net_test.go | 2 +- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/index.go b/index.go index 903d65d..65ff750 100644 --- a/index.go +++ b/index.go @@ -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] diff --git a/multiaddr_test.go b/multiaddr_test.go index 7bc2e92..5de5aaa 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -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) } } diff --git a/net_test.go b/net_test.go index fd1ede1..f734b28 100644 --- a/net_test.go +++ b/net_test.go @@ -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) } }