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

Side by Side Diff: mojom/generators/go/templates/encoding.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
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 fieldEncodingTmplText = ` 11 const fieldEncodingTmplText = `
12 {{- define "FieldEncodingTmpl" -}} 12 {{- define "FieldEncodingTmpl" -}}
13 {{- $info := . -}} 13 {{- $info := . -}}
14 {{- if $info.IsNullable -}} 14 {{- if and (not $info.IsUnion) $info.IsNullable -}}
15 if {{$info.Identifier}} == nil { 15 if {{$info.Identifier}} == nil {
16 {{- if $info.IsPointer -}} 16 {{- if $info.IsPointer -}}
17 encoder.WriteNullPointer() 17 encoder.WriteNullPointer()
18 {{- else if $info.IsHandle -}} 18 {{- else if $info.IsHandle -}}
19 encoder.WriteInvalidHandle() 19 encoder.WriteInvalidHandle()
20 {{- end -}} 20 {{- end -}}
21 } else { 21 } else {
22 {{ template "NonNullableFieldEncodingTmpl" $info }} 22 {{ template "NonNullableFieldEncodingTmpl" $info }}
23 } 23 }
24 {{- else -}} 24 {{- else -}}
(...skipping 19 matching lines...) Expand all
44 if err := encoder.WriteHandle(*({{$info.Identifier}})); err != nil { 44 if err := encoder.WriteHandle(*({{$info.Identifier}})); err != nil {
45 {{- else -}} 45 {{- else -}}
46 if err := encoder.WriteHandle({{$info.Identifier}}); err != nil { 46 if err := encoder.WriteHandle({{$info.Identifier}}); err != nil {
47 {{- end -}} 47 {{- end -}}
48 return err 48 return err
49 } 49 }
50 {{- else if $info.IsStruct -}} 50 {{- else if $info.IsStruct -}}
51 if err := {{$info.Identifier}}.Encode(encoder); err != nil { 51 if err := {{$info.Identifier}}.Encode(encoder); err != nil {
52 return err 52 return err
53 } 53 }
54 {{- else if $info.IsUnion -}}
55 {{- if $info.IsPointer -}}
56 encoder.StartNestedUnion()
57 {{end -}}
58 if {{$info.Identifier}} == nil {
59 {{- if $info.IsNullable -}}
60 encoder.WriteNullUnion()
61 {{- else -}}
62 return &bindings.ValidationError{bindings.UnexpectedNullUnion, "unexpect ed null union"}
63 {{- end -}}
64 } else {
65 if err := {{$info.Identifier}}.Encode(encoder); err != nil {
66 return err
67 }
68 }
54 {{- else if $info.IsArray -}} 69 {{- else if $info.IsArray -}}
55 {{ $elInfo := $info.ElementEncodingInfo -}} 70 {{ $elInfo := $info.ElementEncodingInfo -}}
56 encoder.StartArray(uint32(len({{$info.Identifier}})), {{$elInfo.BitSize}}) 71 encoder.StartArray(uint32(len({{$info.Identifier}})), {{$elInfo.BitSize}})
57 for _, {{$elInfo.Identifier}} := range {{$info.Identifier}} { 72 for _, {{$elInfo.Identifier}} := range {{$info.Identifier}} {
58 {{ template "FieldEncodingTmpl" $elInfo }} 73 {{ template "FieldEncodingTmpl" $elInfo }}
59 } 74 }
60 if err := encoder.Finish(); err != nil { 75 if err := encoder.Finish(); err != nil {
61 return err 76 return err
62 } 77 }
63 {{- else if $info.IsMap -}} 78 {{- else if $info.IsMap -}}
(...skipping 21 matching lines...) Expand all
85 return err 100 return err
86 } 101 }
87 {{- end -}} 102 {{- end -}}
88 {{- end -}} 103 {{- end -}}
89 ` 104 `
90 105
91 func initEncodingTemplates() { 106 func initEncodingTemplates() {
92 template.Must(goFileTmpl.Parse(nonNullableFieldEncodingTmplText)) 107 template.Must(goFileTmpl.Parse(nonNullableFieldEncodingTmplText))
93 template.Must(goFileTmpl.Parse(fieldEncodingTmplText)) 108 template.Must(goFileTmpl.Parse(fieldEncodingTmplText))
94 } 109 }
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