| {{/* | |
| // Copyright 2025 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 "StructDeclaration" -}} | |
| @dataclass | |
| class {{ .PythonName }}: | |
| {{- if .DocComments }} | |
| """ | |
| {{- range .DocComments }} | |
| {{ . | escapeQuotes | trimSpace | indentNonEmpty4 }} | |
| {{- end }} | |
| """ | |
| {{- end }} | |
| {{- range .PythonMembers }} | |
| {{- if .DocComments }} | |
| """ | |
| {{- range .DocComments }} | |
| {{ . | escapeQuotes | trimSpace | indentNonEmpty4 }} | |
| {{- end }} | |
| """ | |
| {{- end }} | |
| {{ .PythonName }}: {{ .PythonType.PythonName }} | |
| {{- end }} | |
| __fidl_kind__ = "struct" | |
| __fidl_type__ = "{{ .PythonName }}" | |
| __fidl_raw_type__ = "{{ .Name }}" | |
| # TODO(https://fxbug.dev/394421154): We should probably remove this method when we | |
| # start making breaking changes. | |
| def __getitem__(self, item: str): # type: ignore | |
| if not isinstance(item, str): | |
| raise TypeError("Subscripted item must be a string") | |
| return getattr(self, item) | |
| def encode(self) -> tuple[bytes, list[typing.Any]]: | |
| return fidl.encode_struct(self, type(self)._encode, {{ .TypeShapeV2.InlineSize }}) | |
| @classmethod | |
| def decode(cls, data: bytes, handles: list[typing.Any]) -> typing.Self: | |
| return fidl.decode_struct(data, handles, cls._decode, {{ .TypeShapeV2.InlineSize }}) | |
| def _encode(self, encoder: fidl.Encoder, _offset: int) -> None: | |
| {{- if .PythonMembers }} | |
| {{- range .PythonMembers }} | |
| {{ .EncoderCall | indentNonEmpty8 }} | |
| {{- end }} | |
| {{- else }} | |
| pass | |
| {{- end }} | |
| @classmethod | |
| def _decode(cls, decoder: fidl.Decoder, _offset: int) -> typing.Self: | |
| {{- if .PythonMembers }} | |
| {{- range .PythonMembers }} | |
| {{ .PythonName }} = {{ .DecoderCall }} | |
| {{- end }} | |
| return cls( | |
| {{- range .PythonMembers }} | |
| {{ .PythonName }}={{ .PythonName }}, | |
| {{- end }} | |
| ) | |
| {{- else }} | |
| return cls() | |
| {{- end }} | |
| # TODO(https://fxbug.dev/394421154): Assigning None (incorrectly) to each type is a consequence | |
| # of needing to support creation of a "default object" before decoding. | |
| @classmethod | |
| def make_default(cls) -> typing.Self: | |
| return cls(**{ | |
| {{- range .PythonMembers }} | |
| "{{ .PythonName }}": None, # type: ignore[arg-type,unused-ignore] | |
| {{- end }} | |
| }) | |
| {{ end }} |