-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/ren/72 custom name ref repository #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
483c438
9ac6d93
5d1cc6d
c32a6e7
219a36b
7195bdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,7 +23,16 @@ type Repository[M any] struct { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (r *Repository[M]) GetName() string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var model M | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return common.GetStructName(model) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctModel := reflect.ValueOf(&model).Elem() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fnc := ctModel.MethodByName("RepositoryName") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var name string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if fnc.IsValid() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name = fnc.Call(nil)[0].String() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name = common.GetStructName(model) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Support pointer receivers and remove panic risks by replacing reflection with a simple interface check Current code misses methods with pointer receivers and can panic if the method’s return type isn’t string. Prefer a type assertion that handles both value and pointer receivers and falls back cleanly. Apply this diff: func (r *Repository[M]) GetName() string {
var model M
-
- ctModel := reflect.ValueOf(&model).Elem()
- fnc := ctModel.MethodByName("RepositoryName")
- var name string
- if fnc.IsValid() {
- name = fnc.Call(nil)[0].String()
- } else {
- name = common.GetStructName(model)
- }
- return name
+ // Prefer interface assertion over reflection; supports both value and pointer receivers.
+ type repositoryNamer interface{ RepositoryName() string }
+ if rn, ok := any(model).(repositoryNamer); ok {
+ if n := rn.RepositoryName(); n != "" {
+ return n
+ }
+ }
+ if rn, ok := any(&model).(repositoryNamer); ok {
+ if n := rn.RepositoryName(); n != "" {
+ return n
+ }
+ }
+ return common.GetStructName(model)
}If you prefer to keep reflection, minimally switch to pointer first and validate signature: - ctModel := reflect.ValueOf(&model).Elem()
- fnc := ctModel.MethodByName("RepositoryName")
+ ptr := reflect.ValueOf(&model)
+ fnc := ptr.MethodByName("RepositoryName")
+ if !fnc.IsValid() {
+ fnc = ptr.Elem().MethodByName("RepositoryName")
+ }
- if fnc.IsValid() {
- name = fnc.Call(nil)[0].String()
+ if fnc.IsValid() && fnc.Type().NumIn() == 0 && fnc.Type().NumOut() >= 1 && fnc.Type().Out(0).Kind() == reflect.String {
+ out := fnc.Call(nil)
+ if len(out) > 0 {
+ s := out[0].String()
+ if s != "" {
+ return s
+ }
+ }
} else {
- name = common.GetStructName(model)
+ return common.GetStructName(model)
}
- return name
+ return common.GetStructName(model)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (r *Repository[M]) SetDB(db *gorm.DB) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Support pointer receivers and make reflection safe to avoid panics.
RepositoryName implemented with a pointer receiver (e.g., func (T* ) RepositoryName() string)) won’t be discovered, and fnc.Call(nil)[0] can panic if the signature differs. Handle both value/pointer receivers and validate the method signature before calling.
Apply:
🤖 Prompt for AI Agents