Index: third_party/mojo/src/mojo/public/tools/bindings/generators/go_templates/struct.tmpl |
diff --git a/third_party/mojo/src/mojo/public/tools/bindings/generators/go_templates/struct.tmpl b/third_party/mojo/src/mojo/public/tools/bindings/generators/go_templates/struct.tmpl |
index 457e33946e76de12d39ab3af1bc7a5510c46b8a1..27f91aed42a91a15a628a10aa78b0e0df3ff3bb5 100644 |
--- a/third_party/mojo/src/mojo/public/tools/bindings/generators/go_templates/struct.tmpl |
+++ b/third_party/mojo/src/mojo/public/tools/bindings/generators/go_templates/struct.tmpl |
@@ -23,18 +23,32 @@ func (s *{{struct|name(exported)}}) Encode(encoder *bindings.Encoder) error { |
return nil |
} |
+var {{struct|name(False)}}_Versions []bindings.DataHeader = []bindings.DataHeader{ |
+{% for versionInfo in struct.versions %} |
+ bindings.DataHeader{{'{'}}{{versionInfo.num_bytes}}, {{versionInfo.version}}{{'}'}}, |
+{% endfor %} |
+} |
+ |
func (s *{{struct|name(exported)}}) Decode(decoder *bindings.Decoder) error { |
-{% if struct.bytes %} |
- version, err := decoder.StartStruct() |
-{% else %} |
- _, err := decoder.StartStruct() |
-{% endif %} |
+ header, err := decoder.StartStruct() |
if err != nil { |
return err |
} |
+ index := sort.Search(len({{struct|name(False)}}_Versions), func(i int) bool { |
+ return {{struct|name(False)}}_Versions[i].ElementsOrVersion >= header.ElementsOrVersion |
+ }) |
+ if index < len({{struct|name(False)}}_Versions) { |
+ if {{struct|name(False)}}_Versions[index].ElementsOrVersion > header.ElementsOrVersion { |
+ index-- |
+ } |
+ expectedSize := {{struct|name(False)}}_Versions[index].Size |
+ if expectedSize != header.Size { |
+ return fmt.Errorf("invalid struct header size: should be %d, but was %d", expectedSize, header.Size) |
+ } |
+ } |
{% for byte in struct.bytes %} |
{% for packed_field in byte.packed_fields %} |
- if version >= {{packed_field.min_version}} { |
+ if header.ElementsOrVersion >= {{packed_field.min_version}} { |
{{decode('s.'~packed_field.field|name(exported), packed_field.field.kind)|tab_indent(2)}} |
} |
{% endfor %} |