Skip to content

Commit

Permalink
Integrate into the pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
theunrepentantgeek committed Jul 7, 2021
1 parent 977a141 commit 33880e9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions hack/generator/pkg/codegen/code_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func createAllPipelineStages(idFactory astmodel.IdentifierFactory, configuration

// Create Storage types
//TODO: For now only used for ARM
pipeline.CreateConversionGraph(graph).UsedFor(pipeline.ARMTarget),
pipeline.InjectOriginalVersionFunction(idFactory).UsedFor(pipeline.ARMTarget),
pipeline.CreateStorageTypes(graph).UsedFor(pipeline.ARMTarget),
pipeline.InjectOriginalVersionProperty().UsedFor(pipeline.ARMTarget),
Expand Down
5 changes: 2 additions & 3 deletions hack/generator/pkg/codegen/pipeline/create_storage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func CreateStorageTypes(conversionGraph *storage.ConversionGraph) Stage {
typesToConvert := types.Where(isPropertyContainer).Where(isNotARMType)

storageTypes := make(astmodel.Types)
typeConverter := storage.NewTypeConverter(types)
typeConverter := storage.NewTypeConverter(types, conversionGraph)

// Create storage variants
for name, def := range typesToConvert {
Expand All @@ -50,14 +50,13 @@ func CreateStorageTypes(conversionGraph *storage.ConversionGraph) Stage {
}

storageTypes.Add(storageDef)
conversionGraph.AddLink(name.PackageReference, storageDef.Name().PackageReference)
}

result := types.Copy()
result.AddTypes(storageTypes)
return result, nil
})

result.RequiresPrerequisiteStages(injectOriginalVersionFunctionStageId)
result.RequiresPrerequisiteStages(InjectOriginalVersionFunctionStageId, CreateConversionGraphStageId)
return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ func InjectOriginalGVKFunction(idFactory astmodel.IdentifierFactory) Stage {
return result, nil
})

stage.RequiresPrerequisiteStages(injectOriginalVersionFunctionStageId)
stage.RequiresPrerequisiteStages(InjectOriginalVersionFunctionStageId)
return stage
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/Azure/azure-service-operator/hack/generator/pkg/functions"
)

// injectOriginalVersionFunctionStageId is the unique identifier for this pipeline stage
const injectOriginalVersionFunctionStageId = "injectOriginalVersionFunction"
// InjectOriginalVersionFunctionStageId is the unique identifier for this pipeline stage
const InjectOriginalVersionFunctionStageId = "injectOriginalVersionFunction"

// InjectOriginalVersionFunction injects the function OriginalVersion() into each Spec type
// This function allows us to recover the original version used to create each custom resource, giving the operator the
Expand All @@ -25,7 +25,7 @@ const injectOriginalVersionFunctionStageId = "injectOriginalVersionFunction"
func InjectOriginalVersionFunction(idFactory astmodel.IdentifierFactory) Stage {

stage := MakeStage(
injectOriginalVersionFunctionStageId,
InjectOriginalVersionFunctionStageId,
"Inject the function OriginalVersion() into each Spec type",
func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) {
injector := storage.NewFunctionInjector()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ func InjectOriginalVersionProperty() Stage {
return result, nil
})

stage.RequiresPrerequisiteStages(injectOriginalVersionFunctionStageId)
stage.RequiresPrerequisiteStages(InjectOriginalVersionFunctionStageId)
return stage
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import (
"github.com/Azure/azure-service-operator/hack/generator/pkg/functions"
)

// injectPropertyAssignmentFunctionsStageId is the unique identifier for this pipeline stage
const injectPropertyAssignmentFunctionsStageId = "injectPropertyAssignmentFunctions"
// InjectPropertyAssignmentFunctionsStageId is the unique identifier for this pipeline stage
const InjectPropertyAssignmentFunctionsStageId = "injectPropertyAssignmentFunctions"

// InjectPropertyAssignmentFunctions injects property assignment functions AssignTo*() and AssignFrom*() into both
// resources and object types. These functions do the heavy lifting of the conversions between versions of each type and
// are the building blocks of the main CovertTo*() and ConvertFrom*() methods.
func InjectPropertyAssignmentFunctions(graph *storage.ConversionGraph, idFactory astmodel.IdentifierFactory) Stage {

stage := MakeStage(
injectPropertyAssignmentFunctionsStageId,
InjectPropertyAssignmentFunctionsStageId,
"Inject property assignment functions AssignFrom() and AssignTo() into resources and objects",
func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) {

Expand Down
14 changes: 9 additions & 5 deletions hack/generator/pkg/codegen/storage/type_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ type TypeConverter struct {
visitor astmodel.TypeVisitor
// types contains all the types for this group
types astmodel.Types
// conversionGraph is the map of package conversions we use
conversionGraph *ConversionGraph
// propertyConverter is used to modify properties
propertyConverter *PropertyConverter
}

// NewTypeConverter creates a new instance of the utility type
func NewTypeConverter(types astmodel.Types) *TypeConverter {
func NewTypeConverter(
types astmodel.Types,
conversionGraph *ConversionGraph) *TypeConverter {
result := &TypeConverter{
types: types,
conversionGraph: conversionGraph,
propertyConverter: NewPropertyConverter(types),
}

Expand Down Expand Up @@ -129,15 +134,14 @@ func (t *TypeConverter) stripAllFlags(
return astmodel.IdentityVisitOfFlaggedType(tv, flaggedType, ctx)
}

func (_ *TypeConverter) tryConvertToStoragePackage(name astmodel.TypeName) (astmodel.TypeName, bool) {
func (t *TypeConverter) tryConvertToStoragePackage(name astmodel.TypeName) (astmodel.TypeName, bool) {
// Map the type name into our storage package
localRef, ok := name.PackageReference.AsLocalPackage()
ref, ok := t.conversionGraph.LookupTransition(name.PackageReference)
if !ok {
return astmodel.TypeName{}, false
}

storageRef := astmodel.MakeStoragePackageReference(localRef)
visitedName := astmodel.MakeTypeName(storageRef, name.Name())
visitedName := astmodel.MakeTypeName(ref, name.Name())
return visitedName, true
}

Expand Down

0 comments on commit 33880e9

Please sign in to comment.