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

Side by Side Diff: mojom/generators/go/templates/encoding.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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package templates 5 package templates
6 6
7 const structEncodingTmplText = ` 7 import (
8 {{- define "StructEncodingTmpl" -}} 8 » "text/template"
9 {{ $struct := . }} 9 )
10 func (s *{{$struct.Name}}) Encode(encoder *bindings.Encoder) error {
11 » encoder.StartStruct({{$struct.CurVersionSize}}, {{$struct.CurVersionNumb er}})
12 » {{- range $field := $struct.Fields}}
13 » » {{ template "FieldEncodingTmpl" $field.EncodingInfo }}
14 » {{- end}}
15
16 » if err := encoder.Finish(); err != nil {
17 » » return err
18 » }
19 » return nil
20 }
21 {{- end -}}
22 `
23 10
24 const fieldEncodingTmplText = ` 11 const fieldEncodingTmplText = `
25 {{- define "FieldEncodingTmpl" -}} 12 {{- define "FieldEncodingTmpl" -}}
26 {{- $info := . -}} 13 {{- $info := . -}}
27 {{- if $info.IsNullable -}} 14 {{- if $info.IsNullable -}}
28 if {{$info.Identifier}} == nil { 15 if {{$info.Identifier}} == nil {
29 {{- if $info.IsPointer -}} 16 {{- if $info.IsPointer -}}
30 encoder.WriteNullPointer() 17 encoder.WriteNullPointer()
31 {{- else if $info.IsHandle -}} 18 {{- else if $info.IsHandle -}}
32 encoder.WriteInvalidHandle() 19 encoder.WriteInvalidHandle()
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 } 80 }
94 {{ template "FieldEncodingTmpl" $keyInfo }} 81 {{ template "FieldEncodingTmpl" $keyInfo }}
95 {{ template "FieldEncodingTmpl" $valueInfo }} 82 {{ template "FieldEncodingTmpl" $valueInfo }}
96 } 83 }
97 if err := encoder.Finish(); err != nil { 84 if err := encoder.Finish(); err != nil {
98 return err 85 return err
99 } 86 }
100 {{- end -}} 87 {{- end -}}
101 {{- end -}} 88 {{- end -}}
102 ` 89 `
90
91 func initEncodingTemplates() {
92 template.Must(goFileTmpl.Parse(nonNullableFieldEncodingTmplText))
93 template.Must(goFileTmpl.Parse(fieldEncodingTmplText))
94 }
OLDNEW
« no previous file with comments | « mojom/generators/go/templates/decoding_test.go ('k') | mojom/generators/go/templates/encoding_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698