make keyed union types easier to marshal
cover make keyed union types easier to marshal
These patches make changes to the generated "keyed union" types that allow marshaling and unmarshaling easier, specifically when using the encoding/json package from the Go standard library.
Nick Rosbrook (2):
golang/xenlight: export keyed union interface types golang/xenlight: use struct pointers in keyed union fields
tools/golang/xenlight/gengotypes.py | 10 ++--- tools/golang/xenlight/helpers.gen.go | 44 ++++++++++----------- tools/golang/xenlight/types.gen.go | 58 ++++++++++++++-------------- 3 files changed, 56 insertions(+), 56 deletions(-)
Signature · 1 line
2.17.1
PATCH 2/2 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.
--- tools/golang/xenlight/gengotypes.py | 4 +-- tools/golang/xenlight/helpers.gen.go | 44 ++++++++++++++-------------- 2 files changed, 24 insertions(+), 24 deletions(-)
tools/golang/xenlight/gengotypes.py +2 -2
@@ -397,7 +397,7 @@ def xenlight_golang_union_from_C(ty = None, union_name = '', struct_name = ''): s += 'if err := {0}.fromC(xc);'.format(goname) s += 'err != nil {{\n return fmt.Errorf("converting field {0}: %v", err)\n}}\n'.format(goname) - s += 'x.{0} = {1}\n'.format(field_name, goname) + s += 'x.{0} = &{1}\n'.format(field_name, goname) # End switch statement s += 'default:\n'
@@ -563,7 +563,7 @@ def xenlight_golang_union_to_C(ty = None, union_name = '', gotype = xenlight_golang_fmt_name(cgotype) field_name = xenlight_golang_fmt_name('{0}_union'.format(keyname)) - s += 'tmp, ok := x.{0}.({1})\n'.format(field_name,gotype) + s += 'tmp, ok := x.{0}.(*{1})\n'.format(field_name,gotype) s += 'if !ok {\n' s += 'return errors.New("wrong type for union key {0}")\n'.format(keyname) s += '}\n'
tools/golang/xenlight/helpers.gen.go +22 -22
173 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 1/2 golang/xenlight: export keyed union interface types
In reply to Nick RosbrookFor structs that have a keyed union, e.g. DomainBuildInfo, the TypeUnion field must be exported so that package users can get/set the fields within. This means that users are aware of the existence of the interface type used in those fields (see [1]), so it is awkward that the interface itself is not exported. However, the single method within the interface must remain unexported so that users cannot mistakenly "implement" those interfaces.
Since there seems to be no reason to do otherwise, export the keyed union interface types.
--- tools/golang/xenlight/gengotypes.py | 6 +-- tools/golang/xenlight/types.gen.go | 58 ++++++++++++++--------------- 2 files changed, 32 insertions(+), 32 deletions(-)
tools/golang/xenlight/gengotypes.py +3 -3
@@ -152,7 +152,7 @@ def xenlight_golang_define_union(ty = None, struct_name = '', union_name = ''): extras = [] interface_name = '{0}_{1}_union'.format(struct_name, ty.keyvar.name) - interface_name = xenlight_golang_fmt_name(interface_name, exported=False) + interface_name = xenlight_golang_fmt_name(interface_name) s += 'type {0} interface {{\n'.format(interface_name) s += 'is{0}()\n'.format(interface_name)
@@ -334,7 +334,7 @@ def xenlight_golang_union_from_C(ty = None, union_name = '', struct_name = ''): field_name = xenlight_golang_fmt_name('{0}_union'.format(keyname)) interface_name = '{0}_{1}_union'.format(struct_name, keyname) - interface_name = xenlight_golang_fmt_name(interface_name, exported=False) + interface_name = xenlight_golang_fmt_name(interface_name) cgo_keyname = keyname if cgo_keyname in go_keywords:
@@ -538,7 +538,7 @@ def xenlight_golang_union_to_C(ty = None, union_name = '', gokeytype = xenlight_golang_fmt_name(keytype) interface_name = '{0}_{1}_union'.format(struct_name, keyname) - interface_name = xenlight_golang_fmt_name(interface_name, exported=False) + interface_name = xenlight_golang_fmt_name(interface_name) cgo_keyname = keyname if cgo_keyname in go_keywords:
tools/golang/xenlight/types.gen.go +29 -29
173 lines not on this page · raw message
Signature · 1 line
2.17.1