From b185168cd2d55c1c57b9eceaedb3f504ce3ff988 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sun, 17 Jan 2016 01:04:37 -0800 Subject: [PATCH] modularize multiaddr protocols --- protocols.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/protocols.go b/protocols.go index c233cef..8364d4c 100644 --- a/protocols.go +++ b/protocols.go @@ -55,6 +55,20 @@ var Protocols = []Protocol{ Protocol{P_IPFS, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_IPFS)}, } +func AddProtocol(p Protocol) error { + for _, pt := range Protocols { + if pt.Code == p.Code { + return fmt.Errorf("protocol code %d already taken by %q", p.Code, pt.Name) + } + if pt.Name == p.Name { + return fmt.Errorf("protocol by the name %q already exists", p.Name) + } + } + + Protocols = append(Protocols, p) + return nil +} + // ProtocolWithName returns the Protocol description with given string name. func ProtocolWithName(s string) Protocol { for _, p := range Protocols {