Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Side by Side Diff: mojo/public/tools/bindings/generators/go_templates/struct.tmpl

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 {% import "encoding_macros.tmpl" as encoding_macros %}
6 {% import "runtime_type_macros.tmpl" as runtime_type_macros %}
7
8 {% macro define(struct, typepkg, package, exported=True) %}
9 type {{struct|name(exported)}} struct {
10 {% for field in struct.fields %}
11 {{field|name(exported)}} {{field.kind|go_type}}
12 {% endfor %}
13 }
14
15 {%- set type_name = struct|name(exported) %}
16 {{runtime_type_macros.maybeWriteStaticMojomTypeAccessor(typepkg, struct)}}
17 {{runtime_type_macros.maybeWriteMojomTypeAccessor(typepkg, struct, type_name)}}
18
19 func (s *{{struct|name(exported)}}) Encode(encoder *bindings.Encoder) error {
20 {% set HEADER_SIZE = 8 %}
21 encoder.StartStruct({{struct.versions[-1].num_bytes - HEADER_SIZE}}, {{s truct.versions[-1].version}})
22 {% for byte in struct.bytes %}
23 {% for packed_field in byte.packed_fields %}
24 {{encoding_macros.encode('s.'~packed_field.field|name(exported), packed_ field.field.kind)|tab_indent()}}
25 {% endfor %}
26 {% endfor %}
27 if err := encoder.Finish(); err != nil {
28 return err
29 }
30 return nil
31 }
32
33 var {{struct|name(False)}}_Versions []bindings.DataHeader = []bindings.DataHeade r{
34 {% for versionInfo in struct.versions %}
35 bindings.DataHeader{{'{'}}{{versionInfo.num_bytes}}, {{versionInfo.versi on}}{{'}'}},
36 {% endfor %}
37 }
38
39 func (s *{{struct|name(exported)}}) Decode(decoder *bindings.Decoder) error {
40 header, err := decoder.StartStruct()
41 if err != nil {
42 return err
43 }
44 index := sort.Search(len({{struct|name(False)}}_Versions), func(i int) b ool {
45 return {{struct|name(False)}}_Versions[i].ElementsOrVersion >= h eader.ElementsOrVersion
46 })
47 if index < len({{struct|name(False)}}_Versions) {
48 if {{struct|name(False)}}_Versions[index].ElementsOrVersion > he ader.ElementsOrVersion {
49 index--
50 }
51 expectedSize := {{struct|name(False)}}_Versions[index].Size
52 if expectedSize != header.Size {
53 return &bindings.ValidationError{bindings.UnexpectedStru ctHeader,
54 fmt.Sprintf("invalid struct header size: should be %d, but was %d", expectedSize, header.Size),
55 }
56 }
57 }
58 {% for byte in struct.bytes %}
59 {% for packed_field in byte.packed_fields %}
60 if header.ElementsOrVersion >= {{packed_field.min_version}} {
61 {{encoding_macros.decode('s.'~packed_field.field|name(exported), packed_field.field.kind)|tab_indent(2)}}
62 }
63 {% endfor %}
64 {% endfor %}
65 if err := decoder.Finish(); err != nil {
66 return err
67 }
68 return nil
69 }
70
71 {% endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698