add io filter
This commit is contained in:
parent
0a6d1a93ee
commit
e859ef136c
32
internal/iofilter/filter.go
Normal file
32
internal/iofilter/filter.go
Normal 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
|
||||||
|
}
|
22
internal/iofilter/filter_test.go
Normal file
22
internal/iofilter/filter_test.go
Normal 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]))
|
||||||
|
}
|
@ -12,7 +12,7 @@
|
|||||||
"required": {
|
"required": {
|
||||||
"description": "version requirement",
|
"description": "version requirement",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"pattern": "^([><~=][=]?[a-zA-Z0-9+.-_]+|)$"
|
"pattern": "^(([><~]*=){0,1}[-_a-zA-Z0-9+.]+|)$"
|
||||||
},
|
},
|
||||||
"version": {
|
"version": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
Loading…
Reference in New Issue
Block a user