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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/generators/go/templates/encoding.go
diff --git a/mojom/generators/go/templates/encoding.go b/mojom/generators/go/templates/encoding.go
index 56c6235cd8573c8705f623c4aa2c134132d4a945..df63d819903c226e427b6664b9192e766eecd58e 100644
--- a/mojom/generators/go/templates/encoding.go
+++ b/mojom/generators/go/templates/encoding.go
@@ -11,7 +11,7 @@ import (
const fieldEncodingTmplText = `
{{- define "FieldEncodingTmpl" -}}
{{- $info := . -}}
-{{- if $info.IsNullable -}}
+{{- if and (not $info.IsUnion) $info.IsNullable -}}
if {{$info.Identifier}} == nil {
{{- if $info.IsPointer -}}
encoder.WriteNullPointer()
@@ -51,6 +51,21 @@ if err := encoder.WriteHandle({{$info.Identifier}}); err != nil {
if err := {{$info.Identifier}}.Encode(encoder); err != nil {
return err
}
+{{- else if $info.IsUnion -}}
+{{- if $info.IsPointer -}}
+encoder.StartNestedUnion()
+{{end -}}
+if {{$info.Identifier}} == nil {
+{{- if $info.IsNullable -}}
+ encoder.WriteNullUnion()
+{{- else -}}
+ return &bindings.ValidationError{bindings.UnexpectedNullUnion, "unexpected null union"}
+{{- end -}}
+} else {
+ if err := {{$info.Identifier}}.Encode(encoder); err != nil {
+ return err
+ }
+}
{{- else if $info.IsArray -}}
{{ $elInfo := $info.ElementEncodingInfo -}}
encoder.StartArray(uint32(len({{$info.Identifier}})), {{$elInfo.BitSize}})
« 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