add io filter
Some checks failed
Lint / golangci-lint (push) Successful in 9m49s
Declarative Tests / test (push) Failing after 49s

This commit is contained in:
Matthew Rich 2024-04-17 16:49:41 -07:00
parent 0a6d1a93ee
commit e859ef136c
3 changed files with 55 additions and 1 deletions

View File

@ -0,0 +1,32 @@
// 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
}

View File

@ -0,0 +1,22 @@
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package iofilter
import (
"github.com/stretchr/testify/assert"
"testing"
"strings"
)
func TestNewReader(t *testing.T) {
buffer := make([]byte, 20)
reader := NewReader(strings.NewReader("testdata"), func(p []byte, in int, e error) (int, error) {
copy(p, []byte("foo"))
return 3, nil
})
assert.NotNil(t, reader)
sz, e := reader.Read(buffer)
assert.Nil(t, e)
assert.Equal(t, 3, sz)
assert.Equal(t, "foo", string(buffer[:3]))
}

View File

@ -12,7 +12,7 @@
"required": {
"description": "version requirement",
"type": "string",
"pattern": "^([><~=][=]?[a-zA-Z0-9+.-_]+|)$"
"pattern": "^(([><~]*=){0,1}[-_a-zA-Z0-9+.]+|)$"
},
"version": {
"type": "string"