Skip to content

fix: correct LastInsertId and Url casing in generated code#15

Closed
kalbasit wants to merge 2 commits intomainfrom
fix-correct-lastinsertid-and-url
Closed

fix: correct LastInsertId and Url casing in generated code#15
kalbasit wants to merge 2 commits intomainfrom
fix-correct-lastinsertid-and-url

Conversation

@kalbasit
Copy link
Copy Markdown
Owner

This fixes two bugs in FixAcronyms:

  1. LastInsertId: The regex was incorrectly transforming res.LastInsertId() to
    res.LastInsertID(). The Go standard library's sql.Result interface defines
    LastInsertId() (lowercase d), so the generated MySQL wrappers failed to compile.

  2. Url casing: With ExtraRules: true, gofumpt was uppercasing field names like
    NewUrl to NewURL, but the underlying struct types generated by sqlc still
    use NewUrl, causing type mismatches.

The fix disables ExtraRules in gofumpt and modifies the Id regex to exclude
'(' from the character class, preventing method calls like LastInsertId()
from being transformed.

This fixes two bugs in FixAcronyms:

1. LastInsertId: The regex was incorrectly transforming res.LastInsertId() to
   res.LastInsertID(). The Go standard library's sql.Result interface defines
   LastInsertId() (lowercase d), so the generated MySQL wrappers failed to compile.

2. Url casing: With ExtraRules: true, gofumpt was uppercasing field names like
   NewUrl to NewURL, but the underlying struct types generated by sqlc still
   use NewUrl, causing type mismatches.

The fix disables ExtraRules in gofumpt and modifies the Id regex to exclude
'(' from the character class, preventing method calls like LastInsertId()
from being transformed.
@kalbasit
Copy link
Copy Markdown
Owner Author

kalbasit commented Mar 23, 2026

This change is part of the following stack:

Change managed by git-spice.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 32.83%. Comparing base (291807a) to head (46c4ca1).

Files with missing lines Patch % Lines
generator/helpers.go 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #15      +/-   ##
==========================================
+ Coverage   32.28%   32.83%   +0.54%     
==========================================
  Files           6        6              
  Lines         734      740       +6     
==========================================
+ Hits          237      243       +6     
  Misses        486      486              
  Partials       11       11              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses two specific issues within the code generation process: incorrect casing of LastInsertId in generated MySQL wrappers and Url casing discrepancies caused by gofumpt's ExtraRules. The changes ensure proper compilation and type consistency by refining the acronym fixing logic and adjusting code formatting settings.

Highlights

  • Corrected LastInsertId casing: The FixAcronyms function incorrectly transformed res.LastInsertId() to res.LastInsertID(), causing compilation failures in generated MySQL wrappers. The fix modifies the regex to prevent this transformation for method calls.
  • Resolved Url casing issues: gofumpt with ExtraRules: true was uppercasing field names like NewUrl to NewURL, leading to type mismatches with sqlc-generated struct types. Disabling ExtraRules in gofumpt resolves this.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an issue where the acronym fixer was incorrectly transforming method calls like LastInsertId() to LastInsertID(). The fix involves modifying the regex logic in generator/helpers.go to exclude the opening parenthesis when matching the "Id" acronym, preventing method calls from being altered. New test cases have been added in generator/helpers_test.go to validate this behavior. Additionally, the ExtraRules for gofumpt formatting were disabled. A review comment suggests a performance improvement for the FixAcronyms function by pre-compiling regular expressions outside the loop.

Comment on lines +101 to +107
// For Id, exclude '(' to preserve method calls like res.LastInsertId()
nonLetterClass := `[^A-Za-z]`
if a.pattern == "Id" {
nonLetterClass = `[^A-Za-z(]`
}

regexNonLetter := regexp.MustCompile(`([a-z])(` + a.pattern + `)(` + nonLetterClass + `)`)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this logic correctly fixes the issue, compiling regular expressions inside this for loop is inefficient. The compilation happens on every call to FixAcronyms for every acronym, which can be a performance bottleneck.

To improve this, consider pre-compiling all the regular expressions once at package initialization using an init() function. You could store them in a global variable (e.g., a slice of structs, where each struct holds the compiled regexes for an acronym). This ensures the expensive compilation step is performed only once when the package is loaded.

@kalbasit
Copy link
Copy Markdown
Owner Author

this is just too messy and error prone, I decided to drop this feature...

@kalbasit kalbasit closed this Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants