Skip to content

Use any in generated code, apply go fix - #23

Merged
umputun merged 2 commits into
masterfrom
chore/modernize-any
Aug 19, 2026
Merged

Use any in generated code, apply go fix#23
umputun merged 2 commits into
masterfrom
chore/modernize-any

Conversation

@paskal

@paskal paskal commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Previously the generated Scan method took an interface{} parameter while the rest of the template already used any. After this change the template emits any throughout. go fix makes the same replacement in a test helper and rewrites a HasPrefix/TrimPrefix pair into strings.CutPrefix.

The committed generated files are regenerated. That also brings the examples up to date with the case-insensitive parsing added to the template earlier, and leaves the integration test data byte for byte what the generator produces, so running the tests no longer leaves a modified file behind.

One test asserted that the generated code contains value int, which only ever matched because of the interface{} in Scan. The enum in that test is uint8 based, so the assertion now names the field type it actually has.

What changes for existing users: generated code no longer compiles in a module that declares a language version below Go 1.18, and a consumer package that declares its own any identifier shadows the predeclared one the generated type assertions rely on. The template already emitted any in the YAML block, so the second case was reachable before this change.

@paskal
paskal requested a review from umputun as a code owner August 19, 2026 01:22
@umputun

umputun commented Aug 19, 2026

Copy link
Copy Markdown
Member

the go fix parts are fine, but I'd keep interface{} in Scan.

any is a universe-scope alias rather than a keyword, so a package can declare its own. This generator requires the enum type to be lowercase and private, which makes -type any valid input: the declared type any uint8 then shadows the alias at package scope, generated Scan(value any) takes the concrete enum type instead of the interface, its type assertions stop compiling, and it no longer satisfies sql.Scanner. An unrelated package-level any in the target package does the same.

remote, and the template already has this exposure through MarshalYAML() (any, error), so it isn't new. But Scan is on every -sql generation rather than just YAML users, and swapping the spelling buys nothing at runtime, so the trade doesn't seem worth it.

the CutPrefix change and the corrected uint8 assertion are good on their own. This also needs a rebase now, #24 rewrote generator.go.

@paskal
paskal force-pushed the chore/modernize-any branch 2 times, most recently from 0243b85 to 524ab38 Compare August 19, 2026 05:36
go fix rewrites a HasPrefix and TrimPrefix pair into strings.CutPrefix, and the
interface{} parameter of a test helper into any. The generated Scan keeps
interface{}: the enum type has to be lowercase, so a package generating for
type any shadows the alias and the generated assertions stop compiling.

The committed generated files are regenerated. That brings the examples up to
date with the case-insensitive parsing added to the template earlier, and
leaves the integration test data byte for byte what the generator produces, so
running the tests no longer leaves a modified file behind.

One test asserted that the generated code contains "value int", which only
ever matched the interface{} in Scan. The enum in that test is uint8 based, so
the assertion now names the field type it has.
@paskal
paskal force-pushed the chore/modernize-any branch from 524ab38 to 9c1d059 Compare August 19, 2026 06:59
The workflow already granted contents: read at the top level, the job did not
say anything of its own. Code scanning reports a job that does not limit the
token, so the job states the same permission explicitly.
@paskal

paskal commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

Reverted, Scan keeps interface{}. Your case reproduces: enum -type any -sql against type any uint8 compiles with interface{}, and with any the same file fails with invalid operation: value (variable of uint8 type any) is not an interface, plus the nil comparison. Left MarshalYAML alone since it is unrelated to this change.

Kept the CutPrefix rewrite and the uint8 assertion, and the test helper in integration_test.go still takes any, which is go fix on our own code rather than generated output.

Rebased on master. The committed generated files are regenerated, which brings the examples up to date with the case-insensitive parsing added to the template earlier and leaves the integration test data byte for byte what the generator writes, so a test run no longer leaves a modified file in the tree.

I also checked for dependency updates and there are none to take: both modules are on the newest versions that keep the go directive at 1.25.0. The one update on offer is modernc.org/libc 1.74.4 to 1.75.3, which I am holding back because modernc.org/sqlite v1.56.0 requires exactly 1.74.4 and the two are generated together.

Separately, the last commit gives the build job its own permissions: contents: read. The workflow already granted that at the top level, but code scanning reports the job for not limiting the token, and stating it on the job silences that without changing what the job can do.

@umputun
umputun merged commit 28cf9d3 into master Aug 19, 2026
4 checks passed
@umputun
umputun deleted the chore/modernize-any branch August 19, 2026 07:25
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