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

Fix no longer being able to place types in top-level modules #1300 #1301

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions pkg/tfgen/generate_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,14 +1002,15 @@ func modulePlacementForType(pkg tokens.Package, path paths.TypePath) tokens.Modu
// root module).
return res.Token().Module()
}
// Supplementary types are defined one level up from
// the module defining the resource.
return parentModule(res.Token().Module())
// Supplementary types are typically defined one level up from the module defining the resource, but may
// also be defined in the same module.
m := res.Token().Module()
return parentModuleOrSelf(m)
case *paths.DataSourceMemberPath:
dataSourceModule := pp.DataSourcePath.Token().Module()
// Supplementary types are defined one level up from
// the module defining the data source.
return parentModule(dataSourceModule)
// Supplementary types are typically defined one level up from the module defining the data source, but
// may also be defined in the same module.
m := pp.DataSourcePath.Token().Module()
return parentModuleOrSelf(m)
case *paths.ConfigPath:
return tokens.NewModuleToken(pkg, configMod)
default:
Expand All @@ -1018,8 +1019,18 @@ func modulePlacementForType(pkg tokens.Package, path paths.TypePath) tokens.Modu
}
}

func parentModule(m tokens.Module) tokens.Module {
return tokens.NewModuleToken(m.Package(), parentModuleName(m.Name()))
func parentModuleOrSelf(self tokens.Module) tokens.Module {
if m, ok := parentModule(self); ok {
return m
}
return self
}

func parentModule(m tokens.Module) (tokens.Module, bool) {
if !strings.Contains(string(m.Name()), tokens.QNameDelimiter) {
return "", false
}
return tokens.NewModuleToken(m.Package(), parentModuleName(m.Name())), true
}

func parentModuleName(m tokens.ModuleName) tokens.ModuleName {
Expand Down
83 changes: 83 additions & 0 deletions pkg/tfgen/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package tfgen

import (
"fmt"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -281,3 +282,85 @@ func Test_ProviderWithObjectTypesInConfigCanGenerateRenames(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "foo_bar", r.Renames.RenamedProperties["test:index/ProviderProp:ProviderProp"]["fooBar"])
}

func TestModulePlacementForType(t *testing.T) {
t.Parallel()

type testCase struct {
pkg tokens.Package
path paths.TypePath
expect tokens.Module
}

prop := paths.PropertyName{Key: "prop", Name: "prop"}

testCases := []testCase{
// Resource in top-level module mymod is placed to myprov:mymod.
{
"myprov",
paths.NewProperyPath(paths.NewResourcePath(
"myprov_myres",
"myprov:mymod:MyRes",
false, /*isProvider*/
).Inputs(), prop),
"myprov:mymod",
},
// Resource in second-level module mymod is placed to parent myprov:mymod.
{
"myprov",
paths.NewProperyPath(paths.NewResourcePath(
"myprov_myres",
"myprov:mymod/mysubmod:MyRes",
false, /*isProvider*/
).Inputs(), prop),
"myprov:mymod",
},
// Resource in third-level module mymod is placed to parent myprov:mymod/mysubmod.
{
"myprov",
paths.NewProperyPath(paths.NewResourcePath(
"myprov_myres",
"myprov:mymod/mysubmod/mysubsubmod:MyRes",
false, /*isProvider*/
).Inputs(), prop),
"myprov:mymod/mysubmod",
},
// Datasource in top-level module mymod is placed to myprov:mymod.
{
"myprov",
paths.NewProperyPath(paths.NewDataSourcePath(
"myprov_myds",
"myprov:mymod:MyFn",
).Args(), prop),
"myprov:mymod",
},
// Datasource in second-level module mymod is placed to parent myprov:mymod.
{
"myprov",
paths.NewProperyPath(paths.NewDataSourcePath(
"myprov_myds",
"myprov:mymod/mysubmod:MyFn",
).Args(), prop),
"myprov:mymod",
},
// Datasource in third-level module mymod is placed to parent myprov:mymod/mysubmod.
{
"myprov",
paths.NewProperyPath(paths.NewDataSourcePath(
"myprov_ds",
"myprov:mymod/mysubmod/mysubsubmod:MyFn",
).Args(), prop),
"myprov:mymod/mysubmod",
},
}

for i, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("testCase-%d", i), func(t *testing.T) {
t.Parallel()
mod := modulePlacementForType(tc.pkg, tc.path)
assert.Equal(t, tc.expect, mod)
})
}

}
Loading