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

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

Issue 2061243002: Encode and decode fields of union type. (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
« no previous file with comments | « no previous file | mojom/generators/go/templates/decoding_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 fieldDecodingTmplText = ` 11 const fieldDecodingTmplText = `
12 {{- define "FieldDecodingTmpl" -}} 12 {{- define "FieldDecodingTmpl" -}}
13 {{- $info := . -}} 13 {{- $info := . -}}
14 {{- if $info.IsPointer -}} 14 {{- if $info.IsPointer -}}
15 pointer, err := decoder.ReadPointer() 15 pointer, err := decoder.ReadPointer()
16 if err != nil { 16 if err != nil {
17 return err 17 return err
18 } 18 }
19 if pointer == 0 { 19 if pointer == 0 {
20 {{- if $info.IsNullable }} 20 {{- if $info.IsNullable}}
21 {{$info.Identifier}} = nil 21 {{$info.Identifier}} = nil
22 {{- else }} 22 {{- else if $info.IsUnion}}
23 » return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe cted null union pointer"}
24 {{- else}}
23 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe cted null pointer"} 25 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe cted null pointer"}
24 {{- end }} 26 {{- end }}
25 } else { 27 } else {
26 {{ template "NonNullableFieldDecodingTmpl" $info }} 28 {{ template "NonNullableFieldDecodingTmpl" $info }}
27 } 29 }
28 {{- else -}} 30 {{- else -}}
29 {{ template "NonNullableFieldDecodingTmpl" $info }} 31 {{ template "NonNullableFieldDecodingTmpl" $info }}
30 {{- end -}} 32 {{- end -}}
31 {{- end -}} 33 {{- end -}}
32 ` 34 `
(...skipping 23 matching lines...) Expand all
56 return &bindings.ValidationError{bindings.UnexpectedInvalidHandle, "unex pected invalid handle"} 58 return &bindings.ValidationError{bindings.UnexpectedInvalidHandle, "unex pected invalid handle"}
57 {{- end -}} 59 {{- end -}}
58 } 60 }
59 {{- else if $info.IsStruct -}} 61 {{- else if $info.IsStruct -}}
60 {{- if $info.IsNullable -}} 62 {{- if $info.IsNullable -}}
61 {{$info.Identifier}} = new({{$info.GoType}}) 63 {{$info.Identifier}} = new({{$info.GoType}})
62 {{end -}} 64 {{end -}}
63 if err := {{$info.Identifier}}.Decode(decoder); err != nil { 65 if err := {{$info.Identifier}}.Decode(decoder); err != nil {
64 return err 66 return err
65 } 67 }
68 {{- else if $info.IsUnion -}}
69 {{- if $info.IsPointer -}}
70 if err := decoder.StartNestedUnion(); err != nil {
71 return err
72 }
73 {{end -}}
74 var err error
75 {{$info.Identifier}}, err = Decode{{$info.GoType}}(decoder)
76 if err != nil {
77 return err
78 }
79 {{- if not $info.IsNullable}}
80 if {{$info.Identifier}} == nil {
81 return &bindings.ValidationError{bindings.UnexpectedNullUnion, "unexpect ed null union"}
82 }
83 {{- end -}}
84 {{- if $info.IsPointer}}
85 decoder.Finish()
86 {{- end -}}
66 {{- else if $info.IsArray -}} 87 {{- else if $info.IsArray -}}
67 {{ $elInfo := $info.ElementEncodingInfo -}} 88 {{ $elInfo := $info.ElementEncodingInfo -}}
68 len0, err := decoder.StartArray({{$elInfo.BitSize}}) 89 len0, err := decoder.StartArray({{$elInfo.BitSize}})
69 if err != nil { 90 if err != nil {
70 return err 91 return err
71 } 92 }
72 {{$info.Identifier}} = make({{$info.GoType}}, len0) 93 {{$info.Identifier}} = make({{$info.GoType}}, len0)
73 for i := uint32(0); i < len0; i++ { 94 for i := uint32(0); i < len0; i++ {
74 {{ template "FieldDecodingTmpl" $elInfo }} 95 {{ template "FieldDecodingTmpl" $elInfo }}
75 {{$info.Identifier}}[i] = {{$elInfo.Identifier}} 96 {{$info.Identifier}}[i] = {{$elInfo.Identifier}}
(...skipping 30 matching lines...) Expand all
106 (*{{$info.Identifier}})[{{$keyInfo.Identifier}}[i]] = {{$valueInfo.Ident ifier}}[i] 127 (*{{$info.Identifier}})[{{$keyInfo.Identifier}}[i]] = {{$valueInfo.Ident ifier}}[i]
107 } 128 }
108 {{- end -}} 129 {{- end -}}
109 {{- end -}} 130 {{- end -}}
110 ` 131 `
111 132
112 func initDecodingTemplates() { 133 func initDecodingTemplates() {
113 template.Must(goFileTmpl.Parse(nonNullableFieldDecodingTmplText)) 134 template.Must(goFileTmpl.Parse(nonNullableFieldDecodingTmplText))
114 template.Must(goFileTmpl.Parse(fieldDecodingTmplText)) 135 template.Must(goFileTmpl.Parse(fieldDecodingTmplText))
115 } 136 }
OLDNEW
« no previous file with comments | « no previous file | mojom/generators/go/templates/decoding_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698