-
Notifications
You must be signed in to change notification settings - Fork 32
Forbid unmarshaling non-scalars into a TextUnmarshaler #77
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
base: main
Are you sure you want to change the base?
Conversation
|
This PR seems good to me but I'd like to see more discussion. |
I've been thinking about this for a bit, but I have no good ideas how to do so. We could port this patch to v3, and run tests across popular repositories with Going the conservative way, we could add a deprecation warning, and an option to opt in immediately. |
24e288b to
83b57fd
Compare
|
I would suggest to do same as json/v2 - unmarshal to a string first, then pass it to |
|
@copilot create an issue based on this PR |
This PR already does the same thing as json/v2. |
|
The only difference perhaps is that handling of ...now that I spelled this out, I think odd cases like unmarshaling a YAML sequence into a slice type implementing |
83b57fd to
33eb195
Compare
|
I've updated this by making the check more explicit. |
This is a breaking change.
Previously an attempt to unmarshal a YAML mapping into a struct
implementing encoding.TextUnmarshaler would succeed, but likely do the wrong
thing.
Consider this example:
type Foo struct {
IP netip.Addr `yaml:"ip"`
}
netip.Addr is a struct type implementing TextUnmarshaler, but
containing no exported fields. So unmarshaling {"ip": "127.0.0.1"} would
use TextUnmarshaler (as expected), but {"ip": {"whatever": 123}} would
silently do nothing.
With this commit, it will now result in a type error.
This matches the behavior of both encoding/json and encoding/json/v2.
The documentation of the latter states:
> If the value type implements encoding.TextUnmarshaler, then the input
> is decoded as a JSON string and the UnmarshalText method is called with
> the decoded string value. This fails with a SemanticError if the input
> is not a JSON string.
encoding/json documentation is somewhat ambiguous on this, but it also
behaves the same way (https://go.dev/play/p/z9okIXkUzvQ).
33eb195 to
bd5ce19
Compare
This is a breaking change.
Previously an attempt to unmarshal a YAML mapping into a struct implementing
encoding.TextUnmarshalerwould succeed, but likely do the wrong thing.Consider this example:
netip.Addris a struct type implementingTextUnmarshaler, but containing no exported fields. So unmarshaling{"ip": "127.0.0.1"}would useTextUnmarshaler(as expected), but{"ip": {"whatever": 123}}would silently do nothing.With this commit, it will now result in a type error.
encoding/json/v2documentation states:encoding/jsondocumentation is somewhat ambiguous on this, but it also behaves the same way (https://go.dev/play/p/z9okIXkUzvQ).