component.go 2.71 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
package multiaddr

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"strings"
)

// Component is a single multiaddr Component.
type Component struct {
	bytes    []byte
	protocol Protocol
	offset   int
}

func (c *Component) Bytes() []byte {
	return c.bytes
}

func (c *Component) Equal(o Multiaddr) bool {
	return bytes.Equal(c.bytes, o.Bytes())
}

func (c *Component) Protocols() []Protocol {
	return []Protocol{c.protocol}
}

func (c *Component) Decapsulate(o Multiaddr) Multiaddr {
	if c.Equal(o) {
		return nil
	}
	return c
}

func (c *Component) Encapsulate(o Multiaddr) Multiaddr {
	m := multiaddr{bytes: c.bytes}
	return m.Encapsulate(o)
}

func (c *Component) ValueForProtocol(code int) (string, error) {
	if c.protocol.Code != code {
		return "", ErrProtocolNotFound
	}
	return c.Value(), nil
}

func (c *Component) Protocol() Protocol {
	return c.protocol
}

func (c *Component) RawValue() []byte {
	return c.bytes[c.offset:]
}

func (c *Component) Value() string {
	if c.protocol.Transcoder == nil {
		return ""
	}
	value, err := c.protocol.Transcoder.BytesToString(c.bytes[c.offset:])
	if err != nil {
		// This Component must have been checked.
		panic(err)
	}
	return value
}

func (c *Component) String() string {
	var b strings.Builder
	c.writeTo(&b)
	return b.String()
}

// writeTo is an efficient, private function for string-formatting a multiaddr.
// Trust me, we tend to allocate a lot when doing this.
func (c *Component) writeTo(b *strings.Builder) {
	b.WriteByte('/')
	b.WriteString(c.protocol.Name)
	value := c.Value()
	if len(value) == 0 {
		return
	}
	if !(c.protocol.Path && value[0] == '/') {
		b.WriteByte('/')
	}
	b.WriteString(value)
}

// NewComponent constructs a new multiaddr component
func NewComponent(protocol, value string) (*Component, error) {
	p := ProtocolWithName(protocol)
	if p.Code == 0 {
		return nil, fmt.Errorf("unsupported protocol: %s", protocol)
	}
	if p.Transcoder != nil {
		bts, err := p.Transcoder.StringToBytes(value)
		if err != nil {
			return nil, err
		}
		return newComponent(p, bts), nil
	} else if value != "" {
		return nil, fmt.Errorf("protocol %s doesn't take a value", p.Name)
	}
	return newComponent(p, nil), nil
	// TODO: handle path /?
}

func newComponent(protocol Protocol, bvalue []byte) *Component {
	size := len(bvalue)
	size += len(protocol.VCode)
	if protocol.Size < 0 {
		size += VarintSize(len(bvalue))
	}
	maddr := make([]byte, size)
	var offset int
	offset += copy(maddr[offset:], protocol.VCode)
	if protocol.Size < 0 {
		offset += binary.PutUvarint(maddr[offset:], uint64(len(bvalue)))
	}
	copy(maddr[offset:], bvalue)

	// For debugging
	if len(maddr) != offset+len(bvalue) {
		panic("incorrect length")
	}

	return &Component{
		bytes:    maddr,
		protocol: protocol,
		offset:   offset,
	}
}