From 26d31750a6b664f4b1ed14834477ac7ac1933538 Mon Sep 17 00:00:00 2001 From: Bevan Arps Date: Fri, 10 Nov 2023 15:46:32 +1300 Subject: [PATCH] Add configuration to rename types (#3466) * Add configuration * Use new configuration * Add documentation to azure-arm.yaml --- v2/azure-arm.yaml | 12 +++++++++++ .../codegen/pipeline/apply_export_filters.go | 16 +++++++++++++-- .../config/object_model_configuration.go | 20 ++++++++++--------- .../internal/config/type_configuration.go | 20 ++++++++++++++++++- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/v2/azure-arm.yaml b/v2/azure-arm.yaml index 6db4907b493..a8b9e2f5df8 100644 --- a/v2/azure-arm.yaml +++ b/v2/azure-arm.yaml @@ -729,6 +729,18 @@ status: # Establishes a connection between the two types allowing for proper # forward and backward conversion. # +# $renameTo: +# Changes the name of any type, allowing selective adjustments to make +# names more idiomatic for Go and/or Kubernetes use. +# +# For resource types, prefer $exportAs +# Note: using $export: + $renameTo: for a resource is not quite the same as +# using $exportAs because the renaming happens at a different stage of the +# pipeline. +# +# If you have two names differing only by letter case, use a TypeTransform +# to selectively rename one of the types. +# # $supportedFrom: # Gives the version number of the first release of ASO that provides # support for this resource. diff --git a/v2/tools/generator/internal/codegen/pipeline/apply_export_filters.go b/v2/tools/generator/internal/codegen/pipeline/apply_export_filters.go index a0377267389..a648a93c928 100644 --- a/v2/tools/generator/internal/codegen/pipeline/apply_export_filters.go +++ b/v2/tools/generator/internal/codegen/pipeline/apply_export_filters.go @@ -72,9 +72,17 @@ func filterTypes( // Find and apply renames renames := make(astmodel.TypeAssociation) for n := range typesToExport { + newName := "" if as, asErr := configuration.ObjectModelConfiguration.ExportAs.Lookup(n); asErr == nil { - configuration.ObjectModelConfiguration.AddTypeAlias(n, as) - renames[n] = n.WithName(as) + newName = as + } else if to, toErr := configuration.ObjectModelConfiguration.RenameTo.Lookup(n); toErr == nil { + newName = to + } + + if newName != "" { + // Add an alias to the configuration so that we can use the new name to access the rest of the config + configuration.ObjectModelConfiguration.AddTypeAlias(n, newName) + renames[n] = n.WithName(newName) } } @@ -86,6 +94,10 @@ func filterTypes( return nil, err } + if err = configuration.ObjectModelConfiguration.RenameTo.VerifyConsumed(); err != nil { + return nil, err + } + // Now apply all the renames renamingVisitor := astmodel.NewRenamingVisitor(renames) result, err := renamingVisitor.RenameAll(typesToExport) diff --git a/v2/tools/generator/internal/config/object_model_configuration.go b/v2/tools/generator/internal/config/object_model_configuration.go index dc4287804d3..e7dccda1187 100644 --- a/v2/tools/generator/internal/config/object_model_configuration.go +++ b/v2/tools/generator/internal/config/object_model_configuration.go @@ -34,15 +34,15 @@ type ObjectModelConfiguration struct { PayloadType groupAccess[PayloadType] // Type access fields here (alphabetical, please) - AzureGeneratedSecrets typeAccess[[]string] - DefaultAzureName typeAccess[bool] - Export typeAccess[bool] - ExportAs typeAccess[string] - GeneratedConfigs typeAccess[map[string]string] - Importable typeAccess[bool] - IsResource typeAccess[bool] - ManualConfigs typeAccess[[]string] - + AzureGeneratedSecrets typeAccess[[]string] + DefaultAzureName typeAccess[bool] + Export typeAccess[bool] + ExportAs typeAccess[string] + GeneratedConfigs typeAccess[map[string]string] + Importable typeAccess[bool] + IsResource typeAccess[bool] + ManualConfigs typeAccess[[]string] + RenameTo typeAccess[string] ResourceEmbeddedInParent typeAccess[string] SupportedFrom typeAccess[string] TypeNameInNextVersion typeAccess[string] @@ -97,6 +97,8 @@ func NewObjectModelConfiguration() *ObjectModelConfiguration { result, func(c *TypeConfiguration) *configurable[bool] { return &c.IsResource }) result.ManualConfigs = makeTypeAccess[[]string]( result, func(c *TypeConfiguration) *configurable[[]string] { return &c.ManualConfigs }) + result.RenameTo = makeTypeAccess[string]( + result, func(c *TypeConfiguration) *configurable[string] { return &c.RenameTo }) result.ResourceEmbeddedInParent = makeTypeAccess[string]( result, func(c *TypeConfiguration) *configurable[string] { return &c.ResourceEmbeddedInParent }) result.SupportedFrom = makeTypeAccess[string]( diff --git a/v2/tools/generator/internal/config/type_configuration.go b/v2/tools/generator/internal/config/type_configuration.go index 1f231f901c9..b00f5e53806 100644 --- a/v2/tools/generator/internal/config/type_configuration.go +++ b/v2/tools/generator/internal/config/type_configuration.go @@ -39,6 +39,7 @@ type TypeConfiguration struct { IsResource configurable[bool] ManualConfigs configurable[[]string] NameInNextVersion configurable[string] + RenameTo configurable[string] ResourceEmbeddedInParent configurable[string] SupportedFrom configurable[string] } @@ -53,6 +54,7 @@ const ( isResourceTag = "$isResource" // Boolean specifying whether a particular type is a resource or not. nameInNextVersionTag = "$nameInNextVersion" // String specifying a type or property name change in the next version supportedFromTag = "$supportedFrom" // Label specifying the first ASO release supporting the resource + renameTo = "$renameTo" // String specifying the new name of a type resourceEmbeddedInParentTag = "$resourceEmbeddedInParent" // String specifying resource name of parent defaultAzureNameTag = "$defaultAzureName" // Boolean indicating if the resource should automatically default AzureName ) @@ -73,6 +75,7 @@ func NewTypeConfiguration(name string) *TypeConfiguration { GeneratedConfigs: makeConfigurable[map[string]string](generatedConfigsTag, scope), ManualConfigs: makeConfigurable[[]string](manualConfigsTag, scope), NameInNextVersion: makeConfigurable[string](nameInNextVersionTag, scope), + RenameTo: makeConfigurable[string](renameTo, scope), ResourceEmbeddedInParent: makeConfigurable[string](resourceEmbeddedInParentTag, scope), SupportedFrom: makeConfigurable[string](supportedFromTag, scope), } @@ -227,7 +230,7 @@ func (tc *TypeConfiguration) UnmarshalYAML(value *yaml.Node) error { continue } - // $AzureGeneratedSecrets: + // $azureGeneratedSecrets: // - secret1 // - secret2 if strings.EqualFold(lastId, azureGeneratedSecretsTag) && c.Kind == yaml.SequenceNode { @@ -248,6 +251,9 @@ func (tc *TypeConfiguration) UnmarshalYAML(value *yaml.Node) error { continue } + // $manualConfigs + // - config1 + // - config2 if strings.EqualFold(lastId, manualConfigsTag) && c.Kind == yaml.SequenceNode { var manualAzureGeneratedConfigs []string for _, content := range c.Content { @@ -272,6 +278,18 @@ func (tc *TypeConfiguration) UnmarshalYAML(value *yaml.Node) error { continue } + // $renameTo: + if strings.EqualFold(lastId, renameTo) && c.Kind == yaml.ScalarNode { + var renameTo string + err := c.Decode(&renameTo) + if err != nil { + return errors.Wrapf(err, "decoding %s", renameTo) + } + + tc.RenameTo.Set(renameTo) + continue + } + // $resourceEmbeddedInParent: if strings.EqualFold(lastId, resourceEmbeddedInParentTag) && c.Kind == yaml.ScalarNode { var resourceEmbeddedInParent string