Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mf
go-multiaddr
Commits
66883764
Commit
66883764
authored
Jun 22, 2018
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unexport ProtocolsByName and add protocolsByCode
parent
47e3ab13
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
22 deletions
+21
-22
protocols.go
protocols.go
+21
-22
No files found.
protocols.go
View file @
66883764
...
...
@@ -81,16 +81,18 @@ var (
protoUNIX
=
Protocol
{
P_UNIX
,
LengthPrefixedVarSize
,
"unix"
,
CodeToVarint
(
P_UNIX
),
true
,
TranscoderUnix
}
)
var
ProtocolsByName
=
map
[
string
]
Protocol
{}
var
protocolsByName
=
map
[
string
]
Protocol
{}
var
protocolsByCode
=
map
[
int
]
Protocol
{}
func
init
()
{
for
_
,
p
:=
range
Protocols
{
ProtocolsByName
[
p
.
Name
]
=
p
protocolsByName
[
p
.
Name
]
=
p
protocolsByCode
[
p
.
Code
]
=
p
}
// explicitly set both of these
P
rotocolsByName
[
"p2p"
]
=
protoP2P
P
rotocolsByName
[
"ipfs"
]
=
protoP2P
p
rotocolsByName
[
"p2p"
]
=
protoP2P
p
rotocolsByName
[
"ipfs"
]
=
protoP2P
}
// SwapToP2pMultiaddrs is a function to make the transition from /ipfs/...
...
...
@@ -115,44 +117,41 @@ func SwapToP2pMultiaddrs() {
protoP2P
.
Name
=
"p2p"
ProtocolsByName
[
"ipfs"
]
=
protoP2P
ProtocolsByName
[
"p2p"
]
=
protoP2P
protocolsByName
[
"ipfs"
]
=
protoP2P
protocolsByName
[
"p2p"
]
=
protoP2P
protocolsByCode
[
protoP2P
.
Code
]
=
protoP2P
}
func
AddProtocol
(
p
Protocol
)
error
{
if
_
,
ok
:=
protocolsByName
[
p
.
Name
];
ok
{
return
fmt
.
Errorf
(
"protocol by the name %q already exists"
,
p
.
Name
)
}
if
_
,
ok
:=
protocolsByCode
[
p
.
Code
];
ok
{
return
fmt
.
Errorf
(
"protocol code %d already taken by %q"
,
p
.
Code
,
p
.
Code
)
}
if
p
.
Size
!=
0
&&
p
.
Transcoder
==
nil
{
return
fmt
.
Errorf
(
"protocols with arguments must define transcoders"
)
}
if
p
.
Path
&&
p
.
Size
>=
0
{
return
fmt
.
Errorf
(
"path protocols must have variable-length sizes"
)
}
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
)
ProtocolsByName
[
p
.
Name
]
=
p
protocolsByName
[
p
.
Name
]
=
p
protocolsByCode
[
p
.
Code
]
=
p
return
nil
}
// ProtocolWithName returns the Protocol description with given string name.
func
ProtocolWithName
(
s
string
)
Protocol
{
return
P
rotocolsByName
[
s
]
return
p
rotocolsByName
[
s
]
}
// ProtocolWithCode returns the Protocol description with given protocol code.
func
ProtocolWithCode
(
c
int
)
Protocol
{
for
_
,
p
:=
range
Protocols
{
if
p
.
Code
==
c
{
return
p
}
}
return
Protocol
{}
return
protocolsByCode
[
c
]
}
// ProtocolsWithString returns a slice of protocols matching given string.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment