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

Side by Side Diff: mojom/generators/go/templates/structs.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 structTmplText = `
12 {{- define "Struct" -}}
13 {{$struct := . -}}
14 {{ template "StructDecl" $struct }}
15
16 {{ template "StructEncodingTmpl" $struct }}
17
18 {{ template "StructVersions" $struct }}
19
20 {{ template "StructDecodingTmpl" $struct }}
21 {{- end -}}
22 `
23
24 const structDeclTmplText = `
25 {{- define "StructDecl" -}}
26 {{$struct := . -}}
27 type {{$struct.Name}} struct {
28 {{- range $field := $struct.Fields}}
29 {{$field.Name}} {{$field.Type}}
30 {{- end}}
31 }
32 {{- end -}}
33 `
34
35 const structEncodingTmplText = `
36 {{- define "StructEncodingTmpl" -}}
37 {{ $struct := . }}
38 func (s *{{$struct.Name}}) Encode(encoder *bindings.Encoder) error {
39 encoder.StartStruct({{$struct.CurVersionSize}}, {{$struct.CurVersionNumb er}})
40 {{- range $field := $struct.Fields}}
41 {{ template "FieldEncodingTmpl" $field.EncodingInfo }}
42 {{- end}}
43
44 if err := encoder.Finish(); err != nil {
45 return err
46 }
47 return nil
48 }
49 {{- end -}}
50 `
51
52 const structVersions = `
53 {{- define "StructVersions" -}}
54 {{- $struct := . -}}
55 var {{$struct.PrivateName}}_Versions []bindings.DataHeader = []bindings.DataHead er{
56 {{- range $version := $struct.Versions}}
57 bindings.DataHeader{ {{$version.NumBytes}}, {{$version.Version}} },
58 {{- end}}
59 }
60 {{- end -}}
61 `
62
63 const structDecodingTmplText = `
64 {{- define "StructDecodingTmpl" -}}
65 {{- $struct := . -}}
66 func (s *{{$struct.Name}}) Decode(decoder *bindings.Decoder) error {
67 header, err := decoder.StartStruct()
68 if err != nil {
69 return err
70 }
71
72 index := sort.Search(len({{$struct.PrivateName}}_Versions), func(i int) bool {
73 return {{$struct.PrivateName}}_Versions[i].ElementsOrVersion >= header.ElementsOrVersion
74 })
75 if index < len({{$struct.PrivateName}}_Versions) {
76 if {{$struct.PrivateName}}_Versions[index].ElementsOrVersion > h eader.ElementsOrVersion {
77 index--
78 }
79 expectedSize := {{$struct.PrivateName}}_Versions[index].Size
80 if expectedSize != header.Size {
81 return &bindings.ValidationError{bindings.UnexpectedStru ctHeader,
82 fmt.Sprintf("invalid struct header size: should be %d, but was %d", expectedSize, header.Size),
83 }
84 }
85 }
86
87 {{- range $field := $struct.Fields}}
88 if header.ElementsOrVersion >= {{$field.MinVersion}} {
89 {{ template "FieldDecodingTmpl" $field.EncodingInfo }}
90 }
91 {{- end}}
92 }
93 {{- end -}}
94 `
95
96 func initStructTemplates() {
97 template.Must(goFileTmpl.Parse(structEncodingTmplText))
98 template.Must(goFileTmpl.Parse(structDeclTmplText))
99 template.Must(goFileTmpl.Parse(structVersions))
100 template.Must(goFileTmpl.Parse(structDecodingTmplText))
101 }
OLDNEW
« no previous file with comments | « mojom/generators/go/templates/encoding_test.go ('k') | mojom/generators/go/templates/structs_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698