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

Side by Side Diff: mojom/generators/go/templates/unions.go

Issue 2045063002: [New go generator] Implement declaring unions. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 6 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 2016 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 package templates
6
7 import (
8 "text/template"
9 )
10
11 const unionTmplText = `
12 {{- define "Union" -}}
13 {{- $union := . }}
14 {{ template "UnionInterfaceDecl" $union }}
15
16 {{ template "UnknownUnionFieldDecl" $union }}
17 {{ template "UnknownUnionFieldEncode" $union }}
18
19 {{- range $field := $union.Field}}
20 {{ template "UnionFieldDecl" $field }}
21
22 {{ template "UnionFieldEncode" $field }}
23
24 {{ template "UnionFieldDecode" $field }}
25 {{- end}}
26
27 {{ template "UnionDecode" $union }}
28
29 {{- end -}}
30 `
31
32 const unionInterfaceDeclTmplText = `
33 {{- define "UnionInterfaceDecl" -}}
34 {{$union := . -}}
35 type {{$union.Name}} interface {
36 Tag() uint32
37 Interface() interface{}
38 __Reflect(__SomeUnionReflect)
39 Encode(encoder *bindings.Encoder) error
40 }
41
42 type __{{$union.Name}}Reflect struct {
43 {{- range $field := $union.Fields}}
44 {{$field.Name}} {{$field.Type}}
45 {{- end}}
46 }
47 {{- end -}}
48 `
49
50 const unionFieldDeclTmplText = `
51 {{- define "UnionFieldDecl" -}}
52 {{$field := . -}}
53 {{$unionName := $field.Union.Name -}}
54 type {{$unionName}}{{$field.Name}} struct{ Value {{$field.Type}} }
55 func (u *{{$unionName}}{{$field.Name}}) Tag() uint32 { return {{$field.Tag}} }
56 func (u *{{$unionName}}{{$field.Name}}) Interface() interface{} { return u.Value }
57 func (u *{{$unionName}}{{$field.Name}}) __Reflect(__{{$unionName}}Reflect) {}
58 {{- end -}}
59 `
60
61 const unionFieldEncodeTmplText = `
62 {{- define "UnionFieldEncode" -}}
63 {{$field := . -}}
64 {{$unionName := $field.Union.Name -}}
65 func (u *{{$unionName}}{{$field.Name}}) Encode(encoder *bindings.Encoder) error {
66 encoder.WriteUnionHeader(u.Tag())
67 {{ template "FieldEncodingTmpl" $field.EncodingInfo }}
68
69 encoder.FinishWritingUnionValue()
70 return nil
71 }
72 {{- end -}}
73 `
74
75 const unknownUnionFieldDeclTmplText = `
76 {{- define "UnknownUnionFieldDecl" -}}
77 {{$union := . -}}
78 type {{$union.Name}}Unknown struct { tag uint32 }
79 func (u *{{$union.Name}}Unknown) Tag() uint32 { return u.tag }
80 func (u *{{$union.Name}}Unknown) Interface() interface{} { return nil }
81 func (u *{{$union.Name}}Unknown) __Reflect(__{{$union.Name}}Reflect) {}
82 {{- end -}}
83 `
84
85 const unknownUnionFieldEncodeTmplText = `
86 {{- define "UnknownUnionFieldEncode" -}}
87 {{$union := . -}}
88 func (u *{{$union.Name}}Unknown) Encode(encoder *bindings.Encoder) error {
89 return fmt.Errorf("Trying to serialize an unknown {{$union.Name}}. There is no sane way to do that!");
90 }
91 {{- end -}}
92 `
93
94 const unionFieldDecodeTmplText = `
95 {{- define "UnionFieldDecode" -}}
96 {{$field := . -}}
97 {{$unionName := $field.Union.Name -}}
98 func (u *{{$unionName}}{{$field.Name}}) decodeInternal(decoder *bindings.Decoder ) error {
99 {{ template "FieldDecodingTmpl" $field.EncodingInfo }}
100
101 return nil
102 }
103 {{- end -}}
104 `
105
106 const unionDecodeTmplText = `
107 {{- define "UnionDecode" -}}
108 {{$union := . -}}
109 func Decode{{$union.Name}}(decoder *bindings.Decoder) ({{$union.Name}}, error) {
110 size, tag, err := decoder.ReadUnionHeader()
111 if err != nil {
112 return nil, err
113 }
114
115 if size == 0 {
116 decoder.SkipUnionValue()
117 return nil, nil
118 }
119
120 switch tag {
121 {{- range $field := $union.Fields}}
122 case {{$field.Tag}}:
123 var value {{$union.Name}}{{$field.Name}}
124 if err := value.decodeInternal(decoder); err != nil {
125 return nil, err
126 }
127 decoder.FinishReadingUnionValue()
128 return &value, nil
129 {{- end -}}
130 }
131
132 decoder.SkipUnionValue()
133 return &{{$union.Name}}Unknown{tag: tag}, nil
134 }
135 {{- end -}}
136 `
137
138 func initUnionTemplates() {
139 template.Must(goFileTmpl.Parse(unionTmplText))
140 template.Must(goFileTmpl.Parse(unionInterfaceDeclTmplText))
141 template.Must(goFileTmpl.Parse(unionFieldDeclTmplText))
142 template.Must(goFileTmpl.Parse(unknownUnionFieldDeclTmplText))
143 template.Must(goFileTmpl.Parse(unknownUnionFieldEncodeTmplText))
144 template.Must(goFileTmpl.Parse(unionFieldEncodeTmplText))
145 template.Must(goFileTmpl.Parse(unionFieldDecodeTmplText))
146 template.Must(goFileTmpl.Parse(unionDecodeTmplText))
147 }
OLDNEW
« no previous file with comments | « mojom/generators/go/templates/test_utils.go ('k') | mojom/generators/go/templates/unions_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698