Bump github.com/gookit/config/v2 from 2.2.3 to 2.2.4
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.3 to 2.2.4. - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.3...v2.2.4) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
committed by
Ralf Haferkamp
parent
30784affc4
commit
0df009eae0
+7
@@ -6,11 +6,13 @@ import (
|
||||
)
|
||||
|
||||
// Buffer wrap and extends the bytes.Buffer, add some useful methods
|
||||
// and implements the io.Writer, io.Closer and stdio.Flusher interfaces
|
||||
type Buffer struct {
|
||||
bytes.Buffer
|
||||
// custom error for testing
|
||||
CloseErr error
|
||||
FlushErr error
|
||||
SyncErr error
|
||||
}
|
||||
|
||||
// NewBuffer instance
|
||||
@@ -117,3 +119,8 @@ func (b *Buffer) Close() error {
|
||||
func (b *Buffer) Flush() error {
|
||||
return b.FlushErr
|
||||
}
|
||||
|
||||
// Sync anf flush buffer
|
||||
func (b *Buffer) Sync() error {
|
||||
return b.SyncErr
|
||||
}
|
||||
|
||||
+39
-32
@@ -1,14 +1,40 @@
|
||||
// Package byteutil provides some useful functions for byte slice.
|
||||
package byteutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Md5 Generate a 32-bit md5 bytes
|
||||
func Md5(src any) []byte {
|
||||
h := md5.New()
|
||||
|
||||
switch val := src.(type) {
|
||||
case []byte:
|
||||
h.Write(val)
|
||||
case string:
|
||||
h.Write([]byte(val))
|
||||
default:
|
||||
h.Write([]byte(fmt.Sprint(src)))
|
||||
}
|
||||
|
||||
bs := h.Sum(nil) // cap(bs) == 16
|
||||
dst := make([]byte, hex.EncodedLen(len(bs)))
|
||||
hex.Encode(dst, bs)
|
||||
return dst
|
||||
}
|
||||
|
||||
// ShortMd5 Generate a 16-bit md5 bytes. remove first 8 and last 8 bytes from 32-bit md5.
|
||||
func ShortMd5(src any) []byte {
|
||||
return Md5(src)[8:24]
|
||||
}
|
||||
|
||||
// Random bytes generate
|
||||
func Random(length int) ([]byte, error) {
|
||||
b := make([]byte, length)
|
||||
@@ -27,32 +53,6 @@ func FirstLine(bs []byte) []byte {
|
||||
return bs
|
||||
}
|
||||
|
||||
// StrOrErr convert to string, return empty string on error.
|
||||
func StrOrErr(bs []byte, err error) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(bs), err
|
||||
}
|
||||
|
||||
// SafeString convert to string, return empty string on error.
|
||||
func SafeString(bs []byte, err error) string {
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(bs)
|
||||
}
|
||||
|
||||
// String unsafe convert bytes to string
|
||||
func String(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
// ToString convert bytes to string
|
||||
func ToString(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
// AppendAny append any value to byte slice
|
||||
func AppendAny(dst []byte, v any) []byte {
|
||||
if v == nil {
|
||||
@@ -104,12 +104,19 @@ func AppendAny(dst []byte, v any) []byte {
|
||||
return dst
|
||||
}
|
||||
|
||||
// Cut bytes. like the strings.Cut()
|
||||
// Cut bytes by one byte char. like bytes.Cut(), but sep is byte.
|
||||
func Cut(bs []byte, sep byte) (before, after []byte, found bool) {
|
||||
if i := bytes.IndexByte(bs, sep); i >= 0 {
|
||||
return bs[:i], bs[i+1:], true
|
||||
}
|
||||
return bytes.Cut(bs, []byte{sep})
|
||||
}
|
||||
|
||||
before = bs
|
||||
// SafeCut bytes by one byte char. always return before and after
|
||||
func SafeCut(bs []byte, sep byte) (before, after []byte) {
|
||||
before, after, _ = bytes.Cut(bs, []byte{sep})
|
||||
return
|
||||
}
|
||||
|
||||
// SafeCuts bytes by sub bytes. like the bytes.Cut(), but always return before and after
|
||||
func SafeCuts(bs []byte, sep []byte) (before, after []byte) {
|
||||
before, after, _ = bytes.Cut(bs, sep)
|
||||
return
|
||||
}
|
||||
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
// Package byteutil Provide some bytes utils functions or structs
|
||||
package byteutil
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Md5 Generate a 32-bit md5 bytes
|
||||
func Md5(src any) []byte {
|
||||
h := md5.New()
|
||||
|
||||
if s, ok := src.(string); ok {
|
||||
h.Write([]byte(s))
|
||||
} else {
|
||||
h.Write([]byte(fmt.Sprint(src)))
|
||||
}
|
||||
return h.Sum(nil)
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package byteutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/gookit/goutil/comdef"
|
||||
)
|
||||
|
||||
// StrOrErr convert to string, return empty string on error.
|
||||
func StrOrErr(bs []byte, err error) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(bs), err
|
||||
}
|
||||
|
||||
// SafeString convert to string, return empty string on error.
|
||||
func SafeString(bs []byte, err error) string {
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(bs)
|
||||
}
|
||||
|
||||
// String unsafe convert bytes to string
|
||||
func String(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
// ToString convert bytes to string
|
||||
func ToString(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
// ToBytes convert any value to []byte. return error on convert failed.
|
||||
func ToBytes(v any) ([]byte, error) {
|
||||
return ToBytesWithFunc(v, nil)
|
||||
}
|
||||
|
||||
// SafeBytes convert any value to []byte. use fmt.Sprint() on convert failed.
|
||||
func SafeBytes(v any) []byte {
|
||||
bs, _ := ToBytesWithFunc(v, func(v any) ([]byte, error) {
|
||||
return []byte(fmt.Sprint(v)), nil
|
||||
})
|
||||
return bs
|
||||
}
|
||||
|
||||
// ToBytesFunc convert any value to []byte
|
||||
type ToBytesFunc = func(v any) ([]byte, error)
|
||||
|
||||
// ToBytesWithFunc convert any value to []byte with custom fallback func.
|
||||
//
|
||||
// refer the strutil.ToStringWithFunc
|
||||
//
|
||||
// On not convert:
|
||||
// - If usrFn is nil, will return comdef.ErrConvType.
|
||||
// - If usrFn is not nil, will call it to convert.
|
||||
func ToBytesWithFunc(v any, usrFn ToBytesFunc) ([]byte, error) {
|
||||
if v == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
switch val := v.(type) {
|
||||
case []byte:
|
||||
return val, nil
|
||||
case string:
|
||||
return []byte(val), nil
|
||||
case int:
|
||||
return []byte(strconv.Itoa(val)), nil
|
||||
case int8:
|
||||
return []byte(strconv.Itoa(int(val))), nil
|
||||
case int16:
|
||||
return []byte(strconv.Itoa(int(val))), nil
|
||||
case int32: // same as `rune`
|
||||
return []byte(strconv.Itoa(int(val))), nil
|
||||
case int64:
|
||||
return []byte(strconv.FormatInt(val, 10)), nil
|
||||
case uint:
|
||||
return []byte(strconv.FormatUint(uint64(val), 10)), nil
|
||||
case uint8:
|
||||
return []byte(strconv.FormatUint(uint64(val), 10)), nil
|
||||
case uint16:
|
||||
return []byte(strconv.FormatUint(uint64(val), 10)), nil
|
||||
case uint32:
|
||||
return []byte(strconv.FormatUint(uint64(val), 10)), nil
|
||||
case uint64:
|
||||
return []byte(strconv.FormatUint(val, 10)), nil
|
||||
case float32:
|
||||
return []byte(strconv.FormatFloat(float64(val), 'f', -1, 32)), nil
|
||||
case float64:
|
||||
return []byte(strconv.FormatFloat(val, 'f', -1, 64)), nil
|
||||
case bool:
|
||||
return []byte(strconv.FormatBool(val)), nil
|
||||
case time.Duration:
|
||||
return []byte(strconv.FormatInt(int64(val), 10)), nil
|
||||
case fmt.Stringer:
|
||||
return []byte(val.String()), nil
|
||||
case error:
|
||||
return []byte(val.Error()), nil
|
||||
default:
|
||||
if usrFn == nil {
|
||||
return nil, comdef.ErrConvType
|
||||
}
|
||||
return usrFn(val)
|
||||
}
|
||||
}
|
||||
+9
-3
@@ -5,6 +5,12 @@ import (
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// BytesEncodeFunc type
|
||||
type BytesEncodeFunc func(src []byte) []byte
|
||||
|
||||
// BytesDecodeFunc type
|
||||
type BytesDecodeFunc func(src []byte) ([]byte, error)
|
||||
|
||||
// BytesEncoder interface
|
||||
type BytesEncoder interface {
|
||||
Encode(src []byte) []byte
|
||||
@@ -13,12 +19,12 @@ type BytesEncoder interface {
|
||||
|
||||
// StdEncoder implement the BytesEncoder
|
||||
type StdEncoder struct {
|
||||
encodeFn func(src []byte) []byte
|
||||
decodeFn func(src []byte) ([]byte, error)
|
||||
encodeFn BytesEncodeFunc
|
||||
decodeFn BytesDecodeFunc
|
||||
}
|
||||
|
||||
// NewStdEncoder instance
|
||||
func NewStdEncoder(encFn func(src []byte) []byte, decFn func(src []byte) ([]byte, error)) *StdEncoder {
|
||||
func NewStdEncoder(encFn BytesEncodeFunc, decFn BytesDecodeFunc) *StdEncoder {
|
||||
return &StdEncoder{
|
||||
encodeFn: encFn,
|
||||
decodeFn: decFn,
|
||||
|
||||
+5
-6
@@ -13,14 +13,14 @@ package byteutil
|
||||
// from https://github.com/minio/minio/blob/master/internal/bpool/bpool.go
|
||||
type ChanPool struct {
|
||||
c chan []byte
|
||||
w int
|
||||
wcap int
|
||||
w int // init byte width
|
||||
wcap int // set byte cap
|
||||
}
|
||||
|
||||
// NewChanPool instance
|
||||
func NewChanPool(maxSize int, width int, capWidth int) *ChanPool {
|
||||
func NewChanPool(chSize int, width int, capWidth int) *ChanPool {
|
||||
return &ChanPool{
|
||||
c: make(chan []byte, maxSize),
|
||||
c: make(chan []byte, chSize),
|
||||
w: width,
|
||||
wcap: capWidth,
|
||||
}
|
||||
@@ -30,8 +30,7 @@ func NewChanPool(maxSize int, width int, capWidth int) *ChanPool {
|
||||
// available in the pool.
|
||||
func (bp *ChanPool) Get() (b []byte) {
|
||||
select {
|
||||
case b = <-bp.c:
|
||||
// reuse existing buffer
|
||||
case b = <-bp.c: // reuse existing buffer
|
||||
default:
|
||||
// create new buffer
|
||||
if bp.wcap > 0 {
|
||||
|
||||
Reference in New Issue
Block a user