Skip to content
Open
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
19 changes: 13 additions & 6 deletions tools/goctl/model/mongo/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,19 @@ func generateTypes(ctx *Context) error {
}

func generateError(ctx *Context) error {
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error)
if err != nil {
return err
}
for _, t := range ctx.Types {
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error)
if err != nil {
return err
}

Comment on lines +126 to 131
Copy link
Preview

Copilot AI Sep 1, 2025

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.

Suggested change
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.

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(),
Copy link
Collaborator

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?

}, output, false); err != nil {
return err
}
}

Comment on lines +126 to 139
Copy link
Preview

Copilot AI Sep 1, 2025

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.

Suggested change
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.

return util.With("error").Parse(text).GoFmt(true).SaveTo(ctx, output, false)
return nil
}
Loading