Skip to content

feat: add fallback field mapping for domain struct params#7

Merged
kalbasit merged 2 commits intomainfrom
feat-add-fallback-field-mapping
Mar 15, 2026
Merged

feat: add fallback field mapping for domain struct params#7
kalbasit merged 2 commits intomainfrom
feat-add-fallback-field-mapping

Conversation

@kalbasit
Copy link
Copy Markdown
Owner

This adds multiple fallback strategies to map source struct fields to target struct
fields when field names differ between the wrapper API and the database adapter.
The strategies include: exact match, case-insensitive match, snake_case match,
and position-based match when structs have the same field count.

This is needed because sqlc generates different field names for unnamed query
parameters (e.g., LIMIT ? generates 'Limit' instead of 'BatchSize'). The fix enables
the generator to correctly map all parameters regardless of naming differences.

@kalbasit
Copy link
Copy Markdown
Owner Author

kalbasit commented Mar 15, 2026

This change is part of the following stack:

Change managed by git-spice.

@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 significantly improves the sqlc-multi-db generator's ability to map struct fields between different layers of an application. It introduces sophisticated fallback strategies to handle discrepancies in field names that arise when sqlc generates code for various database adapters, such as the issue with MySQL's LIMIT parameter. This ensures that the generator can correctly map all parameters regardless of naming differences, enhancing the robustness and flexibility of the generated code.

Highlights

  • Enhanced Field Mapping Logic: Implemented multiple fallback strategies (exact, case-insensitive, snake_case, and position-based) to correctly map source struct fields to target struct fields even when names differ between the wrapper API and the database adapter.
  • New Helper Functions: Introduced findSourceField, fieldsCompatible, and normalizeType to support the advanced field mapping and type compatibility checks, ensuring robust type comparison and normalization.
  • Regression Test Added: A new test case, TestJoinParamsCallFieldMapping, was added to validate the improved field mapping, specifically addressing the MySQL LIMIT parameter naming discrepancy where sqlc generates different field names.
Changelog
  • generator/constants.go
    • Added typeInt and typeBytes constants.
  • generator/generator_test.go
    • Added TestJoinParamsCallFieldMapping to verify the new field mapping logic.
  • generator/helpers.go
    • Implemented findSourceField, fieldsCompatible, and normalizeType functions.
    • Updated joinDomainStructParam to utilize the new field mapping strategies and handle package prefixes for target structs.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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 introduces several fallback strategies for mapping struct fields between the domain and database layers, which is a valuable enhancement for handling inconsistencies in sqlc's generated code. The implementation adds name-based (exact, case-insensitive, snake_case) and position-based matching. I've found a potential issue in the mapping logic where a single source field could be mapped to multiple target fields, and I've provided a suggestion to ensure a one-to-one mapping.

kalbasit and others added 2 commits March 15, 2026 16:03
This adds multiple fallback strategies to map source struct fields to target struct
fields when field names differ between the wrapper API and the database adapter.
The strategies include: exact match, case-insensitive match, snake_case match,
and position-based match when structs have the same field count.

This is needed because sqlc generates different field names for unnamed query
parameters (e.g., LIMIT ? generates 'Limit' instead of 'BatchSize'). The fix enables
the generator to correctly map all parameters regardless of naming differences.
The mapping process now tracks available source fields to prevent one
source field from being mapped to multiple target fields. After a field
is matched, it's removed from the available pool.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 15, 2026

Codecov Report

❌ Patch coverage is 58.18182% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 29.21%. Comparing base (4b21a04) to head (8beb086).

Files with missing lines Patch % Lines
generator/helpers.go 58.18% 17 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main       #7      +/-   ##
==========================================
+ Coverage   26.43%   29.21%   +2.77%     
==========================================
  Files           6        6              
  Lines         643      688      +45     
==========================================
+ Hits          170      201      +31     
- Misses        468      476       +8     
- Partials        5       11       +6     

☔ 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.

@kalbasit kalbasit force-pushed the feat-add-fallback-field-mapping branch from fae6af3 to 8beb086 Compare March 15, 2026 23:08
@kalbasit kalbasit merged commit c725291 into main Mar 15, 2026
6 checks passed
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