Skip to content

Commit 8db4440

Browse files
authored
fix(fxcron): Fix resolution collision by using full import path in type IDs (#366)
1 parent 4977596 commit 8db4440

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

fxcron/info.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fxcron
22

33
import (
4-
"reflect"
54
"time"
65

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

9695
func (i *FxCronModuleInfo) jobType(job CronJob) string {
97-
return reflect.ValueOf(job).Type().String()
96+
return GetType(job)
9897
}

fxcron/module_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func TestModuleInfo(t *testing.T) {
360360
"expression": `*/1 * * * * *`,
361361
"last_run": time.Time{}.Format(time.RFC3339),
362362
"next_run": startAt.Format(time.RFC3339),
363-
"type": "*job.DummyCron",
363+
"type": "github.com/ankorstore/yokai/fxcron/testdata/cron/job.DummyCron",
364364
},
365365
},
366366
"unscheduled": map[string]interface{}{},

fxcron/reflect.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,39 @@ import (
44
"reflect"
55
)
66

7-
// GetType returns the type of a target.
7+
// fullTypeID builds a stable identifier for a type in the form "<pkgpath>.<TypeName>".
8+
func fullTypeID(t reflect.Type) string {
9+
if t == nil {
10+
return ""
11+
}
12+
13+
// Unwrap pointers to get the underlying named type (if any).
14+
for t.Kind() == reflect.Pointer {
15+
t = t.Elem()
16+
}
17+
18+
// For named types, PkgPath() + Name() gives a unique and stable identity.
19+
if t.Name() != "" && t.PkgPath() != "" {
20+
return t.PkgPath() + "." + t.Name()
21+
}
22+
23+
// Fallback for non-named kinds (slices, maps, func, etc.).
24+
return t.String()
25+
}
26+
27+
// GetType returns a stable identifier for the given target’s type.
828
func GetType(target any) string {
9-
return reflect.TypeOf(target).String()
29+
return fullTypeID(reflect.TypeOf(target))
1030
}
1131

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

fxcron/reflect_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestGetType(t *testing.T) {
1717
}{
1818
{123, "int"},
1919
{"test", "string"},
20-
{tracker.NewCronExecutionTracker(), "*tracker.CronExecutionTracker"},
20+
{tracker.NewCronExecutionTracker(), "github.com/ankorstore/yokai/fxcron/testdata/cron/tracker.CronExecutionTracker"},
2121
}
2222

2323
for _, tt := range tests {

0 commit comments

Comments
 (0)