Index: third_party/mojo/src/mojo/public/tools/bindings/generators/go_templates/encoding_macros.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/encoding_macros.tmpl |
similarity index 70% |
copy from third_party/mojo/src/mojo/public/tools/bindings/generators/go_templates/struct.tmpl |
copy to third_party/mojo/src/mojo/public/tools/bindings/generators/go_templates/encoding_macros.tmpl |
index f523ba6b8589ec3fab2a76801e28e4f3fd48f2f0..31e5c639739a345e19ccc9cc788a1882af8f66e7 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/encoding_macros.tmpl |
@@ -1,70 +1,3 @@ |
-// Copyright 2015 The Chromium Authors. All rights reserved. |
-// Use of this source code is governed by a BSD-style license that can be |
-// found in the LICENSE file. |
- |
-{% macro define(struct, exported=True) %} |
-type {{struct|name(exported)}} struct { |
-{% for field in struct.fields %} |
- {{field|name(exported)}} {{field.kind|go_type}} |
-{% endfor %} |
-} |
- |
-func (s *{{struct|name(exported)}}) Encode(encoder *bindings.Encoder) error { |
-{% set HEADER_SIZE = 8 %} |
- encoder.StartStruct({{struct.versions[-1].num_bytes - HEADER_SIZE}}, {{struct.versions[-1].version}}) |
-{% for byte in struct.bytes %} |
-{% for packed_field in byte.packed_fields %} |
- {{encode('s.'~packed_field.field|name(exported), packed_field.field.kind)|tab_indent()}} |
-{% endfor %} |
-{% endfor %} |
- if err := encoder.Finish(); err != nil { |
- return err |
- } |
- 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 { |
- 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 &bindings.ValidationError{bindings.UnexpectedStructHeader, |
- fmt.Sprintf("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 header.ElementsOrVersion >= {{packed_field.min_version}} { |
- {{decode('s.'~packed_field.field|name(exported), packed_field.field.kind)|tab_indent(2)}} |
- } |
-{% endfor %} |
-{% endfor %} |
- if err := decoder.Finish(); err != nil { |
- return err |
- } |
- return nil |
-} |
- |
-{% endmacro %} |
- |
- |
- |
{% macro encode(value, kind, level=0) %} |
{% if kind|is_nullable %} |
if {{value}} == nil { |
@@ -72,6 +5,8 @@ if {{value}} == nil { |
encoder.WriteInvalidInterface() |
{% elif kind|is_handle %} |
encoder.WriteInvalidHandle() |
+{% elif kind|is_union %} |
+ encoder.WriteNullUnion() |
{% else %} |
encoder.WriteNullPointer() |
{% endif %} |
@@ -91,7 +26,7 @@ if err := encoder.WritePointer(); err != nil { |
return err |
} |
{% endif %} |
-{% if kind|is_struct %} |
+{% if kind|is_struct or kind|is_union %} |
if err := {{value}}.Encode(encoder); err != nil { |
return err |
} |
@@ -199,6 +134,8 @@ if err != nil { |
if err := {{value}}.Decode(decoder); err != nil { |
return err |
} |
+{% elif kind|is_union %} |
+// TODO(azani): Implement union decoding. |
{% elif kind|is_array %} |
len{{level}}, err := decoder.StartArray({{kind.kind|bit_size}}) |
if err != nil { |