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

Side by Side Diff: mojom/generators/go/translator/names.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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package translator 5 package translator
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "path/filepath" 9 "path/filepath"
10 "strings" 10 "strings"
(...skipping 18 matching lines...) Expand all
29 if e.Value.DeclData.ContainerTypeKey != nil { 29 if e.Value.DeclData.ContainerTypeKey != nil {
30 containerName := t.goTypeName(*e.Value.DeclData.Containe rTypeKey) 30 containerName := t.goTypeName(*e.Value.DeclData.Containe rTypeKey)
31 goType = fmt.Sprintf("%s_%s", containerName, goType) 31 goType = fmt.Sprintf("%s_%s", containerName, goType)
32 } 32 }
33 } 33 }
34 34
35 t.goTypeCache[typeKey] = goType 35 t.goTypeCache[typeKey] = goType
36 return goType 36 return goType
37 } 37 }
38 38
39 func (t *translator) goConstName(constKey string) (name string) {
40 if cachedName, ok := t.goConstNameCache[constKey]; ok {
41 return cachedName
42 }
43
44 declaredConstant := t.fileGraph.ResolvedConstants[constKey]
45
46 name = formatName(*declaredConstant.DeclData.ShortName)
47 if declaredConstant.DeclData.ContainerTypeKey != nil {
48 containerName := t.goTypeName(*declaredConstant.DeclData.Contain erTypeKey)
49 name = fmt.Sprintf("%s_%s", containerName, name)
50 }
51
52 return
53 }
54
39 func fileNameToPackageName(fileName string) string { 55 func fileNameToPackageName(fileName string) string {
40 base := filepath.Base(fileName) 56 base := filepath.Base(fileName)
41 ext := filepath.Ext(base) 57 ext := filepath.Ext(base)
42 return base[:len(base)-len(ext)] 58 return base[:len(base)-len(ext)]
43 } 59 }
44 60
61 func (t *translator) importMojomFile(fileName string) {
62 pkgName := fileNameToPackageName(fileName)
63 pkgPath, err := filepath.Rel(t.Config.SrcRootPath(), fileName)
64 if err != nil {
65 panic(err.Error())
66 }
67 pkgPath = pkgPath[:len(pkgPath)-len(filepath.Ext(pkgPath))]
68 t.imports[pkgName] = pkgPath
69 }
70
45 func userDefinedTypeDeclData(userDefinedType mojom_types.UserDefinedType) *mojom _types.DeclarationData { 71 func userDefinedTypeDeclData(userDefinedType mojom_types.UserDefinedType) *mojom _types.DeclarationData {
46 switch u := userDefinedType.(type) { 72 switch u := userDefinedType.(type) {
47 case *mojom_types.UserDefinedTypeEnumType: 73 case *mojom_types.UserDefinedTypeEnumType:
48 return u.Value.DeclData 74 return u.Value.DeclData
49 case *mojom_types.UserDefinedTypeStructType: 75 case *mojom_types.UserDefinedTypeStructType:
50 return u.Value.DeclData 76 return u.Value.DeclData
51 case *mojom_types.UserDefinedTypeUnionType: 77 case *mojom_types.UserDefinedTypeUnionType:
52 return u.Value.DeclData 78 return u.Value.DeclData
53 case *mojom_types.UserDefinedTypeInterfaceType: 79 case *mojom_types.UserDefinedTypeInterfaceType:
54 return u.Value.DeclData 80 return u.Value.DeclData
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 } 123 }
98 124
99 for i := range parts { 125 for i := range parts {
100 part := strings.Title(strings.ToLower(parts[i])) 126 part := strings.Title(strings.ToLower(parts[i]))
101 _, size := utf8.DecodeRuneInString(part) 127 _, size := utf8.DecodeRuneInString(part)
102 parts[i] = strings.ToUpper(part[:size]) + part[size:] 128 parts[i] = strings.ToUpper(part[:size]) + part[size:]
103 } 129 }
104 130
105 return strings.Join(parts, "") 131 return strings.Join(parts, "")
106 } 132 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698