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

Unified Diff: mojom/generators/go/templates/decoding.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/declarations_test.go ('k') | mojom/generators/go/templates/decoding_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/generators/go/templates/decoding.go
diff --git a/mojom/generators/go/templates/decoding.go b/mojom/generators/go/templates/decoding.go
index 7033bbf7a7e94ba9abd8f493ce26c39c00f77bfb..ff9361a4ca740b5294e89af81e8d1d47c331267a 100644
--- a/mojom/generators/go/templates/decoding.go
+++ b/mojom/generators/go/templates/decoding.go
@@ -4,49 +4,9 @@
package templates
-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 -}}
-`
+import (
+ "text/template"
+)
const fieldDecodingTmplText = `
{{- define "FieldDecodingTmpl" -}}
@@ -148,3 +108,8 @@ for i := 0; i < len({{$keyInfo.Identifier}}); i++ {
{{- end -}}
{{- end -}}
`
+
+func initDecodingTemplates() {
+ template.Must(goFileTmpl.Parse(nonNullableFieldDecodingTmplText))
+ template.Must(goFileTmpl.Parse(fieldDecodingTmplText))
+}
« no previous file with comments | « mojom/generators/go/templates/declarations_test.go ('k') | mojom/generators/go/templates/decoding_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698