blob: 9f00b2a8d961ed61bd18ca6c32bd9de9fd94d98c [file] [log] [blame]
// Copyright 2018 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.
package templates
// Union is the template for union declarations.
const Union = `
{{- define "UnionDeclaration" -}}
enum {{ .TagName }} {
{{- range .Members }}
{{ .Name }},
{{- end }}
}
{{- range .Doc }}
///{{ . -}}
{{- end }}
class {{ .Name }} extends $fidl.Union {
{{- range .Members }}
const {{ $.Name }}.with{{ .CtorName }}({{ .Type.Decl }} value)
: _data = value, tag = {{ $.TagName }}.{{ .Name }};
{{- end }}
{{ .Name }}._(this.tag, Object data) : _data = data;
final {{ .TagName }} tag;
final _data;
{{- range .Members }}
{{ .Type.Decl }} get {{ .Name }} {
if (tag != {{ $.TagName }}.{{ .Name }}) {
return null;
}
return _data;
}
{{- end }}
@override
String toString() {
switch (tag) {
{{- range .Members }}
case {{ $.TagName }}.{{ .Name }}:
return '{{ $.Name }}.{{ .Name }}(${{ .Name }})';
{{- end }}
default:
return null;
}
}
{{- range .Doc }}
///{{ . -}}
{{- end }}
@override
int get $index => tag.index;
@override
Object get $data => _data;
static {{ .Name }} _ctor(int index, Object data) {
return new {{ .Name }}._({{ .TagName }}.values[index], data);
}
}
// See FIDL-308:
// ignore: recursive_compile_time_constant
const $fidl.UnionType<{{ .Name }}> {{ .TypeSymbol }} = {{ .TypeExpr }};
{{ end }}
`