jx/internal/iofilter/filter.go
Matthew Rich e859ef136c
Some checks failed
Lint / golangci-lint (push) Successful in 9m49s
Declarative Tests / test (push) Failing after 49s
add io filter
2024-04-17 16:49:41 -07:00

33 lines
578 B
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package iofilter
import (
"io"
"log/slog"
)
type ReadFilter func([]byte, int, error) (int, error)
type Reader struct {
input io.Reader
filter ReadFilter
}
func NewReader(reader io.Reader, filter ReadFilter) *Reader {
return &Reader{
input: reader,
filter: filter,
}
}
func (r *Reader) Read(p []byte) (n int, err error) {
n, err = r.input.Read(p)
if r.filter != nil {
return r.filter(p, n, err)
} else {
slog.Warn("iofilter.Reader.Read() - nil filter")
}
return n, err
}