Skip to content

Commit

Permalink
Avoid returning always nil error in strict filterset
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Jan 25, 2021
1 parent 98c3781 commit c960ddb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/processor/filterset/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 6 additions & 10 deletions internal/processor/filterset/strict/strictfilterset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,20 @@ 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.
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{}{}
}
}
6 changes: 2 additions & 4 deletions internal/processor/filterset/strict/strictfilterset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c960ddb

Please sign in to comment.