Skip to content

Commit

Permalink
Support Typeof to get the name of type
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Sep 21, 2024
1 parent ba02d3f commit a86d7d9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cvte.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
6 changes: 0 additions & 6 deletions cvte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
}
}

0 comments on commit a86d7d9

Please sign in to comment.