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

Side by Side Diff: mojo/public/tools/bindings/generators/go_templates/union.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(union, typepkg, package, exported=True) %}
9 type {{union|name(exported)}} interface {
10 Tag() uint32
11 Interface() interface{}
12 __Reflect(__{{union|name(exported)}}Reflect)
13 Encode(encoder *bindings.Encoder) error
14 }
15
16 {{runtime_type_macros.maybeWriteStaticMojomTypeAccessor(typepkg, union)}}
17
18 type __{{union|name(exported)}}Reflect struct {
19 {% for field in union.fields %}
20 {{field|name(exported)}} {{field.kind|go_type}}
21 {% endfor %}
22 }
23
24 func Decode{{union|name(exported)}}(decoder *bindings.Decoder) ({{union|name(exp orted)}}, error) {
25 size, tag, err := decoder.ReadUnionHeader()
26 if err != nil {
27 return nil, err
28 }
29
30 if size == 0 {
31 decoder.SkipUnionValue()
32 return nil, nil
33 }
34
35 switch tag {
36 {% for field in union.fields %}
37 case {{field.ordinal}}:
38 var value {{union|name(exported)}}{{field|name(exported)}}
39 if err := value.decodeInternal(decoder); err != nil {
40 return nil, err
41 }
42 decoder.FinishReadingUnionValue()
43 return &value, nil
44 {% endfor %}
45 }
46
47 decoder.SkipUnionValue()
48 return &{{union|name(exported)}}Unknown{tag: tag}, nil
49 }
50
51 {% set struct_name = union|name(exported) + 'Unknown' %}
52 type {{struct_name}} struct { tag uint32 }
53 func (u *{{struct_name}}) Tag() uint32 { return u.tag }
54 func (u *{{struct_name}}) Interface() interface{} { return nil }
55 func (u *{{struct_name}}) __Reflect(__{{union|name(exported)}}Reflect) {}
56
57 func (u *{{struct_name}}) Encode(encoder *bindings.Encoder) error {
58 return fmt.Errorf("Trying to serialize an unknown {{union|name(exported) }}. There is no sane way to do that!");
59 }
60
61 {% for field in union.fields %}
62 {%- set struct_name = union|name(exported) + field|name(exported) %}
63 type {{struct_name}} struct { Value {{field.kind|go_type}} }
64 func (u *{{struct_name}}) Tag() uint32 { return {{field.ordinal}} }
65 func (u *{{struct_name}}) Interface() interface{} { return u.Value }
66 func (u *{{struct_name}}) __Reflect(__{{union|name(exported)}}Reflect) {}
67
68 func (u *{{struct_name}}) Encode(encoder *bindings.Encoder) error {
69 encoder.WriteUnionHeader(u.Tag())
70 {{encode_union_field('u.Value', field.kind)|tab_indent()}}
71 encoder.FinishWritingUnionValue()
72 return nil
73 }
74
75 func (u *{{struct_name}}) decodeInternal(decoder *bindings.Decoder) error {
76 {{decode_union_field('u.Value', field.kind)|tab_indent()}}
77 return nil
78 }
79
80 {{runtime_type_macros.maybeWriteMojomTypeAccessor(typepkg, union, struct_name)}}
81
82 {% endfor %}
83
84 {% endmacro %}
85
86 {% macro encode_union_field(value, kind) %}
87 {% if kind|is_union %}
88 if err := encoder.WritePointer(); err != nil {
89 return err
90 }
91
92 encoder.StartNestedUnion()
93 {{encoding_macros.encode(value, kind)}}
94 encoder.Finish()
95 {% else %}
96 {{encoding_macros.encode(value, kind)}}
97 {% endif %}
98 {% endmacro %}
99
100 {% macro decode_union_field(value, kind) %}
101 {% if kind|is_union %}
102 if pointer, err := decoder.ReadPointer(); err != nil || pointer == 0 {
103 if err != nil {
104 return err
105 }
106 {% if kind|is_nullable %}
107 {{value}} = nil
108 return nil
109 {% else %}
110 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe cted null union pointer"}
111 {% endif %}
112 }
113
114 if err := decoder.StartNestedUnion(); err != nil {
115 return err
116 }
117
118 {{encoding_macros.decode(value, kind)}}
119
120 decoder.Finish()
121 {% else %}
122 {{encoding_macros.decode(value, kind)}}
123 {% endif %}
124 {% endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698