-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new pipeline stage that merges groups
- In the case that group A references group B, we need to pull group B's types into group A. - Fixes #1578
- Loading branch information
Showing
19 changed files
with
122 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
hack/generator/pkg/codegen/pipeline_collapse_cross_group_refs.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package codegen | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// collapseCrossGroupReferences finds and removes references between API groups. This isn't particularly common | ||
// but does occur in a few instances, for example from Microsoft.Compute -> Microsoft.Compute.Extensions. | ||
func collapseCrossGroupReferences() PipelineStage { | ||
return MakePipelineStage( | ||
"collapseCrossGroupReferences", | ||
"Finds and removes cross group references", | ||
func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { | ||
resources := astmodel.CollectResourceDefinitions(types) | ||
result := make(astmodel.Types) | ||
|
||
for resourceName := range resources { | ||
walker := newTypeWalker(types, resourceName) | ||
updatedTypes, err := walker.Walk(types[resourceName]) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed walking types") | ||
} | ||
|
||
for _, newDef := range updatedTypes { | ||
err := result.AddAllowDuplicates(newDef) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
return result, nil | ||
}) | ||
} | ||
|
||
func newTypeWalker(types astmodel.Types, resourceName astmodel.TypeName) *astmodel.TypeWalker { | ||
visitor := astmodel.TypeVisitorBuilder{}.Build() | ||
walker := astmodel.NewTypeWalker(types, visitor) | ||
walker.AfterVisit = func(original astmodel.TypeDefinition, updated astmodel.TypeDefinition, ctx interface{}) (astmodel.TypeDefinition, error) { | ||
if !resourceName.PackageReference.Equals(updated.Name().PackageReference) { | ||
// Note: If we ever find this generating colliding names, we might need to introduce a unique suffix. | ||
// For now though it doesn't seem to, so preserving the shorter names as they're clearer. | ||
updated = updated.WithName(astmodel.MakeTypeName(resourceName.PackageReference, updated.Name().Name())) | ||
} | ||
return astmodel.IdentityAfterVisit(original, updated, ctx) | ||
} | ||
|
||
return walker | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters