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

Unified Diff: mojom/generators/go/templates/unions.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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojom/generators/go/templates/test_utils.go ('k') | mojom/generators/go/templates/unions_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/generators/go/templates/unions.go
diff --git a/mojom/generators/go/templates/unions.go b/mojom/generators/go/templates/unions.go
new file mode 100644
index 0000000000000000000000000000000000000000..176b215df6e55e4cec0f679e8bb616e0a880882c
--- /dev/null
+++ b/mojom/generators/go/templates/unions.go
@@ -0,0 +1,147 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package templates
+
+import (
+ "text/template"
+)
+
+const unionTmplText = `
+{{- define "Union" -}}
+{{- $union := . }}
+{{ template "UnionInterfaceDecl" $union }}
+
+{{ template "UnknownUnionFieldDecl" $union }}
+{{ template "UnknownUnionFieldEncode" $union }}
+
+{{- range $field := $union.Field}}
+{{ template "UnionFieldDecl" $field }}
+
+{{ template "UnionFieldEncode" $field }}
+
+{{ template "UnionFieldDecode" $field }}
+{{- end}}
+
+{{ template "UnionDecode" $union }}
+
+{{- end -}}
+`
+
+const unionInterfaceDeclTmplText = `
+{{- define "UnionInterfaceDecl" -}}
+{{$union := . -}}
+type {{$union.Name}} interface {
+ Tag() uint32
+ Interface() interface{}
+ __Reflect(__SomeUnionReflect)
+ Encode(encoder *bindings.Encoder) error
+}
+
+type __{{$union.Name}}Reflect struct {
+{{- range $field := $union.Fields}}
+ {{$field.Name}} {{$field.Type}}
+{{- end}}
+}
+{{- end -}}
+`
+
+const unionFieldDeclTmplText = `
+{{- define "UnionFieldDecl" -}}
+{{$field := . -}}
+{{$unionName := $field.Union.Name -}}
+type {{$unionName}}{{$field.Name}} struct{ Value {{$field.Type}} }
+func (u *{{$unionName}}{{$field.Name}}) Tag() uint32 { return {{$field.Tag}} }
+func (u *{{$unionName}}{{$field.Name}}) Interface() interface{} { return u.Value }
+func (u *{{$unionName}}{{$field.Name}}) __Reflect(__{{$unionName}}Reflect) {}
+{{- end -}}
+`
+
+const unionFieldEncodeTmplText = `
+{{- define "UnionFieldEncode" -}}
+{{$field := . -}}
+{{$unionName := $field.Union.Name -}}
+func (u *{{$unionName}}{{$field.Name}}) Encode(encoder *bindings.Encoder) error {
+ encoder.WriteUnionHeader(u.Tag())
+ {{ template "FieldEncodingTmpl" $field.EncodingInfo }}
+
+ encoder.FinishWritingUnionValue()
+ return nil
+}
+{{- end -}}
+`
+
+const unknownUnionFieldDeclTmplText = `
+{{- define "UnknownUnionFieldDecl" -}}
+{{$union := . -}}
+type {{$union.Name}}Unknown struct { tag uint32 }
+func (u *{{$union.Name}}Unknown) Tag() uint32 { return u.tag }
+func (u *{{$union.Name}}Unknown) Interface() interface{} { return nil }
+func (u *{{$union.Name}}Unknown) __Reflect(__{{$union.Name}}Reflect) {}
+{{- end -}}
+`
+
+const unknownUnionFieldEncodeTmplText = `
+{{- define "UnknownUnionFieldEncode" -}}
+{{$union := . -}}
+func (u *{{$union.Name}}Unknown) Encode(encoder *bindings.Encoder) error {
+ return fmt.Errorf("Trying to serialize an unknown {{$union.Name}}. There is no sane way to do that!");
+}
+{{- end -}}
+`
+
+const unionFieldDecodeTmplText = `
+{{- define "UnionFieldDecode" -}}
+{{$field := . -}}
+{{$unionName := $field.Union.Name -}}
+func (u *{{$unionName}}{{$field.Name}}) decodeInternal(decoder *bindings.Decoder) error {
+ {{ template "FieldDecodingTmpl" $field.EncodingInfo }}
+
+ return nil
+}
+{{- end -}}
+`
+
+const unionDecodeTmplText = `
+{{- define "UnionDecode" -}}
+{{$union := . -}}
+func Decode{{$union.Name}}(decoder *bindings.Decoder) ({{$union.Name}}, error) {
+ size, tag, err := decoder.ReadUnionHeader()
+ if err != nil {
+ return nil, err
+ }
+
+ if size == 0 {
+ decoder.SkipUnionValue()
+ return nil, nil
+ }
+
+ switch tag {
+{{- range $field := $union.Fields}}
+ case {{$field.Tag}}:
+ var value {{$union.Name}}{{$field.Name}}
+ if err := value.decodeInternal(decoder); err != nil {
+ return nil, err
+ }
+ decoder.FinishReadingUnionValue()
+ return &value, nil
+{{- end -}}
+ }
+
+ decoder.SkipUnionValue()
+ return &{{$union.Name}}Unknown{tag: tag}, nil
+}
+{{- end -}}
+`
+
+func initUnionTemplates() {
+ template.Must(goFileTmpl.Parse(unionTmplText))
+ template.Must(goFileTmpl.Parse(unionInterfaceDeclTmplText))
+ template.Must(goFileTmpl.Parse(unionFieldDeclTmplText))
+ template.Must(goFileTmpl.Parse(unknownUnionFieldDeclTmplText))
+ template.Must(goFileTmpl.Parse(unknownUnionFieldEncodeTmplText))
+ template.Must(goFileTmpl.Parse(unionFieldEncodeTmplText))
+ template.Must(goFileTmpl.Parse(unionFieldDecodeTmplText))
+ template.Must(goFileTmpl.Parse(unionDecodeTmplText))
+}
« no previous file with comments | « mojom/generators/go/templates/test_utils.go ('k') | mojom/generators/go/templates/unions_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698