| {{/* |
| // 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. |
| */}} |
| |
| {{- define "EnumDefinition" -}} |
| {{- $enum := . }} |
| |
| {{ range $comment := .DocComments }} |
| //{{ $comment }} |
| {{- end }} |
| type {{ $enum.Name }} {{ $enum.Type }} |
| |
| const ( |
| {{- range $memb := $enum.Members }} |
| {{ if $memb.DocComments -}} |
| {{ range $comment := $memb.DocComments }} |
| //{{ $comment }} |
| {{- end }} |
| {{ end -}} |
| {{ $enum.Name }}{{ $memb.Name }} {{ $enum.Name }} = {{ $memb.Value }} |
| {{- end }} |
| |
| {{ if $enum.IsFlexible }} |
| // {{ $enum.Name }}_Unknown is the default unknown placeholder. |
| {{ $enum.Name }}_Unknown {{ $enum.Name }} = {{ $enum.UnknownValueForTmpl | printf "%#x" }} |
| {{ end }} |
| ) |
| |
| func (_ {{ $enum.Name }}) I_EnumValues() []{{ $enum.Name }} { |
| return []{{ $enum.Name }}{ |
| {{- range $memb := $enum.Members }} |
| {{ $enum.Name }}{{ $memb.Name }}, |
| {{- end }} |
| } |
| } |
| |
| func (_ {{ $enum.Name }}) I_EnumIsStrict() bool { |
| return {{ $enum.IsStrict }} |
| } |
| |
| func (x {{ $enum.Name }}) IsUnknown() bool { |
| {{- if $enum.Members }} |
| switch x { |
| {{- range $memb := $enum.Members }} |
| {{- if not $memb.IsUnknown }} |
| case {{ $memb.Value }}: |
| return false |
| {{- end }} |
| {{- end }} |
| } |
| {{- end }} |
| return true |
| } |
| |
| func (x {{ $enum.Name }}) String() string { |
| {{- if $enum.Members }} |
| switch x { |
| {{- range $memb := $enum.Members }} |
| case {{ $memb.Value }}: |
| return "{{ $memb.Name }}" |
| {{- end }} |
| } |
| {{- end }} |
| return "Unknown" |
| } |
| {{- end -}} |