-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(goctl): update mongo generate error #5123
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
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Pull Request Overview
This PR updates the generateError
function in the MongoDB model generator to iterate over all types in the context and generate error templates for each type individually, rather than using the entire context as template data.
- Changes the error generation to loop through each type in
ctx.Types
- Updates template data to use individual type names with title casing
- Replaces direct context usage with type-specific template variables
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
for _, t := range ctx.Types { | ||
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error) | ||
if err != nil { | ||
return err | ||
} | ||
|
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.
The template is being loaded inside the loop for each type, which is inefficient. Consider loading the template once before the loop to avoid repeated I/O operations.
for _, t := range ctx.Types { | |
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error) | |
if err != nil { | |
return err | |
} | |
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error) | |
if err != nil { | |
return err | |
} | |
for _, t := range ctx.Types { |
Copilot uses AI. Check for mistakes.
for _, t := range ctx.Types { | ||
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
output := filepath.Join(ctx.Output, "error.go") | ||
output := filepath.Join(ctx.Output, "error.go") | ||
if err = util.With("error").Parse(text).GoFmt(true).SaveTo(map[string]any{ | ||
"Type": stringx.From(t).Title(), | ||
}, output, false); err != nil { | ||
return err | ||
} | ||
} | ||
|
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.
The same output file 'error.go' is being overwritten in each iteration of the loop. This means only the last type's error template will be preserved, and previous types' error definitions will be lost.
for _, t := range ctx.Types { | |
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error) | |
if err != nil { | |
return err | |
} | |
output := filepath.Join(ctx.Output, "error.go") | |
output := filepath.Join(ctx.Output, "error.go") | |
if err = util.With("error").Parse(text).GoFmt(true).SaveTo(map[string]any{ | |
"Type": stringx.From(t).Title(), | |
}, output, false); err != nil { | |
return err | |
} | |
} | |
var errorDefs []string | |
for _, t := range ctx.Types { | |
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error) | |
if err != nil { | |
return err | |
} | |
rendered, err := util.With("error").Parse(text).GoFmt(true).Build(map[string]any{ | |
"Type": stringx.From(t).Title(), | |
}) | |
if err != nil { | |
return err | |
} | |
errorDefs = append(errorDefs, rendered) | |
} | |
output := filepath.Join(ctx.Output, "error.go") | |
combined := "" | |
for _, def := range errorDefs { | |
combined += def + "\n" | |
} | |
if err := pathx.WriteFile(output, combined, false); err != nil { | |
return err | |
} |
Copilot uses AI. Check for mistakes.
output := filepath.Join(ctx.Output, "error.go") | ||
output := filepath.Join(ctx.Output, "error.go") | ||
if err = util.With("error").Parse(text).GoFmt(true).SaveTo(map[string]any{ | ||
"Type": stringx.From(t).Title(), |
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.
Why do you want to inject a Type
variable into the template? I don't see that the template uses this variable. What problem are you trying to solve?
I want to customize the template. The default package is model. I want to add more build-in functions and a new type variable to customize the template package. |
e.g. package {{.Type | lower}}
import (
"errors"
"github.com/zeromicro/go-zero/core/stores/mon"
)
var (
ErrNotFound = mon.ErrNotFound
ErrInvalidObjectId = errors.New("invalid objectId")
) |
No description provided.