Skip to content

Commit

Permalink
👔 up: arrutil - update the ToString and add new func TString()
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 14, 2023
1 parent 73b905b commit 06b9b37
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
15 changes: 10 additions & 5 deletions arrutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ func ToStrings(arr any) (ret []string, err error) {
return
}

// SliceToStrings convert []any to []string
// SliceToStrings safe convert []any to []string
func SliceToStrings(arr []any) []string {
return QuietStrings(arr)
}

// QuietStrings convert []any to []string
// QuietStrings safe convert []any to []string
func QuietStrings(arr []any) []string {
ss := make([]string, len(arr))
for i, v := range arr {
ss[i] = strutil.QuietString(v)
ss[i] = strutil.SafeString(v)
}
return ss
}
Expand Down Expand Up @@ -186,7 +186,12 @@ func AnyToString(arr any) string {
func SliceToString(arr ...any) string { return ToString(arr) }

// ToString simple and quickly convert []any to string
func ToString[T any](arr []T) string {
func ToString(arr []any) string {
return TString[any](arr)
}

// TString simple and quickly convert []T to string
func TString[T any](arr []T) string {
// like fmt.Println([]any(nil))
if arr == nil {
return "[]"
Expand All @@ -199,7 +204,7 @@ func ToString[T any](arr []T) string {
if i > 0 {
sb.WriteByte(',')
}
sb.WriteString(strutil.QuietString(v))
sb.WriteString(strutil.SafeString(v))
}

sb.WriteByte(']')
Expand Down
8 changes: 6 additions & 2 deletions arrutil/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ func TestSliceToString(t *testing.T) {
is := assert.New(t)

is.Eq("[]", arrutil.SliceToString(nil))
is.Eq("[]", arrutil.ToString[any](nil))
is.Eq("[a,b]", arrutil.ToString([]string{"a", "b"}))
is.Eq("[]", arrutil.TString[any](nil))
is.Eq("[a,b]", arrutil.TString([]string{"a", "b"}))

is.Eq("[]", arrutil.ToString(nil))
is.Eq("[a,b]", arrutil.ToString([]any{"a", "b"}))

is.Eq("[a,b]", arrutil.SliceToString("a", "b"))
}

Expand Down
4 changes: 2 additions & 2 deletions arrutil/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Ints[T comdef.Integer] []T

// String to string
func (is Ints[T]) String() string {
return ToString(is)
return TString(is)
}

// Has given element
Expand Down Expand Up @@ -151,7 +151,7 @@ func (ls SortedList[T]) IsEmpty() bool {

// String to string
func (ls SortedList[T]) String() string {
return ToString(ls)
return TString(ls)
}

// Has given element
Expand Down
25 changes: 25 additions & 0 deletions testutil/fakeobj/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@ import (
"github.com/gookit/goutil/byteutil"
)

// Reader implements the io.Reader
type Reader struct {
byteutil.Buffer
// ErrOnRead return error on read, useful for testing
ErrOnRead bool
}

// Read implements the io.Reader
func (r *Reader) Read(p []byte) (n int, err error) {
if r.ErrOnRead {
return 0, errors.New("fake read error")
}
return r.Buffer.Read(p)
}

// SetErrOnRead mark
func (r *Reader) SetErrOnRead() {
r.ErrOnRead = true
}

// NewReader instance
func NewReader() *Reader {
return &Reader{}
}

// Writer implements the io.Writer, stdio.Flusher, io.Closer.
type Writer struct {
byteutil.Buffer
Expand Down
15 changes: 15 additions & 0 deletions testutil/fakeobj/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import (
"github.com/gookit/goutil/testutil/fakeobj"
)

func TestNewReader(t *testing.T) {
tr := fakeobj.NewReader()
tr.Write([]byte("hello"))

buf := make([]byte, 5)
n, err := tr.Read(buf)
assert.NoErr(t, err)
assert.Eq(t, 5, n)
assert.Eq(t, "hello", string(buf))

tr.SetErrOnRead()
_, err = tr.Read(buf)
assert.Err(t, err)
}

func TestNewWriter(t *testing.T) {
tw := fakeobj.NewBuffer()
_, err := tw.Write([]byte("hello"))
Expand Down

0 comments on commit 06b9b37

Please sign in to comment.