| {{/* |
| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| */}} |
| |
| {{- define "UnionDefinition" }} |
| {{- $union := . }} |
| |
| type {{ $union.TagName }} uint64 |
| const ( |
| {{- if $union.IsFlexible }} |
| {{ $union.Name }}_unknownData = 0 // 0x00000000 |
| {{- end }} |
| {{- range $memb := $union.Members }} |
| {{ $union.Name }}{{ $memb.Name }} = {{ $memb.Ordinal }} // {{ $memb.Ordinal | printf "%#08x" }} |
| {{- end }} |
| ) |
| |
| {{ range $comment := $union.DocComments }} |
| //{{ $comment }} |
| {{- end }} |
| type {{ $union.Name }} struct { |
| {{ $union.TagName }} `{{ $union.Tags }}` |
| {{- if $union.IsFlexible }} |
| I_unknownData interface{} |
| {{- end }} |
| {{- range $memb := $union.Members }} |
| {{- range $comment := $memb.DocComments }} |
| //{{ $comment }} |
| {{- end }} |
| {{ $memb.Name }} {{ $memb.Type }} `{{ $memb.Tags }}` |
| {{- end }} |
| } |
| |
| var _m{{ $union.Name }} = _bindings.CreateLazyMarshaler({{ $union.Name }}{}) |
| |
| func (msg *{{ $union.Name }}) Marshaler() _bindings.Marshaler { |
| return _m{{ $union.Name }} |
| } |
| |
| {{- /* |
| The reset() method below zeroes out all existing variant data, but does not |
| clear the tag value. This method is meant to be used internally by Set...()-like |
| methods to "reset" the union container back to a fully zeroed state before |
| setting a new variant. Failing to do this will result in newly constructed |
| unions and modified-in-place unions with ostensibly the same variant to fail |
| equality checks. |
| */}} |
| |
| func (_m *{{ $union.Name }}) reset() { |
| switch _m.{{ $union.TagName }} { |
| {{- range $memb := $union.Members }} |
| case {{ $memb.Ordinal }}: |
| var _zeroed {{ $memb.Type }} |
| _m.{{ $memb.Name }} = _zeroed |
| {{- end }} |
| {{- if $union.IsFlexible }} |
| default: |
| var _zeroed interface{} |
| _m.I_unknownData = _zeroed |
| {{- end }} |
| } |
| } |
| |
| func (_m *{{ $union.Name }}) Which() {{ $union.TagName }} { |
| {{- if $union.IsStrict }} |
| return _m.{{ $union.TagName }} |
| {{- else }} |
| switch _m.{{ $union.TagName }} { |
| {{- range $memb := $union.Members }} |
| case {{ $memb.Ordinal }}: |
| return {{ $union.Name }}{{ $memb.Name }} |
| {{- end }} |
| default: |
| return {{ $union.Name }}_unknownData |
| } |
| {{- end }} |
| } |
| |
| func (_m *{{ $union.Name }}) Ordinal() uint64 { |
| return uint64(_m.{{ $union.TagName }}) |
| } |
| |
| {{- range $memb := $union.Members }} |
| |
| func (_m *{{ $union.Name }}) Set{{ $memb.Name }}({{ $memb.PrivateName }} {{ $memb.Type }}) { |
| _m.reset() |
| _m.{{ $union.TagName }} = {{ $union.Name }}{{ $memb.Name }} |
| _m.{{ $memb.Name }} = {{ $memb.PrivateName }} |
| } |
| |
| func {{ $union.Name }}With{{ $memb.Name }}({{ $memb.PrivateName }} {{ $memb.Type }}) {{ $union.Name }} { |
| var _u {{ $union.Name }} |
| _u.Set{{ $memb.Name }}({{ $memb.PrivateName }}) |
| return _u |
| } |
| {{- end }} |
| |
| {{- if $union.IsFlexible }} |
| func (_m *{{ $union.Name }}) GetUnknownData() {{ $union.UnknownDataType }} { |
| return _m.I_unknownData.({{ $union.UnknownDataType }}) |
| } |
| {{- end }} |
| |
| {{/* Note that there is no SetUnknownData() function. If you really need to set the I_unknownData |
| field (e.g. for testing), use Go's reflect package. */}} |
| |
| {{- end -}} |