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
3 changes: 1 addition & 2 deletions fxcron/info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fxcron

import (
"reflect"
"time"

"github.com/go-co-op/gocron/v2"
Expand Down Expand Up @@ -94,5 +93,5 @@ func (i *FxCronModuleInfo) jobNextRun(job gocron.Job) string {
}

func (i *FxCronModuleInfo) jobType(job CronJob) string {
return reflect.ValueOf(job).Type().String()
return GetType(job)
}
2 changes: 1 addition & 1 deletion fxcron/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func TestModuleInfo(t *testing.T) {
"expression": `*/1 * * * * *`,
"last_run": time.Time{}.Format(time.RFC3339),
"next_run": startAt.Format(time.RFC3339),
"type": "*job.DummyCron",
"type": "github.com/ankorstore/yokai/fxcron/testdata/cron/job.DummyCron",
},
},
"unscheduled": map[string]interface{}{},
Expand Down
35 changes: 31 additions & 4 deletions fxcron/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))
}
2 changes: 1 addition & 1 deletion fxcron/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGetType(t *testing.T) {
}{
{123, "int"},
{"test", "string"},
{tracker.NewCronExecutionTracker(), "*tracker.CronExecutionTracker"},
{tracker.NewCronExecutionTracker(), "github.com/ankorstore/yokai/fxcron/testdata/cron/tracker.CronExecutionTracker"},
}

for _, tt := range tests {
Expand Down
Loading