change applyFn to interface

This commit is contained in:
Daisuke Maki 2026-02-20 14:05:22 +09:00
parent 88b5b7a39c
commit 8a9dfefb3a
3 changed files with 12 additions and 6 deletions

View file

@ -41,11 +41,17 @@ func (c *LineCollector) Lines() []line.Line {
return c.lines
}
// lineApplier is implemented by filter types that embed baseFilter to provide
// their type-specific matching logic.
type lineApplier interface {
applyInternal(ctx context.Context, lines []line.Line, em LineEmitter) error
}
// baseFilter provides shared implementations of Apply, ApplyCollect,
// NewContext, and BufSize for filters that follow the applyInternal pattern.
// Filters embed this type and set applyFn to their type-specific matching logic.
// Filters embed this type and set impl to their concrete filter value.
type baseFilter struct {
applyFn func(ctx context.Context, lines []line.Line, em LineEmitter) error
impl lineApplier
}
// NewContext returns a context initialized with the given query for pipeline use.
@ -59,13 +65,13 @@ func (b *baseFilter) BufSize() int {
// Apply runs the filter's matching logic on lines, sending matches to out.
func (b *baseFilter) Apply(ctx context.Context, lines []line.Line, out pipeline.ChanOutput) error {
return b.applyFn(ctx, lines, &chanEmitter{out: out})
return b.impl.applyInternal(ctx, lines, &chanEmitter{out: out})
}
// ApplyCollect runs the filter and returns matched lines directly as a slice,
// bypassing channel-based output for better performance in parallel paths.
func (b *baseFilter) ApplyCollect(ctx context.Context, lines []line.Line) ([]line.Line, error) {
c := NewLineCollector(len(lines) / 2)
err := b.applyFn(ctx, lines, c)
err := b.impl.applyInternal(ctx, lines, c)
return c.Lines(), err
}

View file

@ -34,7 +34,7 @@ func NewFuzzy(sortLongest bool) *Fuzzy {
ff := &Fuzzy{
sortLongest: sortLongest,
}
ff.applyFn = ff.applyInternal
ff.impl = ff
return ff
}

View file

@ -127,7 +127,7 @@ func newRegexpFilter(name string, flags regexpFlags, quotemeta bool) *Regexp {
quotemeta: quotemeta,
name: name,
}
rf.applyFn = rf.applyInternal
rf.impl = rf
return rf
}