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

Unified Diff: mojom/generators/go/templates/declarations.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
Index: mojom/generators/go/templates/declarations.go
diff --git a/mojom/generators/go/templates/declarations.go b/mojom/generators/go/templates/declarations.go
index c16bdc4b3a121c54274a5b52d16ad55fc37e0b3f..04c3df8772a8e010a64abb61af1a910908f0720f 100644
--- a/mojom/generators/go/templates/declarations.go
+++ b/mojom/generators/go/templates/declarations.go
@@ -14,3 +14,55 @@ type {{$struct.Name}} struct {
}
{{- end -}}
`
+
+const unionDeclTmplText = `
+{{- define "UnionDecl" -}}
+{{- $struct := . }}
+{{ template "UnionInterfaceDecl" $struct }}
+
+{{ template "UnknownUnionFieldDecl" $struct }}
+
+{{- range $field := $struct.Field}}
+{{ template "UnionFieldDecl" $field }}
+{{- end}}
+{{- 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 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 -}}
+`
« no previous file with comments | « no previous file | mojom/generators/go/templates/declarations_test.go » ('j') | mojom/generators/go/translator/translator.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698