Skip to content

fix: use a value receiver for AppendText so generated enums pass recvcheck - #325

Merged
abice merged 1 commit into
abice:masterfrom
david-long1:fix/appendtext-value-receiver
Sep 21, 2026
Merged

abice merged 1 commit into
abice:masterfrom
david-long1:fix/appendtext-value-receiver

Conversation

@david-long1

@david-long1 david-long1 commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

Every enum generated with --marshal trips golangci-lint's recvcheck linter:

color.go:4:6: the methods of "Color" use pointer receiver and non-pointer receiver. (recvcheck)

so consumers end up adding //nolint:recvcheck to every enum type declaration.

Cause: AppendText is emitted on a pointer receiver (func (x *Foo) AppendText) while its siblings String, IsValid, Ptr, MarshalText, and Value use value receivers. recvcheck tolerates UnmarshalText on a pointer receiver because that method is on the linter's built-in exclusion list (the standard Unmarshal* / GobDecode methods, which have to mutate). AppendText is not on that list, so it is the one method that makes the receiver set mixed.

AppendText only reads the value (append(b, x.String()...)), so a value receiver is the correct choice and matches MarshalText. Both Foo and *Foo still satisfy encoding.TextAppender, since a pointer's method set includes the value-receiver methods.

Change:

  • generator/enum.tmpl and generator/enum_string.tmpl: func (x *{{.enum.Name}}) AppendText becomes func (x {{.enum.Name}}) AppendText.
  • Regenerated example/*_enum.go (make generate) and the generator/.snapshots golden files (UPDATE_SNAPSHOTS=true go test ./generator/...). Every changed line in those files is an AppendText receiver; nothing else moved.

Enums generated with --sql or --flag still carry pointer receivers on Scan, Set, Get, and Type. Those genuinely mutate the value and recvcheck does not exclude them, so this change does not silence the linter for those flag combinations; it only removes the one pointer receiver that had no reason to be one.

Testing

Toolchain: go1.27.1, golangci-lint v2.13.2 (bundles recvcheck v0.3.0).

Unit tests on this branch (same commands as the Makefile test target):

$ go test ./...
ok  	github.com/abice/go-enum	0.344s
ok  	github.com/abice/go-enum/example	0.397s [no tests to run]
ok  	github.com/abice/go-enum/generator	1.097s

$ go test --tags=example ./example
ok  	github.com/abice/go-enum/example	0.464s

Lint proof, in a scratch module containing only this file:

package proof

// ENUM(red, green, blue)
type Color int

Generated with the unmodified v0.9.4 release:

$ go run github.com/abice/go-enum@v0.9.4 -f color.go --marshal
$ golangci-lint run --no-config --enable-only recvcheck ./...
color.go:4:6: the methods of "Color" use pointer receiver and non-pointer receiver. (recvcheck)
type Color int
     ^
1 issues:
* recvcheck: 1

Generated with bin/go-enum built from this branch:

$ bin/go-enum -f color.go --marshal
$ golangci-lint run --no-config --enable-only recvcheck ./...
0 issues.

The only difference between the two generated files, apart from the version header, is the receiver:

$ diff v094/color_enum.go patched/color_enum.go
81c82
< func (x *Color) AppendText(b []byte) ([]byte, error) {
---
> func (x Color) AppendText(b []byte) ([]byte, error) {

Summary by CodeRabbit

  • Compatibility

    • Enum text-appending methods now work consistently with both enum values and pointers.
    • Existing output and error behavior remains unchanged.
  • Generated Code

    • Updated generated enum implementations and templates to use value-based method handling across supported configurations.

@david-long1
david-long1 requested a review from abice as a code owner September 19, 2026 04:20
@coderabbitai

coderabbitai Bot commented Sep 19, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: c0d31ce4-5c14-4df4-9f15-2fc3ee1cd154

📥 Commits

Reviewing files that changed from the base of the PR and between 04fd4b6 and d9a0bfe.

📒 Files selected for processing (29)
  • example/color_enum.go
  • example/commented_enum.go
  • example/example_enum.go
  • example/replace_prefix_enum.go
  • example/replace_prefix_int_enum.go
  • example/sql_enum.go
  • example/strings_only_enum.go
  • generator/.snapshots/Test118CustomPrefixExampleFile-1.18
  • generator/.snapshots/Test118CustomPrefixExampleFile-og
  • generator/.snapshots/Test118ExampleFile-1.18
  • generator/.snapshots/Test118ExampleFile-og
  • generator/.snapshots/Test118ExampleFileMoreOptions-1.18
  • generator/.snapshots/Test118ExampleFileMoreOptions-og
  • generator/.snapshots/Test118NoPrefixExampleFile-1.18
  • generator/.snapshots/Test118NoPrefixExampleFile-og
  • generator/.snapshots/Test118NoPrefixExampleFileWithSnakeToCamel-1.18
  • generator/.snapshots/Test118NoPrefixExampleFileWithSnakeToCamel-og
  • generator/.snapshots/TestCustomPrefixExampleFile
  • generator/.snapshots/TestExampleFile
  • generator/.snapshots/TestExampleFileEmptyHeaders
  • generator/.snapshots/TestExampleFileMoreOptions
  • generator/.snapshots/TestExampleFileMoreOptionsWithForceUpper
  • generator/.snapshots/TestNoIotaExampleFile
  • generator/.snapshots/TestNoIotaOnlyExampleFile
  • generator/.snapshots/TestNoPrefixExampleFile
  • generator/.snapshots/TestNoPrefixExampleFileWithSnakeToCamel
  • generator/.snapshots/TestReplacePrefixExampleFile
  • generator/enum.tmpl
  • generator/enum_string.tmpl

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The generator templates and generated enum outputs now use value receivers for AppendText. The methods retain their existing parameters, string-appending behavior, and nil error results.

Changes

AppendText receiver updates

Layer / File(s) Summary
Generator templates
generator/enum.tmpl, generator/enum_string.tmpl
The templates now generate AppendText methods with value receivers.
Example enum implementations
example/*
The example enum methods now use value receivers without changing their output or error behavior.
Generated snapshots
generator/.snapshots/*
Generated snapshot outputs were updated to match the value-receiver templates across the supported generator options.

Priority: ⬇️ Low

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: changing generated enum AppendText methods to value receivers to resolve recvcheck warnings.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 7…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 92.701%. remained the same — david-long1:fix/appendtext-value-receiver into abice:master

@abice
abice merged commit ad6b63d into abice:master Sep 21, 2026
7 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.

3 participants