Skip to content
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

encoding/gob: allow types to implement an interface which lets them convert themself into an encodable value #48894

Closed
Keithcat1 opened this issue Oct 9, 2021 · 6 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@Keithcat1
Copy link

Currently, the encoding/gob package provides the ability to change the way a type is serialized or deserialized by implementing GobEncoder or GobDecoder on it. This works by having the type somehow convert itself from or to a byte slice and then gob just serializes the resulting bytes.
This isn't usually a problem, but some types can't be serialized as they are and require using the encoding/binary package or something similar to manually convert them to bytes and back again.
This approach is also problematic when trying to serialize a data structure that contains multiple pointers pointing to the same value. When serializing, gob will store the values behind the pointers, not the pointers themselves, and then later loading the data could cause unexpected effects.
A much nicer approach would be to add two new interfaces that allow their implementers to return values which should be serialized in their place and to convert back to the original type respectively.
I'm not sure what the names for these interfaces should be.

@seankhliao
Copy link
Member

I don't see how that would solve your problem oh punters?
In any case your type can implement GobEncoder by converting itself to encodable values, calling encode on them and returning the result

@ianlancetaylor
Copy link
Contributor

Why can't your GobEncoder method use encoding/binary if appropriate?

Note that encoding/gob also checks for the encoding.BinaryMarshaler and encoding.TextMarshaler interfaces.

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 9, 2021
@Keithcat1
Copy link
Author

Keithcat1 commented Oct 12, 2021 via email

@ianlancetaylor
Copy link
Contributor

I guess it's not clear to me why you want to use gob to encode a value that is already being encoded by gob.

Can you give a specific example of a data structure that would use this new feature? Thanks.

@Keithcat1
Copy link
Author

Keithcat1 commented Oct 14, 2021 via email

@seankhliao
Copy link
Member

Seems like we already have a way of doing this (GobEncode or BinaryMarshaler or TextMarshaler),
and the proposed change would only save a few lines without being much more efficient (gob still has to do the same amount of work serializing structs).

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 12, 2022
@golang golang locked and limited conversation to collaborators Jun 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants