Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The behavior change is small, well-targeted, and backed by a focused regression test covering the key tag variants.
Pull request overview
This PR updates the code generator to skip emitting field helpers for struct fields that GORM explicitly excludes from its schema via gorm:"-" or gorm:"-:all", preventing generated queries from referencing non-existent columns.
Changes:
- Add
isIgnoredByGORMto interpret GORM’s-tag variants usingschema.ParseTagSetting. - Skip ignored fields during struct processing so no helper is generated for them.
- Add test coverage to assert helpers are omitted for ignored fields and retained for
-:migration.
File summaries
| File | Description |
|---|---|
| internal/gen/utils.go | Adds isIgnoredByGORM to detect schema-ignored fields via GORM tag parsing. |
| internal/gen/generator.go | Skips helper generation for fields ignored by GORM (- / -:all). |
| internal/gen/ignored_fields_test.go | Adds regression test ensuring ignored fields don’t produce helpers. |
| internal/gen/generator_support_test.go | Adds a test helper to generate code from ad-hoc sources for focused generator tests. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| for name, content := range files { | ||
| if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { | ||
| t.Fatalf("write %s: %v", name, err) | ||
| } | ||
| } |
53b8bca to
5843cce
Compare
|
Addressed the review. The shared test helper now creates parent directories before writing, so a nested key like |
5843cce to
1de7a04
Compare
|
Addressed the second round. The shared test helper now reads the whole output tree rather than its top level, so a fixture that spans packages is not silently truncated. That was only reachable once the helper started accepting nested source paths, so thank you for following it through. |
Problem
A field tagged
gorm:"-"orgorm:"-:all"maps no column, yet the generator emits a field helper for it. The helper names a column that does not exist, and a query built with it fails at the database.Fix
isIgnoredByGORMparses the gorm tag withschema.ParseTagSettingand the generator skips fields whose-setting is-orall. A-:migrationtag only skips migrations and still maps a column, so that field keeps its helper.Test
TestIgnoredFieldsGetNoHelpersgenerates over a struct with a plain field, a-field, a-:allfield and a-:migrationfield, and asserts which of them appear in the output.The first commit adds a small test helper (
generateFromSources,containsField) that writes ad hoc sources into a throwaway module and runs the generator over them. The other pull requests from this fork share that commit; git merges the identical addition without conflict, so they can land in any order.🤖 Generated with Claude Code