When working with wire formats, serialisation, and external types, it is useful to change the struct tags: https://play.golang.org/p/h6b6FmeDuaR. But when the original struct has a field added, this breaks: https://play.golang.org/p/VHmV9r2MxNt. It seems like a foregone conclusion that we suggest against adding a
struct field because it is a breaking change. That said, we probably do
not want to restrict the ability to convert structs as it is really
helpful. Is this a known issue or previously discussed topic? If not what if anything should be done to clarify best practices?
When working with wire formats, serialisation, and external types, it is useful to change the struct tags: https://play.golang.org/p/h6b6FmeDuaR. But when the original struct has a field added, this breaks: https://play.golang.org/p/VHmV9r2MxNt. It seems like a foregone conclusion that we suggest against adding a struct field because it is a breaking change. That said, we probably do not want to restrict the ability to convert structs as it is really helpful. Is this a known issue or previously discussed topic? If not what if anything should be done to clarify best practices?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/dcdcbd6a-838e-47c1-8b3d-935d485d96b5n%40googlegroups.com.
On Mar 11, 2021, at 12:43 PM, 'Axel Wagner' via golang-nuts <golan...@googlegroups.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGp%2B%2B90tK1A-Sti1wjap%3DXxBU-QfrE1ReUSCYDt75tmHQ%40mail.gmail.com.
On Mar 11, 2021, at 5:28 PM, Axel Wagner <axel.wa...@googlemail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHQciGzQoVPnch%2BuyUCjR6gnGzfk%3DBZvnc6ncMF-KYS%3Dw%40mail.gmail.com.
I must be dense on this. I have no idea why there is any use of struct tags or JSON marshaling in the sample code.
On Fri, Mar 12, 2021 at 3:26 AM Robert Engels <ren...@ix.netcom.com> wrote:I must be dense on this. I have no idea why there is any use of struct tags or JSON marshaling in the sample code.To demonstrate why converting between different struct types is useful.If you want take an struct existing type (defined in a different package) and change how it is marshalled (say, omit or rename a certain field) you'd have to create a new struct type with different tags and copy over the fields. This was deemed inconvenient, so it was changed to the current semantics in go 1.8. OP is pointing out that the change means adding a field to a struct is now a backwards incompatible change, as someone might to this conversion.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/oAjxpH-Qq2Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/36e1a048-2505-4841-8882-a9b16a33fd57n%40googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/oAjxpH-Qq2Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d091ff96-0407-4e82-b098-ffd4b37f6377n%40googlegroups.com.
I can see two separate things under discussion - compatibility of struct types for conversion, and backwards-compatibility of serialization [JSON? Protobuf?]
but I can't see what's being proposed to change.
Either
1. Create consensus that adding an (exported) field to a struct should be considered a backwards incompatible change, or
2. Create consensus that converting one struct type into a different struct type should be considered deprecated, as it makes you vulnerable to getting broken by someone adding a field.
Take either consensus and put it into the Go 1 compatibility promise and potentially encode it into a vet check.
I don't follow the argument that "adding a field to a struct is now a backwards incompatible change". Surely this was always the case, regardless of tags? That is, if you havetype T1 struct {Foo string}type T2 struct {Foo stringBar string}then what would you expect to happen for:v1 := T1{}v2 := T2(v1) ??...v2 := T2{}v1 := T1(v2) ??These two structures have different shapes. In the first case, I suppose it could copy the matching members and create zero values for the new ones. In the second case, I suppose it could copy the matching members and silently(!) discard the missing ones. But either way, that just means the compiler magically writing code which copies member-by-member. I'd rather do this by hand.Even with magic conversions, trying to use a *t1 as a *t2 or vice versa would definitely not work, as they have differing layout in memory. Indeed, even the ordering of fields in memory must be the same for two structs to be compatible:However, if two structures have the same members in the same order, and differ only by tags, then they have the same shape. Then it seems reasonable to me to treat a T1 as compatible with T2; copying can be done as a blob of bytes. The change in 1.8 to omit comparing tags has made types more compatible, not less.Separately from that, there's the issue of serialization and forwards/backwards compatibility. I don't see any problem here. For example, if you serialize a type T1 to JSON, and then deserialize to T2 (which may have more or fewer fields), that all works fine.If you want to reject unexpected fields when deserializing, there are options for doing that. But by default, they are compatible in both directions. You can also re-order the fields in the struct, since the JSON deserialization is assigning elements individually.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/36e1a048-2505-4841-8882-a9b16a33fd57n%40googlegroups.com.
On Mar 12, 2021, at 6:15 AM, 'Axel Wagner' via golang-nuts <golan...@googlegroups.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfE09%2BW0MU55LeJfm-9BNM_7SZhGkfRxD4wkP4Rj53Eq_A%40mail.gmail.com.
It may be worth noting that we already have a vet check for composite
literals that verifies that if a composite literal is used with a
struct defined in a different package, then the fields are explicitly
named. This check means that adding fields to a struct will never
break composite literals in other packages. This corresponds to this
Struct literals. For the addition of features in later point
releases, it may be necessary to add fields to exported structs in the
API. Code that uses unkeyed struct literals (such as pkg.T{3, "x"}) to
create values of these types would fail to compile after such a
change. However, code that uses keyed literals (pkg.T{A: 3, B: "x"})
will continue to compile after such a change. We will update such data
structures in a way that allows keyed struct literals to remain
compatible, although unkeyed literals may fail to compile. (There are
also more intricate cases involving nested data structures or
interfaces, but they have the same resolution.) We therefore recommend
that composite literals whose type is defined in a separate package
should use the keyed notation.
I think it would be consistent with that to add a vet check that would
warn about attempts to convert a struct defined in a different package
to a locally defined struct.
Ian
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/oAjxpH-Qq2Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXejyPtRM-sUN_JkcsP24Y2dqfPeqdensbOzmpbKF9r5Q%40mail.gmail.com.