-
Notifications
You must be signed in to change notification settings - Fork 187
/
apply_export_filters.go
113 lines (93 loc) · 3.23 KB
/
apply_export_filters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package pipeline
import (
"context"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/config"
"github.com/go-logr/logr"
"github.com/pkg/errors"
)
const ApplyExportFiltersStageID = "filterTypes"
// ApplyExportFilters creates a Stage to reduce our set of types for export
func ApplyExportFilters(
configuration *config.Configuration,
log logr.Logger,
) *Stage {
stage := NewStage(
ApplyExportFiltersStageID,
"Apply export filters to reduce the number of generated types",
func(ctx context.Context, state *State) (*State, error) {
return filterTypes(configuration, state, log)
})
stage.RequiresPostrequisiteStages(VerifyNoErroredTypesStageID)
return stage
}
// filterTypes applies the configuration include/exclude filters to the generated definitions
func filterTypes(
configuration *config.Configuration,
state *State,
log logr.Logger,
) (*State, error) {
resourcesToExport := make(astmodel.TypeDefinitionSet)
for _, def := range astmodel.FindResourceDefinitions(state.Definitions()) {
defName := def.Name()
export := shouldExport(defName, configuration)
if !export {
log.V(1).Info("Skipping resource", "resource", defName)
continue
}
log.V(1).Info("Exporting resource", "resource", defName)
resourcesToExport.Add(def)
}
typesToExport, err := astmodel.FindConnectedDefinitions(state.Definitions(), resourcesToExport)
if err != nil {
return nil, errors.Wrap(err, "finding types connected to resources marked for export")
}
// Find and apply renames
renames := make(astmodel.TypeAssociation)
for n := range typesToExport {
newName := ""
if as, ok := configuration.ObjectModelConfiguration.ExportAs.Lookup(n); ok {
newName = as
} else if to, ok := configuration.ObjectModelConfiguration.RenameTo.Lookup(n); ok {
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)
}
}
if err = configuration.ObjectModelConfiguration.Export.VerifyConsumed(); err != nil {
return nil, err
}
if err = configuration.ObjectModelConfiguration.ExportAs.VerifyConsumed(); err != nil {
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)
if err != nil {
return nil, err
}
return state.WithDefinitions(result), nil
}
// shouldExport works out whether the specified Resource should be exported or not
func shouldExport(defName astmodel.InternalTypeName, configuration *config.Configuration) bool {
if export, ok := configuration.ObjectModelConfiguration.Export.Lookup(defName); ok {
// $export is configured, return that value
return export
}
if _, ok := configuration.ObjectModelConfiguration.ExportAs.Lookup(defName); ok {
// $exportAs is configured, we DO want to export
return true
}
// Default is to not export
return false
}