Commit b9067889 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

go lint + unexporting some funcs

parent 6ee313e6
......@@ -8,7 +8,7 @@ import (
"strings"
)
func StringToBytes(s string) ([]byte, error) {
func stringToBytes(s string) ([]byte, error) {
b := []byte{}
sp := strings.Split(s, "/")
......@@ -26,7 +26,7 @@ func StringToBytes(s string) ([]byte, error) {
}
b = append(b, byte(p.Code))
a := AddressStringToBytes(p, sp[1])
a := addressStringToBytes(p, sp[1])
b = append(b, a...)
sp = sp[2:]
......@@ -34,7 +34,7 @@ func StringToBytes(s string) ([]byte, error) {
return b, nil
}
func BytesToString(b []byte) (ret string, err error) {
func bytesToString(b []byte) (ret string, err error) {
// panic handler, in case we try accessing bytes incorrectly.
defer func() {
if e := recover(); e != nil {
......@@ -53,7 +53,7 @@ func BytesToString(b []byte) (ret string, err error) {
s = strings.Join([]string{s, "/", p.Name}, "")
b = b[1:]
a := AddressBytesToString(p, b[:(p.Size/8)])
a := addressBytesToString(p, b[:(p.Size/8)])
if len(a) > 0 {
s = strings.Join([]string{s, "/", a}, "")
}
......@@ -63,7 +63,7 @@ func BytesToString(b []byte) (ret string, err error) {
return s, nil
}
func AddressStringToBytes(p *Protocol, s string) []byte {
func addressStringToBytes(p *Protocol, s string) []byte {
switch p.Code {
// ipv4,6
......@@ -83,7 +83,7 @@ func AddressStringToBytes(p *Protocol, s string) []byte {
return []byte{}
}
func AddressBytesToString(p *Protocol, b []byte) string {
func addressBytesToString(p *Protocol, b []byte) string {
switch p.Code {
// ipv4,6
......
......@@ -5,22 +5,26 @@ import (
"strings"
)
// Multiaddr is the data structure representing a multiaddr
type Multiaddr struct {
Bytes []byte
}
// NewMultiaddr parses and validates an input string, returning a *Multiaddr
func NewMultiaddr(s string) (*Multiaddr, error) {
b, err := StringToBytes(s)
b, err := stringToBytes(s)
if err != nil {
return nil, err
}
return &Multiaddr{Bytes: b}, nil
}
// String returns the string representation of a Multiaddr
func (m *Multiaddr) String() (string, error) {
return BytesToString(m.Bytes)
return bytesToString(m.Bytes)
}
// Protocols returns the list of protocols this Multiaddr has.
func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
// panic handler, in case we try accessing bytes incorrectly.
......@@ -44,12 +48,14 @@ func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
return ps, nil
}
// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
func (m *Multiaddr) Encapsulate(o *Multiaddr) *Multiaddr {
b := make([]byte, len(m.Bytes)+len(o.Bytes))
b = append(m.Bytes, o.Bytes...)
return &Multiaddr{Bytes: b}
}
// 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 {
......@@ -68,9 +74,10 @@ func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) {
return NewMultiaddr(s1[:i])
}
// DialArgs is a convenience function returning arguments for use in net.Dial
func (m *Multiaddr) DialArgs() (string, string, error) {
if !m.IsThinWaist() {
return "", "", fmt.Errorf("%s is not a 'thin waist' address.", m)
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
}
str, err := m.String()
......@@ -84,6 +91,8 @@ func (m *Multiaddr) DialArgs() (string, string, error) {
return network, host, nil
}
// IsThinWaist returns whether this multiaddr includes "Thin Waist" Protocols.
// This means: /{IP4, IP6}/{TCP, UDP}
func (m *Multiaddr) IsThinWaist() bool {
p, err := m.Protocols()
if err != nil {
......
......@@ -14,7 +14,7 @@ func TestStringToBytes(t *testing.T) {
t.Error("failed to decode hex", h)
}
b2, err := StringToBytes(s)
b2, err := stringToBytes(s)
if err != nil {
t.Error("failed to convert", s)
}
......@@ -35,7 +35,7 @@ func TestBytesToString(t *testing.T) {
t.Error("failed to decode hex", h)
}
s2, err := BytesToString(b)
s2, err := bytesToString(b)
if err != nil {
t.Error("failed to convert", b)
}
......
package multiaddr
// Protocol is a Multiaddr protocol description structure.
type Protocol struct {
Code int
Size int
......@@ -10,7 +11,6 @@ type Protocol struct {
// 1. avoid parsing the csv
// 2. ensuring errors in the csv don't screw up code.
// 3. changing a number has to happen in two places.
const (
P_IP4 = 4
P_TCP = 6
......@@ -20,6 +20,7 @@ const (
P_SCTP = 132
)
// Protocols is the list of multiaddr protocols supported by this module.
var Protocols = []*Protocol{
&Protocol{P_IP4, 32, "ip4"},
&Protocol{P_TCP, 16, "tcp"},
......@@ -32,6 +33,7 @@ var Protocols = []*Protocol{
// {443, 0, "https"},
}
// ProtocolWithName returns the Protocol description with given string name.
func ProtocolWithName(s string) *Protocol {
for _, p := range Protocols {
if p.Name == s {
......@@ -41,6 +43,7 @@ func ProtocolWithName(s string) *Protocol {
return nil
}
// ProtocolWithCode returns the Protocol description with given protocol code.
func ProtocolWithCode(c int) *Protocol {
for _, p := range Protocols {
if p.Code == c {
......
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