Skip to content

Commit

Permalink
feat: type name map
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwalt committed Mar 24, 2024
1 parent c5d62bb commit 83ffca3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions reflect/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package reflect

import "reflect"

func TypeName(v any) string {
return reflect.TypeOf(v).String()
}
49 changes: 49 additions & 0 deletions reflect/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package reflect_test

import (
"testing"

"github.com/hjwalt/runway/reflect"
"github.com/stretchr/testify/assert"
)

func TestTypeof(t *testing.T) {
assert := assert.New(t)

// Test with an integer
intType := reflect.TypeName(42)
assert.Equal("int", intType)

// Test with a string
stringType := reflect.TypeName("hello")
assert.Equal("string", stringType)

// Test with a custom struct
type Person struct {
Name string
Age int
}
personType := reflect.TypeName(Person{})
assert.Equal("reflect_test.Person", personType)

// Test with a pointer
pointerType := reflect.TypeName(&Person{})
assert.Equal("*reflect_test.Person", pointerType)

// Test with a pointer var
var personPointer *Person
personPointerType := reflect.TypeName(personPointer)
assert.Equal("*reflect_test.Person", personPointerType)

// Test with a slice
sliceType := reflect.TypeName([]int{})
assert.Equal("[]int", sliceType)

// Test with a map
mapType := reflect.TypeName(map[string]int{})
assert.Equal("map[string]int", mapType)

// Test with a function
funcType := reflect.TypeName(func() {})
assert.Equal("func()", funcType)
}

0 comments on commit 83ffca3

Please sign in to comment.