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
334b79e5
Commit
334b79e5
authored
Dec 14, 2021
by
Marten Seemann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move FilterAddrs here from go-addr-util
parent
18266e97
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
1 deletion
+40
-1
multiaddr.go
multiaddr.go
+16
-0
multiaddr_test.go
multiaddr_test.go
+24
-1
No files found.
multiaddr.go
View file @
334b79e5
...
...
@@ -184,3 +184,19 @@ func (m *multiaddr) ValueForProtocol(code int) (value string, err error) {
})
return
}
// FilterAddrs is a filter that removes certain addresses, according to the given filters.
// If all filters return true, the address is kept.
func
FilterAddrs
(
a
[]
Multiaddr
,
filters
...
func
(
Multiaddr
)
bool
)
[]
Multiaddr
{
b
:=
make
([]
Multiaddr
,
0
,
len
(
a
))
for
_
,
addr
:=
range
a
{
good
:=
true
for
_
,
filter
:=
range
filters
{
good
=
good
&&
filter
(
addr
)
}
if
good
{
b
=
append
(
b
,
addr
)
}
}
return
b
}
multiaddr_test.go
View file @
334b79e5
...
...
@@ -8,6 +8,8 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
)
...
...
@@ -226,7 +228,7 @@ func TestStringToBytes(t *testing.T) {
t.Error("failed to decode hex", h)
}
//t.Log("196", h, []byte(b1))
//
t.Log("196", h, []byte(b1))
b2, err := stringToBytes(s)
if err != nil {
...
...
@@ -739,3 +741,24 @@ func TestComponentJSONMarshaler(t *testing.T) {
t.Error("expected equal components in circular marshaling test")
}
}
func TestFilterAddrs(t *testing.T) {
bad := []Multiaddr{
newMultiaddr(t, "/ip6/fe80::1/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::100/tcp/1234"),
}
good := []Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
newMultiaddr(t, "/ip4/1.1.1.1/tcp/999"),
newMultiaddr(t, "/ip4/1.2.3.4/udp/1234/utp"),
}
goodAndBad := append(good, bad...)
filter := func(addr Multiaddr) bool {
return addr.Protocols()[0].Code == P_IP4
}
require.Empty(t, FilterAddrs(bad, filter))
require.ElementsMatch(t, FilterAddrs(good, filter), good)
require.ElementsMatch(t, FilterAddrs(goodAndBad, filter), good)
}
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