Skip to content

Commit

Permalink
Extract alias generation to standalone method
Browse files Browse the repository at this point in the history
Separate alias logic from main genResources method.
  • Loading branch information
danielrbradley committed Jan 17, 2025
1 parent c097acc commit a813e27
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions provider/pkg/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,17 +695,9 @@ type packageGenerator struct {

func (g *packageGenerator) genResources(typeName string, resource *openapi.ResourceSpec, nestedResourceBodyRefs []string) error {
// Resource names should consistently start with upper case.
typeNameAliases := []string{}
typeNameAliases := g.genAliases(typeName)
titleCasedTypeName := strings.Title(typeName)

if g.majorVersion < 3 {
// We need to alias the previous, lowercase name so users can upgrade to v2 without replacement.
// These aliases aren't required anymore with v3.
if titleCasedTypeName != typeName {
typeNameAliases = append(typeNameAliases, typeName)
}
}

// A single API path can be modelled as several resources if it accepts a polymorphic payload:
// i.e., when the request body is a discriminated union type of several object types. Pulumi
// schema doesn't support polymorphic (OneOf) resources, so the provider creates a separate resource
Expand Down Expand Up @@ -737,6 +729,20 @@ func (g *packageGenerator) genResources(typeName string, resource *openapi.Resou
return g.genResourceVariant(resource, mainResource, nestedResourceBodyRefs, typeNameAliases...)
}

func (g *packageGenerator) genAliases(typeName string) []string {
switch g.majorVersion {
case 2:
// We need to alias the previous, lowercase name so users can upgrade to v2 without replacement.
// These aliases aren't required anymore with v3.
if strings.Title(typeName) != typeName {
return []string{typeName}
}
case 3:
// TODO: Add additional aliases for v3 case changes: https://github.com/pulumi/pulumi-azure-native/issues/3848
}
return nil
}

// resourceVariant points to request body's and response's schemas of a resource which is one of the variants
// of a resource related to an API path.
type resourceVariant struct {
Expand Down

0 comments on commit a813e27

Please sign in to comment.