Commit 17f4666d authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

Protocols now value

parent ed277d56
...@@ -28,9 +28,9 @@ m2.Equal(m1) ...@@ -28,9 +28,9 @@ m2.Equal(m1)
```go ```go
// get the multiaddr protocol description objects // get the multiaddr protocol description objects
addr.Protocols() addr.Protocols()
// []*Protocol{ // []Protocol{
// &Protocol{ Code: 4, Name: 'ip4', Size: 32}, // Protocol{ Code: 4, Name: 'ip4', Size: 32},
// &Protocol{ Code: 17, Name: 'udp', Size: 16}, // Protocol{ Code: 17, Name: 'udp', Size: 16},
// } // }
``` ```
......
...@@ -25,7 +25,7 @@ func stringToBytes(s string) ([]byte, error) { ...@@ -25,7 +25,7 @@ func stringToBytes(s string) ([]byte, error) {
for len(sp) > 0 { for len(sp) > 0 {
p := ProtocolWithName(sp[0]) p := ProtocolWithName(sp[0])
if p == nil { if p.Code == 0 {
return nil, fmt.Errorf("no protocol with name %s", sp[0]) return nil, fmt.Errorf("no protocol with name %s", sp[0])
} }
b = append(b, CodeToVarint(p.Code)...) b = append(b, CodeToVarint(p.Code)...)
...@@ -62,7 +62,7 @@ func bytesToString(b []byte) (ret string, err error) { ...@@ -62,7 +62,7 @@ func bytesToString(b []byte) (ret string, err error) {
code, n := ReadVarintCode(b) code, n := ReadVarintCode(b)
b = b[n:] b = b[n:]
p := ProtocolWithCode(code) p := ProtocolWithCode(code)
if p == nil { if p.Code == 0 {
return "", fmt.Errorf("no protocol with code %d", code) return "", fmt.Errorf("no protocol with code %d", code)
} }
s = strings.Join([]string{s, "/", p.Name}, "") s = strings.Join([]string{s, "/", p.Name}, "")
...@@ -92,7 +92,7 @@ func bytesSplit(b []byte) (ret [][]byte, err error) { ...@@ -92,7 +92,7 @@ func bytesSplit(b []byte) (ret [][]byte, err error) {
for len(b) > 0 { for len(b) > 0 {
code, n := ReadVarintCode(b) code, n := ReadVarintCode(b)
p := ProtocolWithCode(code) p := ProtocolWithCode(code)
if p == nil { if p.Code == 0 {
return [][]byte{}, fmt.Errorf("no protocol with code %d", b[0]) return [][]byte{}, fmt.Errorf("no protocol with code %d", b[0])
} }
...@@ -104,7 +104,7 @@ func bytesSplit(b []byte) (ret [][]byte, err error) { ...@@ -104,7 +104,7 @@ func bytesSplit(b []byte) (ret [][]byte, err error) {
return ret, nil return ret, nil
} }
func addressStringToBytes(p *Protocol, s string) ([]byte, error) { func addressStringToBytes(p Protocol, s string) ([]byte, error) {
switch p.Code { switch p.Code {
case P_IP4: // ipv4 case P_IP4: // ipv4
...@@ -138,7 +138,7 @@ func addressStringToBytes(p *Protocol, s string) ([]byte, error) { ...@@ -138,7 +138,7 @@ func addressStringToBytes(p *Protocol, s string) ([]byte, error) {
return []byte{}, fmt.Errorf("failed to parse %s addr: unknown", p.Name) 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 { switch p.Code {
// ipv4,6 // ipv4,6
......
...@@ -26,7 +26,7 @@ type Multiaddr interface { ...@@ -26,7 +26,7 @@ type Multiaddr interface {
// Protocols returns the list of Protocols this Multiaddr includes // Protocols returns the list of Protocols this Multiaddr includes
// will panic if protocol code incorrect (and bytes accessed incorrectly) // will panic if protocol code incorrect (and bytes accessed incorrectly)
Protocols() []*Protocol Protocols() []Protocol
// Encapsulate wraps this Multiaddr around another. For example: // Encapsulate wraps this Multiaddr around another. For example:
// //
......
...@@ -54,7 +54,7 @@ func (m *multiaddr) String() string { ...@@ -54,7 +54,7 @@ func (m *multiaddr) String() string {
// Protocols returns the list of protocols this Multiaddr has. // Protocols returns the list of protocols this Multiaddr has.
// will panic in case we access bytes incorrectly. // 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. // panic handler, in case we try accessing bytes incorrectly.
defer func() { defer func() {
...@@ -64,12 +64,12 @@ func (m *multiaddr) Protocols() []*Protocol { ...@@ -64,12 +64,12 @@ func (m *multiaddr) Protocols() []*Protocol {
} }
}() }()
ps := []*Protocol{} ps := []Protocol{}
b := m.bytes[:] b := m.bytes[:]
for len(b) > 0 { for len(b) > 0 {
code, n := ReadVarintCode(b) code, n := ReadVarintCode(b)
p := ProtocolWithCode(code) p := ProtocolWithCode(code)
if p == nil { if p.Code == 0 {
// this is a panic (and not returning err) because this should've been // this is a panic (and not returning err) because this should've been
// caught on constructing the Multiaddr // caught on constructing the Multiaddr
panic(fmt.Errorf("no protocol with code %d", b[0])) panic(fmt.Errorf("no protocol with code %d", b[0]))
......
...@@ -204,12 +204,12 @@ func TestProtocols(t *testing.T) { ...@@ -204,12 +204,12 @@ func TestProtocols(t *testing.T) {
} }
ps := m.Protocols() ps := m.Protocols()
if ps[0] != ProtocolWithName("ip4") { if ps[0].Code != ProtocolWithName("ip4").Code {
t.Error(ps[0], ProtocolWithName("ip4")) t.Error(ps[0], ProtocolWithName("ip4"))
t.Error("failed to get ip4 protocol") 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(ps[1], ProtocolWithName("udp"))
t.Error("failed to get udp protocol") t.Error("failed to get udp protocol")
} }
......
...@@ -28,38 +28,38 @@ const ( ...@@ -28,38 +28,38 @@ const (
) )
// Protocols is the list of multiaddr protocols supported by this module. // Protocols is the list of multiaddr protocols supported by this module.
var Protocols = []*Protocol{ var Protocols = []Protocol{
&Protocol{P_IP4, 32, "ip4", CodeToVarint(P_IP4)}, Protocol{P_IP4, 32, "ip4", CodeToVarint(P_IP4)},
&Protocol{P_TCP, 16, "tcp", CodeToVarint(P_TCP)}, Protocol{P_TCP, 16, "tcp", CodeToVarint(P_TCP)},
&Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP)}, Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP)},
&Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP)}, Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP)},
&Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)}, Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)},
// these require varint: // these require varint:
&Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)}, Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)},
&Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)}, Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)},
&Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)}, Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)},
// {480, 0, "http"}, // {480, 0, "http"},
// {443, 0, "https"}, // {443, 0, "https"},
} }
// ProtocolWithName returns the Protocol description with given string name. // ProtocolWithName returns the Protocol description with given string name.
func ProtocolWithName(s string) *Protocol { func ProtocolWithName(s string) Protocol {
for _, p := range Protocols { for _, p := range Protocols {
if p.Name == s { if p.Name == s {
return p return p
} }
} }
return nil return Protocol{}
} }
// ProtocolWithCode returns the Protocol description with given protocol code. // ProtocolWithCode returns the Protocol description with given protocol code.
func ProtocolWithCode(c int) *Protocol { func ProtocolWithCode(c int) Protocol {
for _, p := range Protocols { for _, p := range Protocols {
if p.Code == c { if p.Code == c {
return p return p
} }
} }
return nil return Protocol{}
} }
// CodeToVarint converts an integer to a varint-encoded []byte // CodeToVarint converts an integer to a varint-encoded []byte
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment