package multiaddrimport ()// These are special sizesconst (LengthPrefixedVarSize = -1)// Protocol is a Multiaddr protocol description structure.typeProtocolstruct {// 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// Path indicates a path protocol (e.g., unix). When parsing multiaddr // strings, path protocols consume the remainder of the address instead // of stopping at the next forward slash. // // Size must be LengthPrefixedVarSize. Path bool// 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 Transcoder}var protocolsByName = map[string]Protocol{}var protocolsByCode = map[int]Protocol{}// Keep a map of pointers so that we can reuse the same pointer for the same protocol.var protocolPtrByCode = map[int]*Protocol{}// Protocols is the list of multiaddr protocols supported by this module.varProtocols = []Protocol{}func ( Protocol) error {if , := protocolsByName[.Name]; {returnfmt.Errorf("protocol by the name %q already exists", .Name) }if , := protocolsByCode[.Code]; {returnfmt.Errorf("protocol code %d already taken by %q", .Code, .Code) }if .Size != 0 && .Transcoder == nil {returnfmt.Errorf("protocols with arguments must define transcoders") }if .Path && .Size >= 0 {returnfmt.Errorf("path protocols must have variable-length sizes") }iflen(.VCode) == 0 {returnfmt.Errorf("protocol code %d is missing its VCode field", .Code) }Protocols = append(Protocols, )protocolsByName[.Name] = protocolsByCode[.Code] = protocolPtrByCode[.Code] = &returnnil}// ProtocolWithName returns the Protocol description with given string name.func ( string) Protocol {returnprotocolsByName[]}// ProtocolWithCode returns the Protocol description with given protocol code.func ( int) Protocol {returnprotocolsByCode[]}// ProtocolsWithString returns a slice of protocols matching given string.func ( string) ([]Protocol, error) { = strings.Trim(, "/") := strings.Split(, "/")iflen() == 0 {returnnil, nil } := make([]Protocol, len())for , := range { := ProtocolWithName()if .Code == 0 {returnnil, fmt.Errorf("no protocol with name: %s", ) } [] = }return , nil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.