Skip to content

Commit

Permalink
docs: improve comments and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Sep 25, 2024
1 parent d8edbe6 commit 3d2a8e5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
27 changes: 25 additions & 2 deletions routing/http/server/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func parseFilter(param string) []string {
}

// applyFiltersToIter applies the filters to the given iterator and returns a new iterator.
//
// The function iterates over the input iterator, applying the specified filters to each record.
// It supports both positive and negative filters for both addresses and protocols.
//
// Parameters:
// - recordsIter: An iterator of types.Record to be filtered.
// - filterAddrs: A slice of strings representing the address filter criteria.
// - filterProtocols: A slice of strings representing the protocol filter criteria.
func applyFiltersToIter(recordsIter iter.ResultIter[types.Record], filterAddrs, filterProtocols []string) iter.ResultIter[types.Record] {
mappedIter := iter.Map(recordsIter, func(v iter.Result[types.Record]) iter.Result[types.Record] {
if v.Err != nil || v.Val == nil {
Expand Down Expand Up @@ -128,8 +136,23 @@ func applyFilters(provider *types.PeerRecord, filterAddrs, filterProtocols []str
return provider
}

// If there are only negative filters, no addresses will be included in the result. The function will return an empty list.
// For an address to be included, it must pass all negative filters
// applyAddrFilter filters a list of multiaddresses based on the provided filter query.
//
// Parameters:
// - addrs: A slice of types.Multiaddr to be filtered.
// - filterAddrsQuery: A slice of strings representing the filter criteria.
//
// The function supports both positive and negative filters:
// - Positive filters (e.g., "tcp", "udp") include addresses that match the specified protocols.
// - Negative filters (e.g., "!tcp", "!udp") exclude addresses that match the specified protocols.
//
// If no filters are provided, the original list of addresses is returned unchanged.
// If only negative filters are provided, addresses not matching any negative filter are included.
// If positive filters are provided, only addresses matching at least one positive filter (and no negative filters) are included.
// If both positive and negative filters are provided, the address must match at least one positive filter and no negative filters to be included.
//
// Returns:
// A new slice of types.Multiaddr containing only the addresses that pass the filter criteria.
func applyAddrFilter(addrs []types.Multiaddr, filterAddrsQuery []string) []types.Multiaddr {
if len(filterAddrsQuery) == 0 {
return addrs
Expand Down
5 changes: 5 additions & 0 deletions routing/http/server/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func TestApplyAddrFilter(t *testing.T) {
filterAddrs: []string{"!tcp"},
expectedAddrs: []types.Multiaddr{{Multiaddr: addr2}, {Multiaddr: addr5}, {Multiaddr: addr6}, {Multiaddr: addr8}},
},
{
name: "Filter TCP addresses that don't have WebSocket and p2p-circuit",
filterAddrs: []string{"tcp", "!ws", "!wss", "!p2p-circuit"},
expectedAddrs: []types.Multiaddr{{Multiaddr: addr1}},
},
{
name: "Include WebTransport and exclude p2p-circuit",
filterAddrs: []string{"webtransport", "!p2p-circuit"},
Expand Down

0 comments on commit 3d2a8e5

Please sign in to comment.