From a86d7d9915dbe5e96f10eea2c5668aefe95ba617 Mon Sep 17 00:00:00 2001 From: shockerli Date: Sat, 21 Sep 2024 16:57:31 +0800 Subject: [PATCH] Support `Typeof` to get the name of type --- cvte.go | 5 +++++ cvte_test.go | 6 ------ int_test.go | 7 +++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cvte.go b/cvte.go index 9bcb45f..a9bda2b 100644 --- a/cvte.go +++ b/cvte.go @@ -179,6 +179,11 @@ func FieldE(val interface{}, field interface{}) (interface{}, error) { return nil, fmt.Errorf("%w(%s)", errFieldNotFound, sf) } +// Typeof returns a string containing the name of the type of `val`. +func Typeof(val interface{}) string { + return fmt.Sprintf("%T", val) +} + // return the values of struct fields, and deep find the embedded fields func deepStructValues(rv reflect.Value) (sl []interface{}) { for j := 0; j < rv.NumField(); j++ { diff --git a/cvte_test.go b/cvte_test.go index 764f89a..f05ed3b 100644 --- a/cvte_test.go +++ b/cvte_test.go @@ -114,12 +114,6 @@ func (t TestTimeStringer) String() string { return t.time.String() } -func Benchmark(b *testing.B) { - for i := 0; i < b.N; i++ { - cvt.Bool(aliasTypeString0, true) - } -} - // [function tests] func TestField_HasDefault(t *testing.T) { diff --git a/int_test.go b/int_test.go index 7aed384..85f1332 100644 --- a/int_test.go +++ b/int_test.go @@ -2354,3 +2354,10 @@ func TestIntE(t *testing.T) { assertEqual(t, tt.expect, v, "[NonE] "+msg) } } + +func BenchmarkToUint(b *testing.B) { + values := []interface{}{120, int64(122), "123", "120.0", "120.", []byte("125."), true, false} + for n := 0; n < b.N; n++ { + _ = cvt.Uint(values[n%len(values)]) + } +}