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

Unified Diff: mojom/generators/go/translator/translator.go

Issue 2233963003: Add support for constants in the new go generator. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Merge branch 'master' of github.com:domokit/mojo into const Created 4 years, 4 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/translator/translator.go
diff --git a/mojom/generators/go/translator/translator.go b/mojom/generators/go/translator/translator.go
index 5679e384d982626bbf56935c0cdb4db7c02627b6..711a017989e7cac43bcc5bf2cda5a2216c3a8b82 100644
--- a/mojom/generators/go/translator/translator.go
+++ b/mojom/generators/go/translator/translator.go
@@ -21,17 +21,19 @@ type Translator interface {
type translator struct {
fileGraph *mojom_files.MojomFileGraph
// goTypeCache maps type keys to go type strings.
- goTypeCache map[string]string
- imports map[string]string
- currentFileName string
- pkgName string
- Config common.GeneratorConfig
+ goTypeCache map[string]string
+ goConstNameCache map[string]string
+ imports map[string]string
+ currentFileName string
+ pkgName string
+ Config common.GeneratorConfig
}
func NewTranslator(fileGraph *mojom_files.MojomFileGraph) (t *translator) {
t = new(translator)
t.fileGraph = fileGraph
t.goTypeCache = map[string]string{}
+ t.goConstNameCache = map[string]string{}
t.imports = map[string]string{}
return t
}
@@ -94,6 +96,25 @@ func (t *translator) TranslateMojomFile(fileName string) (tmplFile *TmplFile) {
}
}
+ if file.DeclaredMojomObjects.TopLevelConstants == nil {
+ file.DeclaredMojomObjects.TopLevelConstants = &[]string{}
+ }
+ if file.DeclaredMojomObjects.EmbeddedConstants == nil {
+ file.DeclaredMojomObjects.EmbeddedConstants = &[]string{}
+ }
+ for _, constKey := range *file.DeclaredMojomObjects.TopLevelConstants {
+ c := t.translateDeclaredConstant(constKey)
+ if c != nil {
+ tmplFile.Constants = append(tmplFile.Constants, c)
+ }
+ }
+ for _, constKey := range *file.DeclaredMojomObjects.EmbeddedConstants {
+ c := t.translateDeclaredConstant(constKey)
+ if c != nil {
+ tmplFile.Constants = append(tmplFile.Constants, c)
+ }
+ }
+
tmplFile.Imports = []Import{}
tmplFile.MojomImports = []string{}
@@ -299,6 +320,20 @@ func (t *translator) translateMojomMethod(mojomMethod mojom_types.MojomMethod, i
return m
}
+func (t *translator) translateDeclaredConstant(constKey string) (c *ConstantTemplate) {
+ declaredConstant := t.fileGraph.ResolvedConstants[constKey]
+ resolvedValue := t.resolveConstRef(declaredConstant.Value)
+ if _, ok := resolvedValue.(*mojom_types.ValueBuiltinValue); ok {
+ // There is no way to have a constant NaN, or infinity in go.
+ return nil
+ }
+ c = new(ConstantTemplate)
+ c.Name = t.goConstName(constKey)
+ c.Type = t.translateType(declaredConstant.Type)
+ c.Value = t.translateValue(declaredConstant.Value)
+ return c
+}
+
////////////////////////////////////////////////////////////////////////////////
// Implements sort.Interface.

Powered by Google App Engine
This is Rietveld 408576698