Skip to content

marshal-receiver: new rule to enforce correct receiver types for marshal/unmarshal methods #1706

@roidelapluie

Description

@roidelapluie

Is your feature request related to a problem? Please describe.
When implementing JSON or YAML serialization in Go, developers sometimes use incorrect receiver types for MarshalJSON, MarshalYAML, UnmarshalJSON, and UnmarshalYAML methods. Using a pointer receiver on a Marshal method means marshaling won't work when the value is passed by value (e.g., in a slice or map). Using a value receiver on an Unmarshal method means modifications to the receiver are lost. These bugs are subtle and easy to miss in code review.

Describe the solution you'd like
A new marshal-receiver lint rule that flags:

  • MarshalJSON / MarshalYAML methods using a pointer receiver (should use value receiver)
  • UnmarshalJSON / UnmarshalYAML methods using a value receiver (should use pointer receiver)

Describe alternatives you've considered
Manual code review and documentation guidelines.

Additional context
N/A

Metadata

Metadata

Assignees

Labels

feedback requiredRequires additional feedback from a contributorrule proposalIssue proposing a new rule

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions