Skip to content

Commit 11d4115

Browse files
committed
Control the exact type name and not prefix
1 parent 0730846 commit 11d4115

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

pkg/tfbridge/info/info.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,13 @@ type Schema struct {
509509
// whether or not to treat this property as secret
510510
Secret *bool
511511

512-
// If specified, resets the type name prefix for any types that Pulumi needs to generate to represent the schema
513-
// of the current property. Normally the names for helper object types are built from concatenating fragments
514-
// representing the path to the type in the schema. The default strategy can lead to unacceptably long type
515-
// names, reducing the SDK usability. Using TypePrefixOverride allows the maintainer to get shorter type names.
516-
TypePrefixOverride *string
512+
// Specifies the exact name to use for the generated type.
513+
//
514+
// When generating types for properties, by default Pulumi picks reasonable names based on the property path
515+
// prefix and the name of the property. Use [TypeName] to override this decision when the default names for
516+
// nested properties are too long or otherwise undesirable. The choice will further affect the automatically
517+
// generated names for any properties nested under the current one.
518+
TypeName *string
517519
}
518520

519521
// Config represents a synthetic configuration variable that is Pulumi-only, and not passed to Terraform.

pkg/tfgen/generate.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ type propertyType struct {
343343
altTypes []tokens.Type
344344
asset *tfbridge.AssetTranslation
345345

346-
typePrefixOverride *string
346+
typeName *string
347347
}
348348

349349
func (g *Generator) Sink() diag.Sink {
@@ -356,7 +356,7 @@ func (g *Generator) makePropertyType(typePath paths.TypePath,
356356

357357
t := &propertyType{}
358358
if info != nil {
359-
t.typePrefixOverride = info.TypePrefixOverride
359+
t.typeName = info.TypeName
360360
}
361361

362362
var elemInfo *tfbridge.SchemaInfo
@@ -472,7 +472,7 @@ func (g *Generator) makeObjectPropertyType(typePath paths.TypePath,
472472
}
473473

474474
if info != nil {
475-
t.typePrefixOverride = info.TypePrefixOverride
475+
t.typeName = info.TypeName
476476
}
477477

478478
if info != nil {
@@ -565,12 +565,12 @@ func (t *propertyType) equals(other *propertyType) bool {
565565
return false
566566
}
567567
switch {
568-
case t.typePrefixOverride != nil && other.typePrefixOverride == nil:
568+
case t.typeName != nil && other.typeName == nil:
569569
return false
570-
case t.typePrefixOverride == nil && other.typePrefixOverride != nil:
570+
case t.typeName == nil && other.typeName != nil:
571571
return false
572-
case t.typePrefixOverride != nil && other.typePrefixOverride != nil &&
573-
*t.typePrefixOverride != *other.typePrefixOverride:
572+
case t.typeName != nil && other.typeName != nil &&
573+
*t.typeName != *other.typeName:
574574
return false
575575
}
576576
for i, p := range t.properties {

pkg/tfgen/generate_schema.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,16 @@ type declarer interface {
119119
func (nt *schemaNestedTypes) declareType(typePath paths.TypePath, declarer declarer, namePrefix, name string,
120120
typ *propertyType, isInput bool) string {
121121

122-
if typ.typePrefixOverride != nil {
123-
namePrefix = *typ.typePrefixOverride
124-
}
125-
126122
// Generate a name for this nested type.
127-
typeName := namePrefix + cases.Title(language.Und, cases.NoLower).String(name)
123+
var typeName string
124+
125+
if typ.typeName != nil {
126+
// Use an explicit name if provided.
127+
typeName = *typ.typeName
128+
} else {
129+
// Otherwise build one based on the current property name and prefix.
130+
typeName = namePrefix + cases.Title(language.Und, cases.NoLower).String(name)
131+
}
128132

129133
// Override the nested type name, if necessary.
130134
if typ.nestedType.Name().String() != "" {

pkg/tfgen/generate_schema_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestCSharpMiniRandom(t *testing.T) {
124124
// Test the ability to force type sharing. Some of the upstream providers generate very large concrete schemata by in
125125
// Go, with TF not being materially affected. The example is inspired by QuickSight types in AWS. In Pulumi the default
126126
// projection is going to generate named types for every instance of the shared schema. This may lead to SDK bloat. Test
127-
// the ability of the provider author to curb the bloat and force an explit sharing.
127+
// the ability of the provider author to curb the bloat and force an explicit sharing.
128128
func TestTypeSharing(t *testing.T) {
129129
if runtime.GOOS == "windows" {
130130
t.Skipf("Skipping on Windows due to a test setup issue")
@@ -224,7 +224,7 @@ func TestTypeSharing(t *testing.T) {
224224
Fields: map[string]*info.Schema{
225225
"visuals": {
226226
Elem: &info.Schema{
227-
TypePrefixOverride: tfbridge.StringRef(""),
227+
TypeName: tfbridge.StringRef("Visual"),
228228
},
229229
},
230230
},

0 commit comments

Comments
 (0)