Skip to content

Commit

Permalink
Merge #11606
Browse files Browse the repository at this point in the history
11606: Remove .NET type imports and add a test for #11467 r=iwahbe a=iwahbe

This is follow up for #11467. It adjusts a SDK generation test to include the new feature. I also remove the resource import code for .NET, since it broke the test and we use absolute paths at this point.

Co-authored-by: Ian Wahbe <ian@wahbe.com>
  • Loading branch information
bors[bot] and iwahbe committed Dec 9, 2022
2 parents c11a8eb + 7467b16 commit a5e7c3a
Show file tree
Hide file tree
Showing 34 changed files with 2,380 additions and 143 deletions.
@@ -1,4 +1,4 @@
changes:
- type: fix
scope: sdkgen/nodejs
description: Fix NodeJS SDK when a component is using another component from the same schema as a property
- type: fix
scope: sdkgen/nodejs,dotnet
description: Fix imports when a component is using another component from the same schema as a property
137 changes: 0 additions & 137 deletions pkg/codegen/dotnet/gen.go
Expand Up @@ -27,7 +27,6 @@ import (
"path"
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
"unicode"
Expand Down Expand Up @@ -1304,8 +1303,6 @@ func (mod *modContext) genResource(w io.Writer, r *schema.Resource) error {
}

func (mod *modContext) genFunctionFileCode(f *schema.Function) (string, error) {
imports := map[string]codegen.StringSet{}
mod.getImports(f, imports)
buffer := &bytes.Buffer{}
importStrings := mod.pulumiImports()

Expand All @@ -1315,9 +1312,6 @@ func (mod *modContext) genFunctionFileCode(f *schema.Function) (string, error) {
if nonStandardNamespace {
importStrings = append(importStrings, mod.namespaceName)
}
for _, i := range imports {
importStrings = append(importStrings, i.SortedValues()...)
}

// We need to qualify input types when we are not in the same module as them.
if nonStandardNamespace {
Expand Down Expand Up @@ -1679,128 +1673,6 @@ func (mod *modContext) pulumiImports() []string {
return pulumiImports
}

func (mod *modContext) getTypeImports(t schema.Type, recurse bool, imports map[string]codegen.StringSet, seen codegen.Set) {
mod.getTypeImportsForResource(t, recurse, imports, seen, nil)
}

func (mod *modContext) getTypeImportsForResource(t schema.Type, recurse bool, imports map[string]codegen.StringSet, seen codegen.Set, res *schema.Resource) {
if seen.Has(t) {
return
}
seen.Add(t)

switch t := t.(type) {
case *schema.OptionalType:
mod.getTypeImports(t.ElementType, recurse, imports, seen)
return
case *schema.InputType:
mod.getTypeImports(t.ElementType, recurse, imports, seen)
return
case *schema.ArrayType:
mod.getTypeImports(t.ElementType, recurse, imports, seen)
return
case *schema.MapType:
mod.getTypeImports(t.ElementType, recurse, imports, seen)
return
case *schema.ObjectType:
for _, p := range t.Properties {
mod.getTypeImports(p.Type, recurse, imports, seen)
}
return
case *schema.ResourceType:
// If it's an external resource, we'll be using fully-qualified type names, so there's no need
// for an import.
if t.Resource != nil && !codegen.PkgEquals(t.Resource.PackageReference, mod.pkg) {
return
}

// Don't import itself.
if t.Resource == res {
return
}

modName, name, modPath := mod.pkg.TokenToModule(t.Token), tokenToName(t.Token), ""
if modName != mod.mod {
mp, err := filepath.Rel(mod.mod, modName)
contract.Assert(err == nil)
if path.Base(mp) == "." {
mp = path.Dir(mp)
}
modPath = filepath.ToSlash(mp)
}
if len(modPath) == 0 {
return
}
if imports[modPath] == nil {
imports[modPath] = codegen.NewStringSet()
}
imports[modPath].Add(name)
return
case *schema.TokenType:
return
case *schema.UnionType:
for _, e := range t.ElementTypes {
mod.getTypeImports(e, recurse, imports, seen)
}
return
default:
return
}
}

func (mod *modContext) getImports(member interface{}, imports map[string]codegen.StringSet) {
mod.getImportsForResource(member, imports, nil)
}

func (mod *modContext) getImportsForResource(member interface{}, imports map[string]codegen.StringSet, res *schema.Resource) {
seen := codegen.Set{}
switch member := member.(type) {
case *schema.ObjectType:
for _, p := range member.Properties {
mod.getTypeImports(p.Type, true, imports, seen)
}
return
case *schema.ResourceType:
mod.getTypeImports(member, true, imports, seen)
return
case *schema.Resource:
for _, p := range member.Properties {
mod.getTypeImportsForResource(p.Type, false, imports, seen, res)
}
for _, p := range member.InputProperties {
mod.getTypeImportsForResource(p.Type, false, imports, seen, res)
}
for _, method := range member.Methods {
if method.Function.Inputs != nil {
for _, p := range method.Function.Inputs.Properties {
mod.getTypeImportsForResource(p.Type, false, imports, seen, res)
}
}
if method.Function.Outputs != nil {
for _, p := range method.Function.Outputs.Properties {
mod.getTypeImportsForResource(p.Type, false, imports, seen, res)
}
}
}
return
case *schema.Function:
if member.Inputs != nil {
mod.getTypeImports(member.Inputs, false, imports, seen)
}
if member.Outputs != nil {
mod.getTypeImports(member.Outputs, false, imports, seen)
}
return
case []*schema.Property:
for _, p := range member {
mod.getTypeImports(p.Type, false, imports, seen)
}
return
default:
return
}
}

func (mod *modContext) genHeader(w io.Writer, using []string) {
fmt.Fprintf(w, "// *** WARNING: this file was generated by %v. ***\n", mod.tool)
fmt.Fprintf(w, "// *** Do not edit by hand unless you're certain you know what you are doing! ***\n")
Expand Down Expand Up @@ -2045,17 +1917,8 @@ func (mod *modContext) gen(fs codegen.Fs) error {
continue
}

imports := map[string]codegen.StringSet{}
mod.getImportsForResource(r, imports, r)

buffer := &bytes.Buffer{}
var additionalImports []string
for _, i := range imports {
additionalImports = append(additionalImports, i.SortedValues()...)
}
sort.Strings(additionalImports)
importStrings := mod.pulumiImports()
importStrings = append(importStrings, additionalImports...)
mod.genHeader(buffer, importStrings)

if err := mod.genResource(buffer, r); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/testing/test/sdk_driver.go
Expand Up @@ -84,7 +84,7 @@ var allLanguages = codegen.NewStringSet("python/any", "nodejs/any", "dotnet/any"
var PulumiPulumiSDKTests = []*SDKTest{
{
Directory: "naming-collisions",
Description: "Schema with types that could potentially produce collisions (go).",
Description: "Schema with types that could potentially produce collisions.",
},
{
Directory: "dash-named-schema",
Expand Down
@@ -1,6 +1,6 @@
---
title: "example"
title_tag: "example.example"
title_tag: "example Package"
meta_desc: ""
layout: api
no_edit_this_page: true
Expand All @@ -11,8 +11,14 @@ no_edit_this_page: true



<h2 id="modules">Modules</h2>
<ul class="api">
<li><a href="mod/" title="mod"><span class="api-symbol api-symbol--module"></span>mod</a></li>
</ul>

<h2 id="resources">Resources</h2>
<ul class="api">
<li><a href="maincomponent/" title="MainComponent"><span class="api-symbol api-symbol--resource"></span>MainComponent</a></li>
<li><a href="provider/" title="Provider"><span class="api-symbol api-symbol--resource"></span>Provider</a></li>
<li><a href="resource/" title="Resource"><span class="api-symbol api-symbol--resource"></span>Resource</a></li>
<li><a href="resourceinput/" title="ResourceInput"><span class="api-symbol api-symbol--resource"></span>ResourceInput</a></li>
Expand Down
@@ -1,6 +1,10 @@
{
"emittedFiles": [
"_index.md",
"maincomponent/_index.md",
"mod/_index.md",
"mod/component/_index.md",
"mod/component2/_index.md",
"provider/_index.md",
"resource/_index.md",
"resourceinput/_index.md"
Expand Down

0 comments on commit a5e7c3a

Please sign in to comment.