Skip to content

Custom Filesystems

gogenfilter uses Go’s fs.FS interface, making it easy to test without real files or to read from custom sources.

By default, the filter uses os.DirFS(".") (the current working directory):

package main
import "github.com/LarsArtmann/gogenfilter/v3"
func main() {
opts, err := gogenfilter.WithFilterOptions(gogenfilter.FilterAll)
if err != nil {
panic(err)
}
f, err := gogenfilter.NewFilter(opts)
if err != nil {
panic(err)
}
// Uses os.DirFS(".") internally
_ = f
}

Use fstest.MapFS for deterministic, isolated tests:

package main
import (
"testing/fstest"
"github.com/LarsArtmann/gogenfilter/v3"
)
func main() {
mapFS := fstest.MapFS{
"db/models.go": &fstest.MapFile{
Data: []byte("// Code generated by sqlc. DO NOT EDIT.\npackage db\n"),
},
"main.go": &fstest.MapFile{
Data: []byte("package main\n"),
},
}
opts, err := gogenfilter.WithFilterOptions(gogenfilter.FilterSQLC)
if err != nil {
panic(err)
}
fsConfig := gogenfilter.WithFS(mapFS)
f, err := gogenfilter.NewFilter(opts, fsConfig)
if err != nil {
panic(err)
}
filtered, _ := f.Filter("db/models.go") // true
filtered, _ = f.Filter("main.go") // false
_ = filtered
}

Implement fs.FS for any source — embedded files, archives, remote storage:

package main
import (
"embed"
"github.com/LarsArtmann/gogenfilter/v3"
)
//go:embed testdata/*
var testdata embed.FS
func main() {
opts, err := gogenfilter.WithFilterOptions(gogenfilter.FilterAll)
if err != nil {
panic(err)
}
f, err := gogenfilter.NewFilter(opts, gogenfilter.WithFS(testdata))
if err != nil {
panic(err)
}
_ = f
}

Passing nil to WithFS is safe — the filter falls back to os.DirFS(".").