From c960ddb271e6bd19edc5401d198206f46ef1f39f Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Mon, 25 Jan 2021 08:24:16 -0800 Subject: [PATCH] Avoid returning always nil error in strict filterset Signed-off-by: Bogdan Drutu --- internal/processor/filterset/config.go | 2 +- .../filterset/strict/strictfilterset.go | 16 ++++++---------- .../filterset/strict/strictfilterset_test.go | 6 ++---- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/internal/processor/filterset/config.go b/internal/processor/filterset/config.go index df131f75397..da8efcfda57 100644 --- a/internal/processor/filterset/config.go +++ b/internal/processor/filterset/config.go @@ -50,7 +50,7 @@ func CreateFilterSet(filters []string, cfg *Config) (FilterSet, error) { return regexp.NewFilterSet(filters, cfg.RegexpConfig) case Strict: // Strict FilterSets do not have any extra configuration options, so call the constructor directly. - return strict.NewFilterSet(filters) + return strict.NewFilterSet(filters), nil default: return nil, fmt.Errorf("unrecognized %v: '%v', valid types are: %v", MatchTypeFieldName, cfg.MatchType, validMatchTypes) } diff --git a/internal/processor/filterset/strict/strictfilterset.go b/internal/processor/filterset/strict/strictfilterset.go index 459f55866ca..8394b256030 100644 --- a/internal/processor/filterset/strict/strictfilterset.go +++ b/internal/processor/filterset/strict/strictfilterset.go @@ -24,13 +24,16 @@ type FilterSet struct { } // NewFilterSet constructs a FilterSet of exact string matches. -func NewFilterSet(filters []string) (*FilterSet, error) { +func NewFilterSet(filters []string) *FilterSet { fs := &FilterSet{ filters: make(map[string]struct{}, len(filters)), } - fs.addFilters(filters) - return fs, nil + for _, f := range filters { + fs.filters[f] = struct{}{} + } + + return fs } // Matches returns true if the given string matches any of the FilterSet's filters. @@ -38,10 +41,3 @@ func (sfs *FilterSet) Matches(toMatch string) bool { _, ok := sfs.filters[toMatch] return ok } - -// addFilters all the given filters. -func (sfs *FilterSet) addFilters(filters []string) { - for _, f := range filters { - sfs.filters[f] = struct{}{} - } -} diff --git a/internal/processor/filterset/strict/strictfilterset_test.go b/internal/processor/filterset/strict/strictfilterset_test.go index 0d5a51cc161..55ab4522985 100644 --- a/internal/processor/filterset/strict/strictfilterset_test.go +++ b/internal/processor/filterset/strict/strictfilterset_test.go @@ -43,17 +43,15 @@ func TestNewStrictFilterSet(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - fs, err := NewFilterSet(test.filters) + fs := NewFilterSet(test.filters) assert.Equal(t, test.success, fs != nil) - assert.Equal(t, test.success, err == nil) }) } } func TestStrictMatches(t *testing.T) { - fs, err := NewFilterSet(validStrictFilters) + fs := NewFilterSet(validStrictFilters) assert.NotNil(t, fs) - assert.NoError(t, err) matches := []string{ "exact_string_match",