Skip to content

Commit

Permalink
Merge pull request #62 from Fenny/master
Browse files Browse the repository at this point in the history
Improve ToString by 20%
  • Loading branch information
ReneWerner87 authored Feb 9, 2024
2 parents b5c8482 + 21ba716 commit 8e706d8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
12 changes: 9 additions & 3 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func ByteSize(bytes uint64) string {

// ToString Change arg to string
func ToString(arg any, timeFormat ...string) string {
var tmp = reflect.Indirect(reflect.ValueOf(arg)).Interface()
switch v := tmp.(type) {
switch v := arg.(type) {
case int:
return strconv.Itoa(v)
case int8:
Expand Down Expand Up @@ -134,6 +133,13 @@ func ToString(arg any, timeFormat ...string) string {
case fmt.Stringer:
return v.String()
default:
return ""
// Check if the type is a pointer by using reflection
rv := reflect.ValueOf(arg)
if rv.Kind() == reflect.Ptr && !rv.IsNil() {
// Dereference the pointer and recursively call ToString
return ToString(rv.Elem().Interface(), timeFormat...)
}
// For types not explicitly handled, use fmt.Sprint to generate a string representation
return fmt.Sprint(arg)
}
}
78 changes: 69 additions & 9 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package utils

import (
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -68,18 +70,76 @@ func Test_CopyString(t *testing.T) {

func Test_ToString(t *testing.T) {
t.Parallel()
res := ToString([]byte("Hello, World!"))
require.Equal(t, "Hello, World!", res)
res = ToString(true)
require.Equal(t, "true", res)
res = ToString(uint(100))
require.Equal(t, "100", res)

tests := []struct {
input interface{}
expected string
}{
{[]byte("Hello, World!"), "Hello, World!"},
{true, "true"},
{uint(100), "100"},
{int(42), "42"},
{int8(42), "42"},
{int16(42), "42"},
{int32(42), "42"},
{int64(42), "42"},
{uint8(100), "100"},
{uint16(100), "100"},
{uint32(100), "100"},
{uint64(100), "100"},
{"test string", "test string"},
{float32(3.14), "3.14"},
{float64(3.14159), "3.14159"},
{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC), "2000-01-01 12:34:56"},
{struct{ Name string }{"John"}, "{John}"},
}

for _, tc := range tests {
t.Run(reflect.TypeOf(tc.input).String(), func(t *testing.T) {
res := ToString(tc.input)
require.Equal(t, tc.expected, res)
})
}

// Testing pointer to int
intPtr := 42
testsPtr := []struct {
input interface{}
expected string
}{
{&intPtr, "42"},
}
for _, tc := range testsPtr {
t.Run("pointer to "+reflect.TypeOf(tc.input).Elem().String(), func(t *testing.T) {
res := ToString(tc.input)
require.Equal(t, tc.expected, res)
})
}
}

// go test -v -run=^$ -bench=ToString -benchmem -count=2
// go test -v -run=^$ -bench=ToString -benchmem -count=4
func Benchmark_ToString(b *testing.B) {
hello := []byte("Hello, World!")
values := []interface{}{
42,
int8(42),
int16(42),
int32(42),
int64(42),
uint(42),
uint8(42),
uint16(42),
uint32(42),
uint64(42),
"Hello, World!",
[]byte("Hello, World!"),
true,
float32(3.14),
float64(3.14),
time.Now(),
}
for n := 0; n < b.N; n++ {
ToString(hello)
for _, value := range values {
_ = ToString(value)
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/gofiber/utils/v2
go 1.19

require (
github.com/google/uuid v1.5.0
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.8.4
)

Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

1 comment on commit 8e706d8

@ReneWerner87
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 8e706d8 Previous: b5c8482 Ratio
Benchmark_ToString 572.8 ns/op 96 B/op 6 allocs/op 53.45 ns/op 40 B/op 2 allocs/op 10.72
Benchmark_ToString - ns/op 572.8 ns/op 53.45 ns/op 10.72
Benchmark_ToString - B/op 96 B/op 40 B/op 2.40
Benchmark_ToString - allocs/op 6 allocs/op 2 allocs/op 3

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.