Skip to content

Map named types over basic kinds to scalar field helpers - #64

Open
dschreij wants to merge 2 commits into
go-gorm:masterfrom
dschreij:fix/named-scalar-types
Open

dschreij wants to merge 2 commits into
go-gorm:masterfrom
dschreij:fix/named-scalar-types

Conversation

@dschreij

@dschreij dschreij commented Sep 5, 2026

Copy link
Copy Markdown

Problem

A named type whose underlying type is a basic kind is one scalar column: an integer enum (protobuf enums, for one), a time.Duration, a string alias, a flag over bool. The generator emitted field.Struct[...] for such fields, so using them in Where or Set produced association SQL.

Fix

scalarHelperForNamedType maps a named type over a basic kind to the helper of its kind: field.Number[T] for integers and floats, field.String, field.Bool, and field.Bytes for a named byte slice such as json.RawMessage.

Test

TestNamedTypesOverBasicKindsGetScalarHelpers generates over a struct with an integer enum, a duration, a string alias, a bool alias and a json.RawMessage field, and asserts the helper of each.

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

Copilot AI lite review requested due to automatic review settings September 5, 2026 07:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

There are two concrete issues that can lead to failures (incorrect type qualification in generated code for some packages, and the new test helper not creating parent directories for nested file paths).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR updates the code generator’s field-type selection so that named types whose underlying type is a scalar (e.g., enums over integers, time.Duration, string/bool aliases, and named []byte) are treated as scalar columns rather than associations, preventing incorrect association SQL in Where/Set.

Changes:

  • Add scalarHelperForNamedType to map named scalar-like types to the appropriate scalar field helper.
  • Use the new helper during field type selection to avoid emitting field.Struct[...] for scalar named types.
  • Add a throwaway-module test harness plus a new test covering named scalar types (including json.RawMessage).
File summaries
File Description
internal/gen/utils.go Adds helper that maps named types over basic kinds / named byte slices to scalar field helpers.
internal/gen/generator.go Uses the helper when determining the generated field helper type.
internal/gen/generator_support_test.go Adds test helper to generate code from ad hoc sources in a temporary module.
internal/gen/named_scalar_types_test.go Adds a test ensuring named scalar-like types get scalar helpers (and not field.Struct[...]).
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread internal/gen/generator.go
Comment on lines +460 to +464
// A named type over a basic kind (an enum, a duration, a flag) is a scalar column and
// gets the helper of its kind instead of being mistaken for an association.
if helper, ok := scalarHelperForNamedType(typ, filepath.Base(goType)); ok {
return helper
}
Comment on lines +19 to +23
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)
}
}
@dschreij
dschreij force-pushed the fix/named-scalar-types branch from 9f5c48b to 83cf9c3 Compare September 5, 2026 15:38
@dschreij

dschreij commented Sep 5, 2026

Copy link
Copy Markdown
Author

Addressed the review. The shared test helper now creates parent directories before writing, so a nested key like models/model.go works as its doc comment promises.

On the package qualifier: you are right that filepath.Base of the full type mis-qualifies a package whose name differs from the last path element, /v2 modules being the clear case. That is not introduced here, though. Field.Type already used filepath.Base for both field.Field[...] and field.Struct[...] before this change, and this commit follows the same idiom for the scalar case. Fixing it means carrying the real package identifier from the import rather than deriving it from the path, which touches every branch of that function, so it seems better as its own change than folded in here.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The change is localized, aligns with the stated problem, and includes a regression test covering the key named-scalar scenarios.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@dschreij
dschreij force-pushed the fix/named-scalar-types branch from 83cf9c3 to f2828d5 Compare September 5, 2026 20:00
@dschreij

dschreij commented Sep 5, 2026

Copy link
Copy Markdown
Author

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.

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