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

Side by Side Diff: mojom/generators/go/templates/decoding.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 structVersions = ` 7 import (
8 {{- define "StructVersions" -}} 8 » "text/template"
9 {{- $struct := . -}} 9 )
10 var {{$struct.PrivateName}}_Versions []bindings.DataHeader = []bindings.DataHead er{
11 » {{- range $version := $struct.Versions}}
12 » bindings.DataHeader{ {{$version.NumBytes}}, {{$version.Version}} },
13 » {{- end}}
14 }
15 {{- end -}}
16 `
17
18 const structDecodingTmplText = `
19 {{- define "StructDecodingTmpl" -}}
20 {{- $struct := . -}}
21 func (s *{{$struct.Name}}) Decode(decoder *bindings.Decoder) error {
22 » header, err := decoder.StartStruct()
23 » if err != nil {
24 » » return err
25 » }
26
27 » index := sort.Search(len({{$struct.PrivateName}}_Versions), func(i int) bool {
28 » » return {{$struct.PrivateName}}_Versions[i].ElementsOrVersion >= header.ElementsOrVersion
29 » })
30 » if index < len({{$struct.PrivateName}}_Versions) {
31 » » if {{$struct.PrivateName}}_Versions[index].ElementsOrVersion > h eader.ElementsOrVersion {
32 » » » index--
33 » » }
34 » » expectedSize := {{$struct.PrivateName}}_Versions[index].Size
35 » » if expectedSize != header.Size {
36 » » » return &bindings.ValidationError{bindings.UnexpectedStru ctHeader,
37 » » » » fmt.Sprintf("invalid struct header size: should be %d, but was %d", expectedSize, header.Size),
38 » » » }
39 » » }
40 » }
41
42 » {{- range $field := $struct.Fields}}
43 » if header.ElementsOrVersion >= {{$field.MinVersion}} {
44 » » {{ template "FieldDecodingTmpl" $field.EncodingInfo }}
45 » }
46 » {{- end}}
47 }
48 {{- end -}}
49 `
50 10
51 const fieldDecodingTmplText = ` 11 const fieldDecodingTmplText = `
52 {{- define "FieldDecodingTmpl" -}} 12 {{- define "FieldDecodingTmpl" -}}
53 {{- $info := . -}} 13 {{- $info := . -}}
54 {{- if $info.IsPointer -}} 14 {{- if $info.IsPointer -}}
55 pointer, err := decoder.ReadPointer() 15 pointer, err := decoder.ReadPointer()
56 if err != nil { 16 if err != nil {
57 return err 17 return err
58 } 18 }
59 if pointer == 0 { 19 if pointer == 0 {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 return &bindings.ValidationError{bindings.DifferentSizedArraysInMap, 101 return &bindings.ValidationError{bindings.DifferentSizedArraysInMap,
142 fmt.Sprintf("Number of keys %d is different from number of value s %d", 102 fmt.Sprintf("Number of keys %d is different from number of value s %d",
143 len({{$keyInfo.Identifier}}), len({{$valueInfo.Identifier}}))} 103 len({{$keyInfo.Identifier}}), len({{$valueInfo.Identifier}}))}
144 } 104 }
145 for i := 0; i < len({{$keyInfo.Identifier}}); i++ { 105 for i := 0; i < len({{$keyInfo.Identifier}}); i++ {
146 (*{{$info.Identifier}})[{{$keyInfo.Identifier}}[i]] = {{$valueInfo.Ident ifier}}[i] 106 (*{{$info.Identifier}})[{{$keyInfo.Identifier}}[i]] = {{$valueInfo.Ident ifier}}[i]
147 } 107 }
148 {{- end -}} 108 {{- end -}}
149 {{- end -}} 109 {{- end -}}
150 ` 110 `
111
112 func initDecodingTemplates() {
113 template.Must(goFileTmpl.Parse(nonNullableFieldDecodingTmplText))
114 template.Must(goFileTmpl.Parse(fieldDecodingTmplText))
115 }
OLDNEW
« no previous file with comments | « mojom/generators/go/templates/declarations_test.go ('k') | mojom/generators/go/templates/decoding_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698