diff --git a/README.md b/README.md index f254548..42c23ff 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ m2.Equal(m1) ```go // get the multiaddr protocol description objects addr.Protocols() -// []*Protocol{ -// &Protocol{ Code: 4, Name: 'ip4', Size: 32}, -// &Protocol{ Code: 17, Name: 'udp', Size: 16}, +// []Protocol{ +// Protocol{ Code: 4, Name: 'ip4', Size: 32}, +// Protocol{ Code: 17, Name: 'udp', Size: 16}, // } ``` diff --git a/codec.go b/codec.go index 22e65c0..59825f9 100644 --- a/codec.go +++ b/codec.go @@ -25,7 +25,7 @@ func stringToBytes(s string) ([]byte, error) { for len(sp) > 0 { p := ProtocolWithName(sp[0]) - if p == nil { + if p.Code == 0 { return nil, fmt.Errorf("no protocol with name %s", sp[0]) } b = append(b, CodeToVarint(p.Code)...) @@ -62,7 +62,7 @@ func bytesToString(b []byte) (ret string, err error) { code, n := ReadVarintCode(b) b = b[n:] p := ProtocolWithCode(code) - if p == nil { + if p.Code == 0 { return "", fmt.Errorf("no protocol with code %d", code) } s = strings.Join([]string{s, "/", p.Name}, "") @@ -92,7 +92,7 @@ func bytesSplit(b []byte) (ret [][]byte, err error) { for len(b) > 0 { code, n := ReadVarintCode(b) p := ProtocolWithCode(code) - if p == nil { + if p.Code == 0 { return [][]byte{}, fmt.Errorf("no protocol with code %d", b[0]) } @@ -104,7 +104,7 @@ func bytesSplit(b []byte) (ret [][]byte, err error) { return ret, nil } -func addressStringToBytes(p *Protocol, s string) ([]byte, error) { +func addressStringToBytes(p Protocol, s string) ([]byte, error) { switch p.Code { case P_IP4: // ipv4 @@ -138,7 +138,7 @@ func addressStringToBytes(p *Protocol, s string) ([]byte, error) { return []byte{}, fmt.Errorf("failed to parse %s addr: unknown", p.Name) } -func addressBytesToString(p *Protocol, b []byte) string { +func addressBytesToString(p Protocol, b []byte) string { switch p.Code { // ipv4,6 diff --git a/interface.go b/interface.go index 6f57625..512cb62 100644 --- a/interface.go +++ b/interface.go @@ -26,7 +26,7 @@ type Multiaddr interface { // Protocols returns the list of Protocols this Multiaddr includes // will panic if protocol code incorrect (and bytes accessed incorrectly) - Protocols() []*Protocol + Protocols() []Protocol // Encapsulate wraps this Multiaddr around another. For example: // diff --git a/multiaddr.go b/multiaddr.go index 3071fa9..373c2c1 100644 --- a/multiaddr.go +++ b/multiaddr.go @@ -54,7 +54,7 @@ func (m *multiaddr) String() string { // Protocols returns the list of protocols this Multiaddr has. // will panic in case we access bytes incorrectly. -func (m *multiaddr) Protocols() []*Protocol { +func (m *multiaddr) Protocols() []Protocol { // panic handler, in case we try accessing bytes incorrectly. defer func() { @@ -64,12 +64,12 @@ func (m *multiaddr) Protocols() []*Protocol { } }() - ps := []*Protocol{} + ps := []Protocol{} b := m.bytes[:] for len(b) > 0 { code, n := ReadVarintCode(b) p := ProtocolWithCode(code) - if p == nil { + if p.Code == 0 { // this is a panic (and not returning err) because this should've been // caught on constructing the Multiaddr panic(fmt.Errorf("no protocol with code %d", b[0])) diff --git a/multiaddr_test.go b/multiaddr_test.go index 2c848a2..f9f0a47 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -204,12 +204,12 @@ func TestProtocols(t *testing.T) { } ps := m.Protocols() - if ps[0] != ProtocolWithName("ip4") { + if ps[0].Code != ProtocolWithName("ip4").Code { t.Error(ps[0], ProtocolWithName("ip4")) t.Error("failed to get ip4 protocol") } - if ps[1] != ProtocolWithName("udp") { + if ps[1].Code != ProtocolWithName("udp").Code { t.Error(ps[1], ProtocolWithName("udp")) t.Error("failed to get udp protocol") } diff --git a/protocols.go b/protocols.go index 49051be..3be0b30 100644 --- a/protocols.go +++ b/protocols.go @@ -28,38 +28,38 @@ const ( ) // Protocols is the list of multiaddr protocols supported by this module. -var Protocols = []*Protocol{ - &Protocol{P_IP4, 32, "ip4", CodeToVarint(P_IP4)}, - &Protocol{P_TCP, 16, "tcp", CodeToVarint(P_TCP)}, - &Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP)}, - &Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP)}, - &Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)}, +var Protocols = []Protocol{ + Protocol{P_IP4, 32, "ip4", CodeToVarint(P_IP4)}, + Protocol{P_TCP, 16, "tcp", CodeToVarint(P_TCP)}, + Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP)}, + Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP)}, + Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)}, // these require varint: - &Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)}, - &Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)}, - &Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)}, + Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)}, + Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)}, + Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)}, // {480, 0, "http"}, // {443, 0, "https"}, } // ProtocolWithName returns the Protocol description with given string name. -func ProtocolWithName(s string) *Protocol { +func ProtocolWithName(s string) Protocol { for _, p := range Protocols { if p.Name == s { return p } } - return nil + return Protocol{} } // ProtocolWithCode returns the Protocol description with given protocol code. -func ProtocolWithCode(c int) *Protocol { +func ProtocolWithCode(c int) Protocol { for _, p := range Protocols { if p.Code == c { return p } } - return nil + return Protocol{} } // CodeToVarint converts an integer to a varint-encoded []byte