multiprotocol

package module
v0.0.0-...-fabc926 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2020 License: MIT Imports: 13 Imported by: 0

README

go-multiprotocol

Version MIT License API Reference Go Report Card Build Status

A lot of this code has been adapted from multiaddr.

Multiprotocol can be initialized using a CSV file, for the CSV format see the specification.

package main

import mp "github.com/vacp2p/go-multiprotocol"

err := mp.Init("testdata/multiprotocol.csv")
// err non-nil when parsing failed.

mp, err := mp.NewMultiprotocol("/vac/waku/2/store/2/relay/2")
// err non-nil when parsing failed.

Protocols can also be added programatically using the AddProtocol function.

Documentation

Index

Constants

View Source
const (
	LengthPrefixedVarSize = -1
)

These are special sizes

Variables

View Source
var ErrProtocolNotFound = fmt.Errorf("protocol not found in multiprotocol")
View Source
var Protocols = []Protocol{}

Protocols is the list of multiprotocol protocols supported by this module.

Functions

func AddProtocol

func AddProtocol(p Protocol) error

func ForEach

func ForEach(m Multiprotocol, cb func(c Component) bool)

func Init

func Init(path string) error

Init initializes multiprotocol with a CSV file

Types

type Component

type Component struct {
	// contains filtered or unexported fields
}

func NewComponent

func NewComponent(protocol, value string) (*Component, error)

NewComponent constructs a new multiprotocol component

func (*Component) Bytes

func (c *Component) Bytes() []byte

func (*Component) Equal

func (c *Component) Equal(o Multiprotocol) bool

func (*Component) MarshalBinary

func (c *Component) MarshalBinary() ([]byte, error)

func (*Component) MarshalJSON

func (c *Component) MarshalJSON() ([]byte, error)

func (*Component) MarshalText

func (c *Component) MarshalText() ([]byte, error)

func (*Component) Protocol

func (c *Component) Protocol() Protocol

func (*Component) Protocols

func (c *Component) Protocols() []Protocol

func (*Component) RawValue

func (c *Component) RawValue() []byte

func (*Component) String

func (c *Component) String() string

func (*Component) UnmarshalBinary

func (c *Component) UnmarshalBinary(data []byte) error

func (*Component) UnmarshalJSON

func (c *Component) UnmarshalJSON(data []byte) error

func (*Component) UnmarshalText

func (c *Component) UnmarshalText(data []byte) error

func (*Component) Value

func (c *Component) Value() string

func (*Component) ValueForProtocol

func (c *Component) ValueForProtocol(code int) (string, error)

type Multiprotocol

type Multiprotocol interface {
	json.Marshaler
	json.Unmarshaler
	encoding.TextMarshaler
	encoding.TextUnmarshaler
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler

	// Equal returns whether two Multiprotocols are exactly equal
	Equal(Multiprotocol) bool

	// Bytes returns the []byte representation of this Multiprotocol
	//
	// This function may expose immutable, internal state. Do not modify.
	Bytes() []byte

	// String returns the string representation of this Multiprotocol
	// (may panic if internal state is corrupted)
	String() string

	// Protocols returns the list of Protocols this Multiprotocol includes
	// will panic if protocol code incorrect (and bytes accessed incorrectly)
	Protocols() []Protocol

	// ValueForProtocol returns the value (if any) following the specified protocol
	//
	// Note: protocols can appear multiple times in a single multiprotocol.
	// Consider using `ForEach` to walk over the addr manually.
	ValueForProtocol(code int) (string, error)
}

Multiprotocol is a cross-protocol, cross-platform format for representing self-describing protocol identifiers. Learn more here: https://github.com/vacp2p/multiprotocol

Multiprotocol have both a binary and string representation.

import mp "github.com/vacp2p/go-multiprotocol"

proto, err := mp.NewMultiprotocol("/vac/waku/2")
// err non-nil when parsing failed.

func NewMultiprotocol

func NewMultiprotocol(s string) (p Multiprotocol, err error)

NewMultiprotocol parses and validates an input string, returning a *Multiprotocol

func NewMultiprotocolBytes

func NewMultiprotocolBytes(b []byte) (a Multiprotocol, err error)

NewMultiprotocolBytes initializes a Multiprotocol from a byte representation. It validates it as an input string.

type Protocol

type Protocol struct {
	// Name is the string representation of the protocol code. E.g., ip4,
	// ip6, tcp, udp, etc.
	Name string

	// Code is the protocol's multicodec (a normal, non-varint number).
	Code int

	// VCode is a precomputed varint encoded version of Code.
	VCode []byte

	// Size is the size of the argument to this protocol.
	//
	// * Size == 0 means this protocol takes no argument.
	// * Size >  0 means this protocol takes a constant sized argument.
	// * Size <  0 means this protocol takes a variable length, varint
	//             prefixed argument.
	Size int // a size of -1 indicates a length-prefixed variable size

	// Transcoder converts between the byte representation and the string
	// representation of this protocol's argument (if any).
	//
	// This should only be non-nil if Size != 0
	Transcoder multiaddr.Transcoder
}

Protocol is a protocol description structure. This was taken from Multiaddr

func ProtocolWithCode

func ProtocolWithCode(c int) Protocol

ProtocolWithCode returns the Protocol description with given protocol code.

func ProtocolWithName

func ProtocolWithName(s string) Protocol

ProtocolWithName returns the Protocol description with given string name.

func ProtocolsWithString

func ProtocolsWithString(s string) ([]Protocol, error)

ProtocolsWithString returns a slice of protocols matching given string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL