Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid returning always nil error in strict filterset #2399

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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