parse filters text

This commit is contained in:
Annika Hannig 2024-01-18 11:23:59 +01:00
parent 498ce65af5
commit bd78a05f37
2 changed files with 115 additions and 0 deletions

View File

@ -1,9 +1,11 @@
package api
import (
"fmt"
"log"
"net/url"
"strconv"
"strings"
)
// SearchKeys are filterable attributes
@ -535,6 +537,61 @@ func FiltersFromQuery(query url.Values) (*SearchFilters, error) {
return queryFilters, nil
}
// parseCommunityFilterText creates FilterValue from the
// text input which may be a api.Community or api.ExtCommunity.
func parseCommunityFilterText(text string) (string, *SearchFilter, error) {
tokens := strings.Split(text, ":")
if len(tokens) < 2 {
return "", nil, fmt.Errorf("BGP community incomplete")
}
// Check if we are dealing with an ext. community
maybeExt := false
_, err := strconv.Atoi(tokens[0])
if err != nil {
maybeExt = true
}
// Parse filter value
if maybeExt {
filter, err := parseExtCommunityValue(text)
if err != nil {
return "", nil, err
}
return SearchKeyExtCommunities, filter, nil
}
filter, err := parseCommunityValue(text)
if err != nil {
return "", nil, err
}
if len(tokens) == 2 {
return SearchKeyCommunities, filter, nil
}
return SearchKeyLargeCommunities, filter, nil
}
// FiltersFromQueryText parses the passed list of filters
// extracted from the query string and creates the filter.
func FiltersFromQueryText(filters []string) (*SearchFilters, error) {
queryFilters := NewSearchFilters()
for _, filter := range filters {
if strings.HasPrefix(filter, "#") { // Community query
key, value, err := parseCommunityFilterText(filter[1:])
if err != nil {
return nil, err
}
queryFilters.GetGroupByKey(key).AddFilter(&SearchFilter{
Value: value,
})
}
}
return queryFilters, nil
}
// MatchRoute checks if a route matches all filters.
// Unless all filters are blank.
func (s *SearchFilters) MatchRoute(r Filterable) bool {

View File

@ -680,3 +680,61 @@ func TestSearchFiltersHasKey(t *testing.T) {
t.Error("sources should not be filtered")
}
}
func TestParseInvalidCommunityFilterText(t *testing.T) {
_, _, err := parseCommunityFilterText("")
if err == nil {
t.Error("Expected error for empty filter")
}
t.Log(err)
_, _, err = parseCommunityFilterText("23452")
if err == nil {
t.Error("Expected error for empty filter")
}
}
func TestParseCommunityFilterText(t *testing.T) {
text := "12345:23"
key, filter, err := parseCommunityFilterText(text)
if err != nil {
t.Fatal(err)
}
if key != SearchKeyCommunities {
t.Error("Expected key to be", SearchKeyCommunities, "but got:", key)
}
v := filter.Value.(Community)
if v[0] != 12345 && v[1] != 23 {
t.Error("Expected community to be 12345:23 but got:", v)
}
}
func TestParseLargeCommunityFilterText(t *testing.T) {
text := "12345:23:42"
key, filter, err := parseCommunityFilterText(text)
if err != nil {
t.Fatal(err)
}
if key != SearchKeyLargeCommunities {
t.Error("Expected key to be", SearchKeyLargeCommunities, "but got:", key)
}
v := filter.Value.(Community)
if v[0] != 12345 && v[1] != 23 && v[2] != 42 {
t.Error("Expected community to be 12345:23:42 but got:", v)
}
}
func TestParseExtCommunityFilterText(t *testing.T) {
text := "ro:12345:23"
key, filter, err := parseCommunityFilterText(text)
if err != nil {
t.Fatal(err)
}
if key != SearchKeyExtCommunities {
t.Error("Expected key to be", SearchKeyExtCommunities, "but got:", key)
}
v := filter.Value.(ExtCommunity)
if v[0] != "ro" && v[1] != "12345" && v[2] != "23" {
t.Error("Expected community to be ro:12345:23 but got:", v)
}
}