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
WaitingForInfo

Comments

@Keithcat1
Copy link

@Keithcat1 Keithcat1 commented Oct 9, 2021

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

@seankhliao seankhliao commented Oct 9, 2021

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

@ianlancetaylor ianlancetaylor commented Oct 9, 2021

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 label Oct 9, 2021
@Keithcat1
Copy link
Author

@Keithcat1 Keithcat1 commented Oct 12, 2021

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Oct 12, 2021

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 Keithcat1 commented Oct 14, 2021

@seankhliao
Copy link
Member

@seankhliao seankhliao commented Jun 12, 2022

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 Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WaitingForInfo
Projects
None yet
Development

No branches or pull requests

3 participants