-
-
Notifications
You must be signed in to change notification settings - Fork 360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
staticcheck: detect unintentional encoder and decoder implementations #715
Comments
I'd argue that any type that embeds a type that implements TextMarshaler, BinaryMarshaler, or json.Marshaler (and unmarshalers) should get flagged. It's almost certainly not intended, and if it is it can be implemented in the parent type by calling the embedded method. |
@FiloSottile I think, that's way too radical. Consider something like: type TimeWithSomeContext struct {
time.Time
Processed bool
Status int
// …
}
I want |
@ainar-g you'd do this to resolve the error. seems nice to be explicit: type TimeWithSomeContext struct {
Time time.Time
Processed bool
Status int
// …
}
func (t TimeWithSomeContext) MarshalJSON() ([]byte, error) {
return t.Time.MarshalJSON()
} |
I agree with Ainar. We can very likely flag structs that use tags, but flagging any struct that embeds a marshaler will lead to some false positives, and we always try to err on the side of false negatives, not false positives. It's fine to miss some bugs, it's not fine to claim bugs where there aren't any. We could consider adding an opt-in for more strict checking a la Filippo, and then you can decide to enforce that rule in your own code base. |
Could you also flag locations where such a type is passed to json.Marshal?
|
The statically known ones, yes. For the case of embedding but no struct tags, we can only flag the locations where |
(Couldn't think of a more descriptive title, feel free to change.)
See this tweet from @katiehockman.
This will print
2009-11-10T23:00:00Z
, becausetime.Time
implementsjson.Marshaler
.The structs that we can definitely mark are the structs that (1) have fields with
json
tags, (2) embed marshalers, and (3) don't implement the interface themselves:The text was updated successfully, but these errors were encountered: