Skip to content

Commit

Permalink
Improve file naming for ARM types (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthchr authored Aug 31, 2020
1 parent d8cc794 commit b55b413
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
11 changes: 11 additions & 0 deletions hack/generator/pkg/astmodel/arm_type_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package astmodel

// CreateArmTypeName creates an ARM object type name
func CreateArmTypeName(name TypeName) TypeName {
return MakeTypeName(name.PackageReference, name.Name()+"Arm")
}
53 changes: 51 additions & 2 deletions hack/generator/pkg/astmodel/reference_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

package astmodel

import (
"github.com/pkg/errors"
)

// ReferenceGraph is a graph of references between types
type ReferenceGraph struct {
roots TypeNameSet
Expand All @@ -23,6 +27,48 @@ func CollectResourceDefinitions(definitions Types) TypeNameSet {
return resources
}

// CollectArmSpecDefinitions returns a TypeNameSet of all of the
// arm spec definitions passed in.
func CollectArmSpecAndStatusDefinitions(definitions Types) TypeNameSet {
findType := func(t Type) (TypeName, error) {
name, ok := t.(TypeName)
if !ok {
return TypeName{}, errors.Errorf("Type was not of type TypeName, instead %T", t)
}

armName := CreateArmTypeName(name)

if _, ok = definitions[armName]; !ok {
return TypeName{}, errors.Errorf("Couldn't ARM type find %q", armName)
}

return armName, nil
}

// TODO: We should be using a better way to identify ARM types. I believe
// TODO Bevan is working on it.
armSpecAndStatus := make(TypeNameSet)
for _, def := range definitions {
if resourceType, ok := def.Type().(*ResourceType); ok {

armSpecName, err := findType(resourceType.spec)
if err != nil {
panic(errors.Wrapf(err, "Error getting ARM spec for resource %q", def.Name()))
}
armSpecAndStatus.Add(armSpecName)

if resourceType.status != nil {
armStatusName, err := findType(resourceType.status)
if err != nil {
panic(errors.Wrapf(err, "Error getting ARM status for resource %q", def.Name()))
}
armSpecAndStatus.Add(armStatusName)
}
}
}
return armSpecAndStatus
}

// NewReferenceGraph produces a new ReferenceGraph with the given roots and references
func NewReferenceGraph(roots TypeNameSet, references map[TypeName]TypeNameSet) ReferenceGraph {
return ReferenceGraph{
Expand All @@ -32,9 +78,12 @@ func NewReferenceGraph(roots TypeNameSet, references map[TypeName]TypeNameSet) R
}

// NewReferenceGraphWithResourcesAsRoots produces a ReferenceGraph for the given set of
// types, where the Resource types are the roots.
// types, where the Resource types (and their ARM spec/status) are the roots.
func NewReferenceGraphWithResourcesAsRoots(types Types) ReferenceGraph {
roots := CollectResourceDefinitions(types)
resources := CollectResourceDefinitions(types)
armSpecAndStatus := CollectArmSpecAndStatusDefinitions(types)

roots := SetUnion(resources, armSpecAndStatus)

references := make(map[TypeName]TypeNameSet)
for _, def := range types {
Expand Down
10 changes: 3 additions & 7 deletions hack/generator/pkg/codegen/pipeline_create_arm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,6 @@ func removeValidations(t *astmodel.ObjectType) (*astmodel.ObjectType, error) {
return t, nil
}

func createArmTypeName(name astmodel.TypeName) astmodel.TypeName {
return astmodel.MakeTypeName(name.PackageReference, name.Name()+"Arm")
}

type conversionHandler = func(t *astmodel.ObjectType) (*astmodel.ObjectType, error)

func transformTypeDefinition(
Expand Down Expand Up @@ -260,7 +256,7 @@ func createArmTypeDefinition(definitions astmodel.Types, def astmodel.TypeDefini

armDef, err := transformTypeDefinition(
// This type is the ARM type so give it the ARM name
def.WithName(createArmTypeName(def.Name())),
def.WithName(astmodel.CreateArmTypeName(def.Name())),
[]conversionHandler{removeValidations, convertPropertiesToArmTypesWrapper})
if err != nil {
return astmodel.TypeDefinition{}, err
Expand Down Expand Up @@ -354,7 +350,7 @@ func convertArmPropertyTypeIfNeeded(definitions astmodel.Types, t astmodel.Type)
}

if _, ok := def.Type().(*astmodel.ObjectType); ok {
return createArmTypeName(def.Name())
return astmodel.CreateArmTypeName(def.Name())
} else {
// We may or may not need to use an updated type name (i.e. if it's an aliased primitive type we can
// just keep using that alias)
Expand All @@ -363,7 +359,7 @@ func convertArmPropertyTypeIfNeeded(definitions astmodel.Types, t astmodel.Type)
if updatedType.Equals(def.Type()) {
return it
} else {
return createArmTypeName(def.Name())
return astmodel.CreateArmTypeName(def.Name())
}
}
}
Expand Down

0 comments on commit b55b413

Please sign in to comment.