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

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

Issue 2082173002: New go generator shakedown. (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 import ( 7 import (
8 "text/template" 8 "text/template"
9 ) 9 )
10 10
11 const ampersandIfNullable = `
12 {{- define "AmpersandIfNullable" -}}
vardhan 2016/06/22 15:17:15 haha
13 {{- $info := . -}}
14 {{- if $info.IsNullable -}}
15 &
16 {{- end -}}
17 {{- end -}}
18 `
19
11 const fieldDecodingTmplText = ` 20 const fieldDecodingTmplText = `
12 {{- define "FieldDecodingTmpl" -}} 21 {{- define "FieldDecodingTmpl" -}}
13 {{- $info := . -}} 22 {{- $info := . -}}
14 {{- if $info.IsPointer -}} 23 {{- if $info.IsPointer -}}
15 pointer, err := decoder.ReadPointer() 24 pointer, err := decoder.ReadPointer()
16 if err != nil { 25 if err != nil {
17 return err 26 return err
18 } 27 }
19 if pointer == 0 { 28 if pointer == 0 {
20 {{- if $info.IsNullable}} 29 {{- if $info.IsNullable}}
21 {{$info.Identifier}} = nil 30 {{$info.Identifier}} = nil
22 {{- else if $info.IsUnion}} 31 {{- else if $info.IsUnion}}
23 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe cted null union pointer"} 32 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe cted null union pointer"}
24 {{- else}} 33 {{- else}}
25 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe cted null pointer"} 34 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe cted null pointer"}
26 {{- end }} 35 {{- end }}
27 } else { 36 } else {
28 {{ template "NonNullableFieldDecodingTmpl" $info }} 37 {{ template "NonNullableFieldDecodingTmpl" $info }}
29 } 38 }
30 {{- else -}} 39 {{- else -}}
31 {{ template "NonNullableFieldDecodingTmpl" $info }} 40 {{ template "NonNullableFieldDecodingTmpl" $info }}
32 {{- end -}} 41 {{- end -}}
33 {{- end -}} 42 {{- end -}}
34 ` 43 `
35 44
36 const nonNullableFieldDecodingTmplText = ` 45 const nonNullableFieldDecodingTmplText = `
37 {{- define "NonNullableFieldDecodingTmpl" -}} 46 {{- define "NonNullableFieldDecodingTmpl" -}}
38 {{- $info := . -}} 47 {{- $info := . -}}
vardhan 2016/06/22 15:17:15 sorry for not mentioning earlier, but it might hel
39 {{- if or $info.IsEnum $info.IsSimple -}} 48 {{- if or $info.IsEnum $info.IsSimple -}}
40 value, err := decoder.{{$info.ReadFunction}}() 49 value, err := decoder.{{$info.ReadFunction}}()
41 if err != nil { 50 if err != nil {
42 return err 51 return err
43 } 52 }
44 {{if $info.IsSimple -}} 53 {{if $info.IsSimple -}}
45 {{$info.Identifier}} = value 54 {{$info.Identifier}} = value
46 {{- else -}} 55 {{- else -}}
47 {{$info.Identifier}} = {{$info.GoType}}(value) 56 {{$info.Identifier}} = {{$info.GoType}}(value)
48 {{- end -}} 57 {{- end -}}
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 return err 90 return err
82 } 91 }
83 {{- if not $info.IsNullable}} 92 {{- if not $info.IsNullable}}
84 if {{$info.Identifier}} == nil { 93 if {{$info.Identifier}} == nil {
85 return &bindings.ValidationError{bindings.UnexpectedNullUnion, "unexpect ed null union"} 94 return &bindings.ValidationError{bindings.UnexpectedNullUnion, "unexpect ed null union"}
86 } 95 }
87 {{- end -}} 96 {{- end -}}
88 {{- if $info.IsPointer}} 97 {{- if $info.IsPointer}}
89 decoder.Finish() 98 decoder.Finish()
90 {{- end -}} 99 {{- end -}}
100 {{- else if $info.IsInterface -}}
101 handle, err := decoder.{{$info.ReadFunction}}()
102 if err != nil {
103 return err
104 }
105 if handle.IsValid() {
106 handleOwner := bindings.NewMessagePipeHandleOwner(handle)
107 {{- if $info.IsInterfaceRequest}}
108 {{$info.Identifier}} = {{template "AmpersandIfNullable" $info}}{{$info.G oType}}_Request{handleOwner}
109 {{- else}}
110 {{$info.Identifier}} = {{template "AmpersandIfNullable" $info}}{{$info.G oType}}_Pointer{handleOwner}
111 {{- end -}}
112 } else {
113 {{- if $info.IsNullable}}
114 {{$info.Identifier}} = nil
115 {{- else}}
116 return &bindings.ValidationError{bindings.UnexpectedInvalidHandle, "unex pected invalid handle"}
117 {{- end -}}
118 }
91 {{- else if $info.IsArray -}} 119 {{- else if $info.IsArray -}}
92 {{ $elInfo := $info.ElementEncodingInfo -}} 120 {{ $elInfo := $info.ElementEncodingInfo -}}
93 len0, err := decoder.StartArray({{$elInfo.BitSize}}) 121 len0, err := decoder.StartArray({{$elInfo.BitSize}})
94 if err != nil { 122 if err != nil {
95 return err 123 return err
96 } 124 }
97 {{$info.Identifier}} = make({{$info.GoType}}, len0) 125 {{$info.Identifier}} = make({{$info.GoType}}, len0)
98 for i := uint32(0); i < len0; i++ { 126 for i := uint32(0); i < len0; i++ {
99 {{ template "FieldDecodingTmpl" $elInfo }} 127 {{ template "FieldDecodingTmpl" $elInfo }}
100 {{$info.Identifier}}[i] = {{$elInfo.Identifier}} 128 {{$info.Identifier}}[i] = {{$elInfo.Identifier}}
(...skipping 29 matching lines...) Expand all
130 for i := 0; i < len({{$keyInfo.Identifier}}); i++ { 158 for i := 0; i < len({{$keyInfo.Identifier}}); i++ {
131 (*{{$info.Identifier}})[{{$keyInfo.Identifier}}[i]] = {{$valueInfo.Ident ifier}}[i] 159 (*{{$info.Identifier}})[{{$keyInfo.Identifier}}[i]] = {{$valueInfo.Ident ifier}}[i]
132 } 160 }
133 {{- end -}} 161 {{- end -}}
134 {{- end -}} 162 {{- end -}}
135 ` 163 `
136 164
137 func initDecodingTemplates() { 165 func initDecodingTemplates() {
138 template.Must(goFileTmpl.Parse(nonNullableFieldDecodingTmplText)) 166 template.Must(goFileTmpl.Parse(nonNullableFieldDecodingTmplText))
139 template.Must(goFileTmpl.Parse(fieldDecodingTmplText)) 167 template.Must(goFileTmpl.Parse(fieldDecodingTmplText))
168 template.Must(goFileTmpl.Parse(ampersandIfNullable))
140 } 169 }
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/mojom_tool/bin/mac64/generators/go.sha1 ('k') | mojom/generators/go/templates/decoding_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698