Skip to content

Commit

Permalink
Add configuration to rename types (#3466)
Browse files Browse the repository at this point in the history
* Add configuration

* Use new configuration

* Add documentation to azure-arm.yaml
  • Loading branch information
theunrepentantgeek authored Nov 10, 2023
1 parent c243cbe commit 26d3175
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
12 changes: 12 additions & 0 deletions v2/azure-arm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,18 @@ status:
# Establishes a connection between the two types allowing for proper
# forward and backward conversion.
#
# $renameTo: <string>
# 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: <version>
# Gives the version number of the first release of ASO that provides
# support for this resource.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
Expand Down
20 changes: 11 additions & 9 deletions v2/tools/generator/internal/config/object_model_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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](
Expand Down
20 changes: 19 additions & 1 deletion v2/tools/generator/internal/config/type_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand All @@ -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
)
Expand All @@ -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),
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -272,6 +278,18 @@ func (tc *TypeConfiguration) UnmarshalYAML(value *yaml.Node) error {
continue
}

// $renameTo: <string>
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: <string>
if strings.EqualFold(lastId, resourceEmbeddedInParentTag) && c.Kind == yaml.ScalarNode {
var resourceEmbeddedInParent string
Expand Down

0 comments on commit 26d3175

Please sign in to comment.