FilterKit is a modular, composable system that applies multiple validation criteria to strings in Go.
This repository provides a modular, composable filtering system in Go. The main goal is to determine whether a given string satisfies one or more validation criteria, using a clean, extensible design. Here's an overview of the key components:
-
Filter Interface
Each filter implements thefilter.Filterinterface, which declares a single method:type Filter interface { Accepts(s string) bool }This design allows different criteria to be wrapped as separate filters.
-
Allowed & Blocked Filters
allowed.Allowedloads a list of allowed strings and checks if an input exists in that list.blocked.Blockeddoes the opposite: it loads a list of blocked strings and returnsfalseif the input is in that list.
-
Length Filter
lengthBlocker.NewLengthBlocker(maxLength)validates whether a string’s length is below a specified maximum.
-
Intersection of Filters
intersectionOfFilters.IntersectionOfFilterscombines multiple filters and returnstrueonly if all included filters accept the input.
-
Crafting Filters
- The “craft” packages (e.g.,
craftAllowedFilter,craftBlockedFilter,craftMaxLengthFilter) read external files (each line treated as a string entry) and create corresponding filters. craftIntersectionFiltercreates the final composite filter by adding an allowed filter, a blocked filter, and the length filter toIntersectionOfFilters.
- The “craft” packages (e.g.,
-
Create text files listing the allowed and blocked strings (one item per line).
-
Instantiate the composite filter in your Go code:
import ( "filters/craftIntersectionFilter" ) func main() { // Example usage: compositeFilter := craftIntersectionFilter.CreateIntersectionFilter( "allowed.txt", // Path to file with allowed strings "blocked.txt", // Path to file with blocked strings 30, // Max length ) // Test any given string: testString := "SomeStringToTest" if compositeFilter.Accepts(testString) { // String passes all checks } else { // String fails one or more checks } } -
Extend or modify filters to fit your needs—just implement
filter.Filterfor new validation criteria and add it to your intersection filter.
- Modularity: Each filter has a single responsibility, making the system easy to extend.
- Composability:
IntersectionOfFilterscan combine any number of filters, enabling complex validation rules from simpler building blocks. - Simplicity: Each package handles a specific part of the filtering logic (loading data, validating strings, etc.), ensuring the code is straightforward to maintain.
This project uses Gomock for generating mocks in tests. Gomock is a powerful framework that allows you to create mock implementations of your interfaces and set expectations on method calls, helping to isolate components during testing. For more details and advanced usage, refer to the Gomock documentation.
To run the unit tests for FilterKit, follow these steps:
- Open your terminal.
- Navigate to the project root directory.: For example, if your project is in
/Users/yourname/FilterKit, run:bash cd /Users/yourname/FilterKit - Run the tests using the Go test command::
bash go test ./...This command will search for all files ending with_test.goin your project and execute the tests. - Review the test output. If all tests pass, you'll see output similar to:
PASS ok filters/allowed 0.417s```