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

Unified Diff: mojom/generators/go/templates/structs.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/encoding_test.go ('k') | mojom/generators/go/templates/structs_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/generators/go/templates/structs.go
diff --git a/mojom/generators/go/templates/structs.go b/mojom/generators/go/templates/structs.go
new file mode 100644
index 0000000000000000000000000000000000000000..9320185a8466cecbffbaabca95599f54df2ddac8
--- /dev/null
+++ b/mojom/generators/go/templates/structs.go
@@ -0,0 +1,101 @@
+// 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 structTmplText = `
+{{- define "Struct" -}}
+{{$struct := . -}}
+{{ template "StructDecl" $struct }}
+
+{{ template "StructEncodingTmpl" $struct }}
+
+{{ template "StructVersions" $struct }}
+
+{{ template "StructDecodingTmpl" $struct }}
+{{- end -}}
+`
+
+const structDeclTmplText = `
+{{- define "StructDecl" -}}
+{{$struct := . -}}
+type {{$struct.Name}} struct {
+{{- range $field := $struct.Fields}}
+ {{$field.Name}} {{$field.Type}}
+{{- end}}
+}
+{{- end -}}
+`
+
+const structEncodingTmplText = `
+{{- define "StructEncodingTmpl" -}}
+{{ $struct := . }}
+func (s *{{$struct.Name}}) Encode(encoder *bindings.Encoder) error {
+ encoder.StartStruct({{$struct.CurVersionSize}}, {{$struct.CurVersionNumber}})
+ {{- range $field := $struct.Fields}}
+ {{ template "FieldEncodingTmpl" $field.EncodingInfo }}
+ {{- end}}
+
+ if err := encoder.Finish(); err != nil {
+ return err
+ }
+ return nil
+}
+{{- end -}}
+`
+
+const structVersions = `
+{{- define "StructVersions" -}}
+{{- $struct := . -}}
+var {{$struct.PrivateName}}_Versions []bindings.DataHeader = []bindings.DataHeader{
+ {{- range $version := $struct.Versions}}
+ bindings.DataHeader{ {{$version.NumBytes}}, {{$version.Version}} },
+ {{- end}}
+}
+{{- end -}}
+`
+
+const structDecodingTmplText = `
+{{- define "StructDecodingTmpl" -}}
+{{- $struct := . -}}
+func (s *{{$struct.Name}}) Decode(decoder *bindings.Decoder) error {
+ header, err := decoder.StartStruct()
+ if err != nil {
+ return err
+ }
+
+ index := sort.Search(len({{$struct.PrivateName}}_Versions), func(i int) bool {
+ return {{$struct.PrivateName}}_Versions[i].ElementsOrVersion >= header.ElementsOrVersion
+ })
+ if index < len({{$struct.PrivateName}}_Versions) {
+ if {{$struct.PrivateName}}_Versions[index].ElementsOrVersion > header.ElementsOrVersion {
+ index--
+ }
+ expectedSize := {{$struct.PrivateName}}_Versions[index].Size
+ if expectedSize != header.Size {
+ return &bindings.ValidationError{bindings.UnexpectedStructHeader,
+ fmt.Sprintf("invalid struct header size: should be %d, but was %d", expectedSize, header.Size),
+ }
+ }
+ }
+
+ {{- range $field := $struct.Fields}}
+ if header.ElementsOrVersion >= {{$field.MinVersion}} {
+ {{ template "FieldDecodingTmpl" $field.EncodingInfo }}
+ }
+ {{- end}}
+}
+{{- end -}}
+`
+
+func initStructTemplates() {
+ template.Must(goFileTmpl.Parse(structEncodingTmplText))
+ template.Must(goFileTmpl.Parse(structDeclTmplText))
+ template.Must(goFileTmpl.Parse(structVersions))
+ template.Must(goFileTmpl.Parse(structDecodingTmplText))
+}
« no previous file with comments | « mojom/generators/go/templates/encoding_test.go ('k') | mojom/generators/go/templates/structs_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698