Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions fxgrpcserver/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,39 @@ import (
"reflect"
)

// GetType returns the type of a target.
// fullTypeID builds a stable identifier for a type in the form "<pkgpath>.<TypeName>".
func fullTypeID(t reflect.Type) string {
if t == nil {
return ""
}

// Unwrap pointers to get the underlying named type (if any).
for t.Kind() == reflect.Pointer {
t = t.Elem()
}

// For named types, PkgPath() + Name() gives a unique and stable identity.
if t.Name() != "" && t.PkgPath() != "" {
return t.PkgPath() + "." + t.Name()
}

// Fallback for non-named kinds (slices, maps, func, etc.).
return t.String()
}

// GetType returns a stable identifier for the given target’s type.
func GetType(target any) string {
return reflect.TypeOf(target).String()
return fullTypeID(reflect.TypeOf(target))
}

// GetReturnType returns the return type of a target.
// GetReturnType returns a stable identifier for the return type of constructor-like target.
// If a target is a function, we examine its first return value (index 0), unwrap pointers, and
// build an identifier for that named type. For non-function or empty-return cases, we return "".
func GetReturnType(target any) string {
return reflect.TypeOf(target).Out(0).String()
t := reflect.TypeOf(target)
if t == nil || t.Kind() != reflect.Func || t.NumOut() == 0 {
return ""
}

return fullTypeID(t.Out(0))
}
12 changes: 7 additions & 5 deletions fxgrpcserver/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fxgrpcserver_test
import (
"testing"

"github.com/ankorstore/yokai/fxhealthcheck"
"github.com/ankorstore/yokai/fxgrpcserver"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wrong dependency

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch 👌

"github.com/ankorstore/yokai/fxhealthcheck/testdata/probes"
"github.com/stretchr/testify/assert"
)
Expand All @@ -15,10 +15,11 @@ func TestGetType(t *testing.T) {
target any
expected string
}{
{nil, ""},
{123, "int"},
{"test", "string"},
{probes.NewSuccessProbe(), "*probes.SuccessProbe"},
{probes.NewFailureProbe(), "*probes.FailureProbe"},
{probes.NewSuccessProbe(), "github.com/ankorstore/yokai/fxhealthcheck/testdata/probes.SuccessProbe"},
{probes.NewFailureProbe(), "github.com/ankorstore/yokai/fxhealthcheck/testdata/probes.FailureProbe"},
}

for _, tt := range tests {
Expand All @@ -27,7 +28,7 @@ func TestGetType(t *testing.T) {
t.Run(tt.expected, func(t *testing.T) {
t.Parallel()

got := fxhealthcheck.GetType(tt.target)
got := fxgrpcserver.GetType(tt.target)
assert.Equal(t, tt.expected, got)
})
}
Expand All @@ -40,6 +41,7 @@ func TestGetReturnType(t *testing.T) {
target any
expected string
}{
{nil, ""},
{func() string { return "test" }, "string"},
{func() int { return 123 }, "int"},
}
Expand All @@ -49,7 +51,7 @@ func TestGetReturnType(t *testing.T) {
t.Run(tt.expected, func(t *testing.T) {
t.Parallel()

got := fxhealthcheck.GetReturnType(tt.target)
got := fxgrpcserver.GetReturnType(tt.target)
assert.Equal(t, tt.expected, got)
})
}
Expand Down
Loading