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
ae64eb99
Commit
ae64eb99
authored
Jan 18, 2016
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrite value for protocols and add more tests
parent
a581da3f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
18 deletions
+16
-18
multiaddr.go
multiaddr.go
+5
-18
multiaddr_test.go
multiaddr_test.go
+11
-0
No files found.
multiaddr.go
View file @
ae64eb99
...
@@ -117,28 +117,15 @@ func (m *multiaddr) Decapsulate(o Multiaddr) Multiaddr {
...
@@ -117,28 +117,15 @@ func (m *multiaddr) Decapsulate(o Multiaddr) Multiaddr {
var
ErrProtocolNotFound
=
fmt
.
Errorf
(
"protocol not found in multiaddr"
)
var
ErrProtocolNotFound
=
fmt
.
Errorf
(
"protocol not found in multiaddr"
)
func
(
m
*
multiaddr
)
ValueForProtocol
(
code
int
)
(
string
,
error
)
{
func
(
m
*
multiaddr
)
ValueForProtocol
(
code
int
)
(
string
,
error
)
{
protos
:=
m
.
Protocols
()
for
_
,
sub
:=
range
Split
(
m
)
{
found
:=
-
1
p
:=
sub
.
Protocols
()[
0
]
index
:=
2
if
p
.
Code
==
code
{
for
i
,
p
:=
range
protos
{
if
code
==
p
.
Code
{
if
p
.
Size
==
0
{
if
p
.
Size
==
0
{
return
""
,
nil
return
""
,
nil
}
}
found
=
i
return
strings
.
Split
(
sub
.
String
(),
"/"
)[
2
],
nil
break
}
else
{
index
+=
2
if
p
.
Size
==
0
{
index
--
}
}
}
}
}
if
found
==
-
1
{
return
""
,
ErrProtocolNotFound
return
""
,
ErrProtocolNotFound
}
return
strings
.
Split
(
m
.
String
(),
"/"
)[
index
],
nil
}
}
multiaddr_test.go
View file @
ae64eb99
...
@@ -330,4 +330,15 @@ func TestGetValue(t *testing.T) {
...
@@ -330,4 +330,15 @@ func TestGetValue(t *testing.T) {
default
:
default
:
t
.
Fatalf
(
"expected ErrProtocolNotFound but got: %s"
,
err
)
t
.
Fatalf
(
"expected ErrProtocolNotFound but got: %s"
,
err
)
}
}
a
=
newMultiaddr
(
t
,
"/ip4/0.0.0.0"
)
// only one addr
assertValueForProto
(
t
,
a
,
P_IP4
,
"0.0.0.0"
)
a
=
newMultiaddr
(
t
,
"/ip4/0.0.0.0/ip4/0.0.0.0/ip4/0.0.0.0"
)
// same sub-addr
assertValueForProto
(
t
,
a
,
P_IP4
,
"0.0.0.0"
)
a
=
newMultiaddr
(
t
,
"/ip4/0.0.0.0/udp/12345/utp"
)
// ending in a no-value one.
assertValueForProto
(
t
,
a
,
P_IP4
,
"0.0.0.0"
)
assertValueForProto
(
t
,
a
,
P_UDP
,
"12345"
)
assertValueForProto
(
t
,
a
,
P_UTP
,
""
)
}
}
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