-
-
Notifications
You must be signed in to change notification settings - Fork 341
feat: add WithAutoRegistry option for automatic model registration #1399
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
base: master
Are you sure you want to change the base?
Conversation
|
Add This PR introduces a fully-automated model-registry feature for GORM-Gen. By enabling the new Key Changes• 新增顶层配置方法 Affected Areas• This summary was automatically generated by @propel-code-bot |
auto_registry_test.go
Outdated
| // getExpectedModelName 根据表名获取期望的模型名 | ||
| func getExpectedModelName(tableName string) string { | ||
| switch tableName { | ||
| case "users": | ||
| return "User" | ||
| case "orders": | ||
| return "Order" | ||
| default: | ||
| // 简单的首字母大写 | ||
| return strings.Title(tableName) |
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.
[BestPractice]
Deprecated function usage detected: strings.Title is deprecated since Go 1.18. Use cases.Title from golang.org/x/text/cases package instead:
Suggested Change
| // getExpectedModelName 根据表名获取期望的模型名 | |
| func getExpectedModelName(tableName string) string { | |
| switch tableName { | |
| case "users": | |
| return "User" | |
| case "orders": | |
| return "Order" | |
| default: | |
| // 简单的首字母大写 | |
| return strings.Title(tableName) | |
| func getExpectedModelName(tableName string) string { | |
| switch tableName { | |
| case "users": | |
| return "User" | |
| case "orders": | |
| return "Order" | |
| default: | |
| // Convert first letter to uppercase | |
| if len(tableName) == 0 { | |
| return "" | |
| } | |
| return strings.ToUpper(tableName[:1]) + tableName[1:] | |
| } | |
| } |
⚡ Committable suggestion
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
Context for Agents
[**BestPractice**]
Deprecated function usage detected: `strings.Title` is deprecated since Go 1.18. Use `cases.Title` from golang.org/x/text/cases package instead:
<details>
<summary>Suggested Change</summary>
```suggestion
func getExpectedModelName(tableName string) string {
switch tableName {
case "users":
return "User"
case "orders":
return "Order"
default:
// Convert first letter to uppercase
if len(tableName) == 0 {
return ""
}
return strings.ToUpper(tableName[:1]) + tableName[1:]
}
}
```
⚡ **Committable suggestion**
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
</details>
File: auto_registry_test.go
Line: 168| for i, expected := range tt.expectedList { | ||
| if i >= len(cfg.RegistryTableList) || cfg.RegistryTableList[i] != expected { | ||
| t.Errorf("RegistryTableList[%d] = %s, want %s", | ||
| i, cfg.RegistryTableList[i], expected) | ||
| } |
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.
[BestPractice]
Potential slice bounds error: Accessing cfg.RegistryTableList[i] in the error message without verifying i < len(cfg.RegistryTableList) could panic if the slice is shorter than expected:
Suggested Change
| for i, expected := range tt.expectedList { | |
| if i >= len(cfg.RegistryTableList) || cfg.RegistryTableList[i] != expected { | |
| t.Errorf("RegistryTableList[%d] = %s, want %s", | |
| i, cfg.RegistryTableList[i], expected) | |
| } | |
| for i, expected := range tt.expectedList { | |
| if i >= len(cfg.RegistryTableList) { | |
| t.Errorf("RegistryTableList[%d] missing, want %s", i, expected) | |
| } else if cfg.RegistryTableList[i] != expected { | |
| t.Errorf("RegistryTableList[%d] = %s, want %s", | |
| i, cfg.RegistryTableList[i], expected) | |
| } | |
| } |
⚡ Committable suggestion
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
Context for Agents
[**BestPractice**]
Potential slice bounds error: Accessing `cfg.RegistryTableList[i]` in the error message without verifying `i < len(cfg.RegistryTableList)` could panic if the slice is shorter than expected:
<details>
<summary>Suggested Change</summary>
```suggestion
for i, expected := range tt.expectedList {
if i >= len(cfg.RegistryTableList) {
t.Errorf("RegistryTableList[%d] missing, want %s", i, expected)
} else if cfg.RegistryTableList[i] != expected {
t.Errorf("RegistryTableList[%d] = %s, want %s",
i, cfg.RegistryTableList[i], expected)
}
}
```
⚡ **Committable suggestion**
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
</details>
File: auto_registry_test.go
Line: 278There 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.
LGTM, ship it! 🚢
Why was this auto-approved?
AI analysis completed with no actionable comments or suggestions.
What did this pull request do?
This PR introduces the
WithAutoRegistryoption to automatically generate model registration code, eliminating the need for manual model registration boilerplate. The implementation includes:Core Features:
WithAutoRegistry()method for automatic registration of all generated modelsWithAutoRegistry(tables...)method for selective table registrationinit()functions in model files that automatically callRegisterModel()Registry System:
RegisterModel(model, name)- Register a model instance with its nameGetAllModels()- Retrieve all registered model instancesGetAllModelNames()- Retrieve all registered model namesGetModelCount()andClear()methods for cleaner APICode Generation Improvements:
Testing & Documentation:
TestAutoRegistryInitGeneration)User Case Description
Problem:
In large projects with many database tables, developers need to manually register each generated model for reflection-based operations, dynamic queries, or runtime model discovery. This creates repetitive boilerplate code and increases maintenance overhead.
Before (Manual Registration):
After (Automatic Registration):