Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Merge 0039a8b into d8a9e0b
Browse files Browse the repository at this point in the history
  • Loading branch information
theunrepentantgeek committed Dec 14, 2020
2 parents d8a9e0b + 0039a8b commit 3b9daf5
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 145 deletions.
66 changes: 0 additions & 66 deletions hack/generator/pkg/astmodel/arm_type.go

This file was deleted.

6 changes: 0 additions & 6 deletions hack/generator/pkg/astmodel/arm_type_test.go

This file was deleted.

3 changes: 0 additions & 3 deletions hack/generator/pkg/astmodel/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ func createAsObjectTypeVisitor() *TypeVisitor {
* Unwrap if needed
*/

result.VisitArmType = func(_ *TypeVisitor, at *ArmType, _ interface{}) (Type, error) {
return &at.objectType, nil
}
result.VisitOptionalType = func(tv *TypeVisitor, ot *OptionalType, ctx interface{}) (Type, error) {
return tv.Visit(ot.element, ctx)
}
Expand Down
22 changes: 0 additions & 22 deletions hack/generator/pkg/astmodel/type_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type TypeVisitor struct {
VisitOptionalType func(this *TypeVisitor, it *OptionalType, ctx interface{}) (Type, error)
VisitEnumType func(this *TypeVisitor, it *EnumType, ctx interface{}) (Type, error)
VisitResourceType func(this *TypeVisitor, it *ResourceType, ctx interface{}) (Type, error)
VisitArmType func(this *TypeVisitor, it *ArmType, ctx interface{}) (Type, error)
VisitFlaggedType func(this *TypeVisitor, it *FlaggedType, ctx interface{}) (Type, error)
}

Expand Down Expand Up @@ -56,8 +55,6 @@ func (tv *TypeVisitor) Visit(t Type, ctx interface{}) (Type, error) {
return tv.VisitEnumType(tv, it, ctx)
case *ResourceType:
return tv.VisitResourceType(tv, it, ctx)
case *ArmType:
return tv.VisitArmType(tv, it, ctx)
case *FlaggedType:
return tv.VisitFlaggedType(tv, it, ctx)
}
Expand Down Expand Up @@ -123,7 +120,6 @@ func MakeTypeVisitor() TypeVisitor {
VisitEnumType: IdentityVisitOfEnumType,
VisitOptionalType: IdentityVisitOfOptionalType,
VisitResourceType: IdentityVisitOfResourceType,
VisitArmType: IdentityVisitOfArmType,
VisitOneOfType: IdentityVisitOfOneOfType,
VisitAllOfType: IdentityVisitOfAllOfType,
VisitFlaggedType: IdentityVisitOfFlaggedType,
Expand Down Expand Up @@ -233,24 +229,6 @@ func IdentityVisitOfResourceType(this *TypeVisitor, it *ResourceType, ctx interf
return it.WithSpec(visitedSpec).WithStatus(visitedStatus), nil
}

func IdentityVisitOfArmType(this *TypeVisitor, at *ArmType, ctx interface{}) (Type, error) {
nt, err := this.Visit(&at.objectType, ctx)
if err != nil {
return nil, errors.Wrapf(err, "failed to visit ARM underlying type %T", at.objectType)
}

switch newType := nt.(type) {
case *ObjectType:
return NewArmType(*newType), nil

case *ArmType:
return newType, nil

default:
return nil, errors.Errorf("expected transformation of ARM underlying type %T to return ObjectType or ArmType, not %T", at.objectType, nt)
}
}

func IdentityVisitOfOneOfType(this *TypeVisitor, it OneOfType, ctx interface{}) (Type, error) {
var newTypes []Type
err := it.Types().ForEachError(func(oneOf Type, _ int) error {
Expand Down
30 changes: 0 additions & 30 deletions hack/generator/pkg/astmodel/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,36 +112,6 @@ func (types Types) ResolveResourceType(aType Type) (*ResourceType, bool) {
}
}

// IsArmType returns true if the passed type is an Arm type or names an Arm type; false otherwise.
func (types Types) IsArmType(aType Type) bool {
switch t := aType.(type) {
case *ArmType:
return true

case *ResourceType:
return types.IsArmResource(t)

case TypeName:
if def, ok := types[t]; ok {
return types.IsArmDefinition(&def)
}
return false

default:
return false
}
}

// IsArmDefinition returns true if the passed definition is for an Arm type or names an Arm type; false otherwise.
func (types Types) IsArmDefinition(definition *TypeDefinition) bool {
return types.IsArmType(definition.Type())
}

// IsArmDefinition returns true if the passed resource contains Arm Types or names Arm types; false otherwise.
func (types Types) IsArmResource(resource *ResourceType) bool {
return types.IsArmType(resource.SpecType()) || types.IsArmType(resource.StatusType())
}

// ResolveEnumType returns true if the passed type is an enum type or names an enum type; false otherwise.
func (types Types) ResolveEnumType(aType Type) (EnumType, bool) {
switch t := aType.(type) {
Expand Down
18 changes: 12 additions & 6 deletions hack/generator/pkg/codegen/pipeline_create_arm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,19 +281,25 @@ func createArmResourceSpecDefinition(
}

// ARM specs have a special interface that they need to implement, go ahead and create that here
spec, ok := armTypeDef.Type().(*astmodel.ArmType)
if !astmodel.ArmFlag.IsOn(armTypeDef.Type()) {
return emptyDef, emptyName, errors.Errorf("Arm spec %q isn't a flagged object, instead: %T", armTypeDef.Name(), armTypeDef.Type())
}

// Safe because above test passed
flagged := armTypeDef.Type().(*astmodel.FlaggedType)

specObj, ok := flagged.Element().(*astmodel.ObjectType)
if !ok {
return emptyDef, emptyName, errors.Errorf("Arm spec %q isn't of type ArmType, instead: %T", armTypeDef.Name(), armTypeDef.Type())
return emptyDef, emptyName, errors.Errorf("Arm spec %q isn't an object, instead: %T", armTypeDef.Name(), armTypeDef.Type())
}

specObj := spec.ObjectType()
iface, err := astmodel.NewArmSpecInterfaceImpl(idFactory, &specObj)
iface, err := astmodel.NewArmSpecInterfaceImpl(idFactory, specObj)
if err != nil {
return emptyDef, emptyName, err
}

updatedSpec := specObj.WithInterface(iface)
armTypeDef = armTypeDef.WithType(astmodel.NewArmType(*updatedSpec))
armTypeDef = armTypeDef.WithType(astmodel.ArmFlag.ApplyTo(updatedSpec))

return armTypeDef, resourceSpecDef.Name(), nil
}
Expand All @@ -312,7 +318,7 @@ func createArmTypeDefinition(definitions astmodel.Types, isSpecType bool, def as
}

result, err := armDef.ApplyObjectTransformation(func(objectType *astmodel.ObjectType) (astmodel.Type, error) {
return astmodel.NewArmType(*objectType), nil
return astmodel.ArmFlag.ApplyTo(objectType), nil
})
if err != nil {
return astmodel.TypeDefinition{},
Expand Down
20 changes: 12 additions & 8 deletions hack/generator/pkg/codegen/pipeline_create_storage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createStorageTypes() PipelineStage {
for _, d := range types {
d := d

if types.IsArmDefinition(&d) {
if astmodel.ArmFlag.IsOn(d.Type()) {
// Skip ARM definitions, we don't need to create storage variants of those
continue
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func makeStorageTypesVisitor(types astmodel.Types) astmodel.TypeVisitor {
result := astmodel.MakeTypeVisitor()
result.VisitTypeName = factory.visitTypeName
result.VisitObjectType = factory.visitObjectType
result.VisitArmType = factory.visitArmType
result.VisitFlaggedType = factory.visitFlaggedType

factory.visitor = result
factory.propertyConversions = []propertyConversion{
Expand Down Expand Up @@ -192,12 +192,16 @@ func (factory *StorageTypeFactory) convertPropertiesForStorage(
return p, nil
}

func (factory *StorageTypeFactory) visitArmType(
_ *astmodel.TypeVisitor,
armType *astmodel.ArmType,
_ interface{}) (astmodel.Type, error) {
// We don't want to do anything with ARM types
return armType, nil
func (factory *StorageTypeFactory) visitFlaggedType(
tv *astmodel.TypeVisitor,
flaggedType *astmodel.FlaggedType,
ctx interface{}) (astmodel.Type, error) {
if flaggedType.HasFlag(astmodel.ArmFlag) {
// We don't want to do anything with ARM types
return flaggedType, nil
}

return astmodel.IdentityVisitOfFlaggedType(tv, flaggedType, ctx)
}

func descriptionForStorageVariant(definition astmodel.TypeDefinition) []string {
Expand Down
8 changes: 4 additions & 4 deletions hack/generator/pkg/codegen/pipeline_simplify_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func simplifyDefinitions() PipelineStage {
func createSimplifyingVisitor() astmodel.TypeVisitor {
result := astmodel.MakeTypeVisitor()

// Unwrap ArmTypes, promoting the object within
result.VisitArmType = func(tv *astmodel.TypeVisitor, at *astmodel.ArmType, ctx interface{}) (astmodel.Type, error) {
ot := at.ObjectType()
return tv.Visit(&ot, ctx)
// Unwrap FlaggedTypes, promoting the object within
result.VisitFlaggedType = func(tv *astmodel.TypeVisitor, ft *astmodel.FlaggedType, ctx interface{}) (astmodel.Type, error) {
element := ft.Element()
return tv.Visit(element, ctx)
}

// Unwrap flagged types, promoting the object within.
Expand Down

0 comments on commit 3b9daf5

Please sign in to comment.