Package IOUtils/BufferReadCloser:

- refactor package to allow closer for bytes.Buffer, bufio.Reader, bufio.Writer, bufio.ReadWriter
- implement ReadFrom, WriterTo, StringWriter, ReadRune, ReadByte, WriteByte
- allow specify a custom close function, called in close statement
- add NewBuffer (New declare as deprecated), NewReader, NewWriter, NewReadWriter to each types bytes / bufio
- remove call of Reset in close for ReadWriter: ambigous function (ambigous in bufio struct)

Package Static:
- replace BufferReadCloser New function to NewBuffer

Other:
- bump dependencies
This commit is contained in:
Nicolas JUHEL
2025-02-10 14:03:08 +01:00
parent fe90c8e82c
commit 0c1f4675fb
7 changed files with 310 additions and 41 deletions

64
go.mod
View File

@@ -2,15 +2,15 @@ module github.com/nabbar/golib
go 1.23
toolchain go1.23.3
toolchain go1.23.5
require (
github.com/aws/aws-sdk-go v1.55.6
github.com/aws/aws-sdk-go-v2 v1.34.0
github.com/aws/aws-sdk-go-v2/config v1.29.2
github.com/aws/aws-sdk-go-v2/credentials v1.17.55
github.com/aws/aws-sdk-go-v2/service/iam v1.38.8
github.com/aws/aws-sdk-go-v2/service/s3 v1.74.1
github.com/aws/aws-sdk-go-v2 v1.36.1
github.com/aws/aws-sdk-go-v2/config v1.29.6
github.com/aws/aws-sdk-go-v2/credentials v1.17.59
github.com/aws/aws-sdk-go-v2/service/iam v1.39.1
github.com/aws/aws-sdk-go-v2/service/s3 v1.76.0
github.com/aws/smithy-go v1.22.2
github.com/bits-and-blooms/bitset v1.20.0
github.com/c-bata/go-prompt v0.2.6
@@ -33,7 +33,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/nats-io/jwt/v2 v2.7.3
github.com/nats-io/nats-server/v2 v2.10.25
github.com/nats-io/nats.go v1.38.0
github.com/nats-io/nats.go v1.39.0
github.com/onsi/ginkgo/v2 v2.22.2
github.com/onsi/gomega v1.36.2
github.com/pelletier/go-toml v1.9.5
@@ -47,14 +47,14 @@ require (
github.com/spf13/viper v1.19.0
github.com/ugorji/go/codec v1.2.12
github.com/ulikunitz/xz v0.5.12
github.com/vbauerster/mpb/v8 v8.9.1
github.com/vbauerster/mpb/v8 v8.9.2
github.com/xanzy/go-gitlab v0.115.0
github.com/xhit/go-simple-mail v2.2.2+incompatible
golang.org/x/net v0.34.0
golang.org/x/oauth2 v0.25.0
golang.org/x/sync v0.10.0
golang.org/x/sys v0.29.0
golang.org/x/term v0.28.0
golang.org/x/oauth2 v0.26.0
golang.org/x/sync v0.11.0
golang.org/x/sys v0.30.0
golang.org/x/term v0.29.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/clickhouse v0.6.1
gorm.io/driver/mysql v1.5.7
@@ -67,8 +67,8 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
github.com/ClickHouse/ch-go v0.63.1 // indirect
github.com/ClickHouse/clickhouse-go/v2 v2.30.1 // indirect
github.com/ClickHouse/ch-go v0.64.1 // indirect
github.com/ClickHouse/clickhouse-go/v2 v2.30.3 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
@@ -78,18 +78,18 @@ require (
github.com/andybalholm/brotli v1.1.1 // indirect
github.com/andybalholm/cascadia v1.3.3 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.8 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.29 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.29 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.28 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.32 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.32 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.2 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.29 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.32 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.5.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.10 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.10 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.24.12 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.11 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.33.10 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.5.6 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.13 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.13 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.24.15 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.14 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.33.14 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.12.8 // indirect
github.com/bytedance/sonic/loader v0.2.3 // indirect
@@ -163,23 +163,23 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/vanng822/css v1.0.1 // indirect
github.com/vanng822/go-premailer v1.22.0 // indirect
github.com/vanng822/go-premailer v1.23.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.13.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/exp v0.0.0-20250128144449-3edf0e91c1ae // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.9.0 // indirect
golang.org/x/arch v0.14.0 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/time v0.10.0 // indirect
golang.org/x/tools v0.29.0 // indirect
google.golang.org/protobuf v1.36.4 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

View File

@@ -0,0 +1,78 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package bufferReadCloser
import (
"bytes"
"io"
)
type buf struct {
b *bytes.Buffer
f FuncClose
}
func (b *buf) Read(p []byte) (n int, err error) {
return b.b.Read(p)
}
func (b *buf) ReadFrom(r io.Reader) (n int64, err error) {
return b.b.ReadFrom(r)
}
func (b *buf) ReadByte() (byte, error) {
return b.b.ReadByte()
}
func (b *buf) ReadRune() (r rune, size int, err error) {
return b.b.ReadRune()
}
func (b *buf) Write(p []byte) (n int, err error) {
return b.b.Write(p)
}
func (b *buf) WriteString(s string) (n int, err error) {
return b.b.WriteString(s)
}
func (b *buf) WriteTo(w io.Writer) (n int64, err error) {
return b.b.WriteTo(w)
}
func (b *buf) WriteByte(c byte) error {
return b.b.WriteByte(c)
}
func (b *buf) Close() error {
b.b.Reset()
if b.f != nil {
return b.f()
}
return nil
}

View File

@@ -26,18 +26,72 @@
package bufferReadCloser
import (
"bufio"
"bytes"
"io"
)
type FuncClose func() error
type Buffer interface {
io.Reader
io.ReaderFrom
io.ByteReader
io.RuneReader
io.Writer
io.WriterTo
io.ByteWriter
io.StringWriter
io.Closer
}
// @deprecated use NewBuffer instead of New
func New(b *bytes.Buffer) Buffer {
return NewBuffer(b, nil)
}
func NewBuffer(b *bytes.Buffer, fct FuncClose) Buffer {
return &buf{
b: b,
f: fct,
}
}
type Reader interface {
io.Reader
io.WriterTo
io.Closer
}
func NewReader(b *bufio.Reader, fct FuncClose) Reader {
return &rdr{
b: b,
f: fct,
}
}
type Writer interface {
io.Writer
io.StringWriter
io.ReaderFrom
io.Closer
}
func NewWriter(b *bufio.Writer, fct FuncClose) Writer {
return &wrt{
b: b,
f: fct,
}
}
type ReadWriter interface {
Reader
Writer
}
func NewReadWriter(b *bufio.ReadWriter, fct FuncClose) ReadWriter {
return &rwt{
b: b,
f: fct,
}
}

View File

@@ -25,21 +25,30 @@
package bufferReadCloser
import "bytes"
import (
"bufio"
"io"
)
type buf struct {
b *bytes.Buffer
type rdr struct {
b *bufio.Reader
f FuncClose
}
func (b *buf) Read(p []byte) (n int, err error) {
func (b *rdr) Read(p []byte) (n int, err error) {
return b.b.Read(p)
}
func (b *buf) Write(p []byte) (n int, err error) {
return b.b.Write(p)
func (b *rdr) WriteTo(w io.Writer) (n int64, err error) {
return b.b.WriteTo(w)
}
func (b *buf) Close() error {
b.b.Reset()
func (b *rdr) Close() error {
b.b.Reset(nil)
if b.f != nil {
return b.f()
}
return nil
}

View File

@@ -0,0 +1,69 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package bufferReadCloser
import (
"bufio"
"io"
)
type rwt struct {
b *bufio.ReadWriter
f FuncClose
}
func (b *rwt) Read(p []byte) (n int, err error) {
return b.b.Read(p)
}
func (b *rwt) WriteTo(w io.Writer) (n int64, err error) {
return b.b.WriteTo(w)
}
func (b *rwt) ReadFrom(r io.Reader) (n int64, err error) {
return b.b.ReadFrom(r)
}
func (b *rwt) Write(p []byte) (n int, err error) {
return b.b.Write(p)
}
func (b *rwt) WriteString(s string) (n int, err error) {
return b.b.WriteString(s)
}
func (b *rwt) Close() error {
_ = b.b.Flush()
// cannot call Reset => ambigous func can be Reader and Writer
// b.b.Reset(nil)
if b.f != nil {
return b.f()
}
return nil
}

View File

@@ -0,0 +1,59 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package bufferReadCloser
import (
"bufio"
"io"
)
type wrt struct {
b *bufio.Writer
f FuncClose
}
func (b *wrt) ReadFrom(r io.Reader) (n int64, err error) {
return b.b.ReadFrom(r)
}
func (b *wrt) Write(p []byte) (n int, err error) {
return b.b.Write(p)
}
func (b *wrt) WriteString(s string) (n int, err error) {
return b.b.WriteString(s)
}
func (b *wrt) Close() error {
_ = b.b.Flush()
b.b.Reset(nil)
if b.f != nil {
return b.f()
}
return nil
}

View File

@@ -142,7 +142,7 @@ func (s *staticHandler) _fileBuff(pathFile string) (io.ReadCloser, liberr.Error)
} else if err != nil {
return nil, ErrorFileOpen.Error(err)
} else {
return libbuf.New(bytes.NewBuffer(obj)), nil
return libbuf.NewBuffer(bytes.NewBuffer(obj), nil), nil
}
}