golang/xenlight: use struct pointers in keyed union fields
authorNick Rosbrook <rosbrookn@gmail.com>
Mon, 24 May 2021 20:36:46 +0000 (16:36 -0400)
committerGeorge Dunlap <george.dunlap@citrix.com>
Mon, 21 Jun 2021 15:49:08 +0000 (16:49 +0100)
commit1997940ad25e3566d1ab38496b8c7b07a086695a
tree887f29c242306ca242aafe3cf5a42470845beed3
parentbc9f632e31ee66be3f1860fc7303fe91a42e56a6
golang/xenlight: use struct pointers in keyed union fields

Currently, when marshalig Go types with keyed union fields, we assign the
value of the struct (e.g. DomainBuildInfoTypeUnionHvm) which implements the
interface of the keyed union field (e.g. DomainBuildInfoTypeUnion).
As-is, this means that if a populated DomainBuildInfo is marshaled to
e.g. JSON, unmarshaling back to DomainBuildInfo will fail.

When the encoding/json is unmarshaling data into a Go type, and
encounters a JSON object, it basically can either marshal the data into
an empty interface, a map, or a struct. It cannot, however, marshal data
into an interface with at least one method defined on it (e.g.
DomainBuildInfoTypeUnion). Before this check is done, however, the
decoder will check if the Go type is a pointer, and dereference it if
so. It will then use the type of this value as the "target" type.

This means that if the TypeUnion field is populated with a
DomainBuildInfoTypeUnion, the decoder will see a non-empty interface and
fail. If the TypeUnion field is populated with a
*DomainBuildInfoTypeUnionHvm, it dereferences the pointer and sees a
struct instead, allowing decoding to continue normally.

Since there does not appear to be a strict need for NOT using pointers
in these fields, update code generation to set keyed union fields to
pointers of their implementing structs.

Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
Reviewed-by: George Dunlap <george.dunlap@citrix.com>
tools/golang/xenlight/gengotypes.py
tools/golang/xenlight/helpers.gen.go