Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup import generation #11440

Merged
merged 1 commit into from Nov 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 25 additions & 37 deletions pkg/codegen/go/gen_program.go
Expand Up @@ -363,7 +363,7 @@ func (g *generator) collectImports(
program *pcl.Program,
stdImports,
pulumiImports,
preambleHelperMethods codegen.StringSet) (codegen.StringSet, codegen.StringSet, codegen.StringSet) {
preambleHelperMethods codegen.StringSet) {
// Accumulate import statements for the various providers
for _, n := range program.Nodes {
if r, isResource := n.(*pcl.Resource); isResource {
Expand Down Expand Up @@ -432,8 +432,6 @@ func (g *generator) collectImports(
})
contract.Assert(len(diags) == 0)
}

return stdImports, pulumiImports, preambleHelperMethods
}

func (g *generator) collectConvertImports(
Expand Down Expand Up @@ -492,48 +490,38 @@ func (g *generator) getGoPackageInfo(pkg string) (GoPackageInfo, bool) {
return info, ok
}

func (g *generator) getPulumiImport(pkg, vPath, mod, name string) string {
info, _ := g.getGoPackageInfo(pkg)
if m, ok := info.ModuleToPackage[mod]; ok {
mod = m
}
func (g *generator) getPulumiImport(pkg, versionPath, mod, name string) string {
// We do this before we let the user set overrides. That way the user can still have a
// module named IndexToken.
info, _ := g.getGoPackageInfo(pkg) // We're allowing `info` to be zero-initialized

imp := fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s/%s", pkg, vPath, pkg, mod)
// namespaceless invokes "aws:index:..."
if mod == "" {
imp = fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s", pkg, vPath, pkg)
}
importPath := func(mod string) string {
importBasePath := info.ImportBasePath
if importBasePath == "" {
importBasePath = fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s", pkg, versionPath, pkg)
}

// All providers don't follow the sdk/go/<package> scheme. Allow ImportBasePath as
// a means to override this assumption.
if info.ImportBasePath != "" {
if mod != "" {
imp = fmt.Sprintf("%s/%s", info.ImportBasePath, mod)
} else {
imp = info.ImportBasePath
if mod != "" && mod != IndexToken {
return fmt.Sprintf("%s/%s", importBasePath, mod)
}
return importBasePath
}

if alias, ok := info.PackageImportAliases[imp]; ok {
return fmt.Sprintf("%s %q", alias, imp)
if m, ok := info.ModuleToPackage[mod]; ok {
mod = m
}

modSplit := strings.Split(mod, "/")
// account for mods like "eks/ClusterVpcConfig" index...
if len(modSplit) > 1 {
if modSplit[0] == "" || modSplit[0] == IndexToken {
imp = fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s", pkg, vPath, pkg)
if info.ImportBasePath != "" {
imp = info.ImportBasePath
}
} else {
imp = fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s/%s", pkg, vPath, pkg, modSplit[0])
if info.ImportBasePath != "" {
imp = fmt.Sprintf("%s/%s", info.ImportBasePath, modSplit[0])
}
}
path := importPath(mod)
if alias, ok := info.PackageImportAliases[path]; ok {
return fmt.Sprintf("%s %q", alias, path)
}
return fmt.Sprintf("%q", imp)

// Trim off anything after the first '/'.
// This handles transforming modules like s3/bucket to s3 (as found in
// aws:s3/bucket:Bucket).
mod = strings.SplitN(mod, "/", 2)[0]

return fmt.Sprintf("%#v", importPath(mod))
}

// genPostamble closes the method
Expand Down