[codegen/{go,nodejs,python}] Normalize modules. (#5006)

In general, a package/module name in these targets is derived from the
module portion of a type token. If the type token is not already in an
expected form--namely, all lowercase--the generated package/module names
will also be in unexpected forms. These changes normalize the module
names to lowercase s.t. the generated package/module names conform to
expectations.
This commit is contained in:
Pat Gavlin 2020-07-14 10:58:29 -07:00 committed by GitHub
parent 8baee06e30
commit a19843f433
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 34 deletions

View file

@ -92,6 +92,14 @@ func camel(s string) string {
return string(res)
}
func tokenToPackage(pkg *schema.Package, overrides map[string]string, tok string) string {
mod := pkg.TokenToModule(tok)
if override, ok := overrides[mod]; ok {
mod = override
}
return strings.ToLower(mod)
}
type pkgContext struct {
pkg *schema.Package
mod string
@ -120,6 +128,10 @@ func (pkg *pkgContext) details(t *schema.ObjectType) *typeDetails {
return details
}
func (pkg *pkgContext) tokenToPackage(tok string) string {
return tokenToPackage(pkg.pkg, pkg.modToPkg, tok)
}
func (pkg *pkgContext) tokenToType(tok string) string {
// token := pkg : module : member
// module := path/to/module
@ -133,14 +145,13 @@ func (pkg *pkgContext) tokenToType(tok string) string {
panic(fmt.Errorf("pkg.pkg is nil. token %s", tok))
}
mod, name := pkg.pkg.TokenToModule(tok), components[2]
if override, ok := pkg.modToPkg[mod]; ok {
mod = override
}
mod, name := pkg.tokenToPackage(tok), components[2]
// If the package containing the type's token already has a resource with the
// same name, add a `Type` suffix.
modPkg := pkg.getPkg(mod)
modPkg, ok := pkg.packages[mod]
contract.Assert(ok)
name = Title(name)
if modPkg.names.has(name) {
name += "Type"
@ -924,10 +935,7 @@ func (pkg *pkgContext) getTypeImports(t schema.Type, recurse bool, imports strin
case *schema.MapType:
pkg.getTypeImports(t.ElementType, recurse, imports, seen)
case *schema.ObjectType:
mod := pkg.pkg.TokenToModule(t.Token)
if override, ok := pkg.modToPkg[mod]; ok {
mod = override
}
mod := pkg.tokenToPackage(t.Token)
if mod != pkg.mod {
imports.add(path.Join(pkg.importBasePath, mod))
}
@ -1074,25 +1082,10 @@ func (pkg *pkgContext) genConfig(w io.Writer, variables []*schema.Property) erro
return nil
}
func (pkg *pkgContext) getPkg(mod string) *pkgContext {
if override, ok := pkg.modToPkg[mod]; ok {
mod = override
}
pack, ok := pkg.packages[mod]
if !ok {
return nil
}
return pack
}
// generatePackageContextMap groups resources, types, and functions into Go packages.
func generatePackageContextMap(tool string, pkg *schema.Package, goInfo GoPackageInfo) map[string]*pkgContext {
packages := map[string]*pkgContext{}
getPkg := func(mod string) *pkgContext {
if override, ok := goInfo.ModuleToPackage[mod]; ok {
mod = override
}
pack, ok := packages[mod]
if !ok {
pack = &pkgContext{
@ -1113,7 +1106,7 @@ func generatePackageContextMap(tool string, pkg *schema.Package, goInfo GoPackag
}
getPkgFromToken := func(token string) *pkgContext {
return getPkg(pkg.TokenToModule(token))
return getPkg(tokenToPackage(pkg, goInfo.ModuleToPackage, token))
}
if len(pkg.Config) > 0 {

View file

@ -1054,8 +1054,10 @@ func (mod *modContext) isReservedSourceFileName(name string) bool {
func (mod *modContext) gen(fs fs) error {
files := append([]string(nil), mod.extraSourceFiles...)
modDir := strings.ToLower(mod.mod)
addFile := func(name, contents string) {
p := path.Join(mod.mod, name)
p := path.Join(modDir, name)
files = append(files, p)
fs.add(p, []byte(contents))
}
@ -1066,7 +1068,7 @@ func (mod *modContext) gen(fs fs) error {
buffer := &bytes.Buffer{}
mod.genHeader(buffer, nil, nil)
fmt.Fprintf(buffer, "%s", utilitiesFile)
fs.add(path.Join(mod.mod, "utilities.ts"), buffer.Bytes())
fs.add(path.Join(modDir, "utilities.ts"), buffer.Bytes())
// Ensure that the top-level (provider) module directory contains a README.md file.
readme := mod.pkg.Language["nodejs"].(NodePackageInfo).Readme
@ -1085,7 +1087,7 @@ func (mod *modContext) gen(fs fs) error {
if readme != "" && readme[len(readme)-1] != '\n' {
readme += "\n"
}
fs.add(path.Join(mod.mod, "README.md"), []byte(readme))
fs.add(path.Join(modDir, "README.md"), []byte(readme))
case "config":
if len(mod.pkg.Config) > 0 {
buffer := &bytes.Buffer{}
@ -1135,12 +1137,12 @@ func (mod *modContext) gen(fs fs) error {
// Nested types
if len(mod.types) > 0 {
input, output := mod.genTypes()
fs.add(path.Join(mod.mod, "input.ts"), []byte(input))
fs.add(path.Join(mod.mod, "output.ts"), []byte(output))
fs.add(path.Join(modDir, "input.ts"), []byte(input))
fs.add(path.Join(modDir, "output.ts"), []byte(output))
}
// Index
fs.add(path.Join(mod.mod, "index.ts"), []byte(mod.genIndex(files)))
fs.add(path.Join(modDir, "index.ts"), []byte(mod.genIndex(files)))
return nil
}
@ -1151,10 +1153,11 @@ func (mod *modContext) genIndex(exports []string) string {
// Export anything flatly that is a direct export rather than sub-module.
if len(exports) > 0 {
modDir := strings.ToLower(mod.mod)
fmt.Fprintf(w, "// Export members:\n")
sort.Strings(exports)
for _, exp := range exports {
rel, err := filepath.Rel(mod.mod, exp)
rel, err := filepath.Rel(modDir, exp)
contract.Assert(err == nil)
if path.Base(rel) == "." {
rel = path.Dir(rel)
@ -1166,7 +1169,7 @@ func (mod *modContext) genIndex(exports []string) string {
children := codegen.NewStringSet()
for _, mod := range mod.children {
child := mod.mod
child := strings.ToLower(mod.mod)
if mod.compatibility == kubernetes20 {
// Extract version suffix from child modules. Nested versions will have their own index.ts file.
// Example: apps/v1beta1 -> v1beta1

View file

@ -1347,7 +1347,7 @@ func generateModuleContextMap(tool string, pkg *schema.Package, info PackageInfo
getModFromToken := func(token string) *modContext {
canonicalModName := pkg.TokenToModule(token)
modName := PyName(canonicalModName)
modName := PyName(strings.ToLower(canonicalModName))
if override, ok := info.ModuleNameOverrides[canonicalModName]; ok {
modName = override
}