-
Notifications
You must be signed in to change notification settings - Fork 187
/
strip_unused_types.go
48 lines (39 loc) · 1.44 KB
/
strip_unused_types.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package pipeline
import (
"context"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
)
// StripUnreferencedTypeDefinitionsStageID is the unique identifier for this pipeline stage
const StripUnreferencedTypeDefinitionsStageID = "stripUnreferenced"
func StripUnreferencedTypeDefinitions() *Stage {
return NewLegacyStage(
StripUnreferencedTypeDefinitionsStageID,
"Strip unreferenced types",
func(ctx context.Context, defs astmodel.TypeDefinitionSet) (astmodel.TypeDefinitionSet, error) {
resources := astmodel.FindResourceDefinitions(defs)
armSpecAndStatus := astmodel.CollectARMSpecAndStatusDefinitions(defs)
roots := astmodel.SetUnion(resources.Names(), armSpecAndStatus)
return StripUnusedDefinitions(roots, defs)
})
}
// StripUnusedDefinitions removes all types that aren't in roots or
// referred to by the types in roots, for example types that are
// generated as a byproduct of an allOf element.
func StripUnusedDefinitions(
roots astmodel.TypeNameSet,
defs astmodel.TypeDefinitionSet,
) (astmodel.TypeDefinitionSet, error) {
graph := astmodel.MakeReferenceGraphWithRoots(roots, defs)
connectedDefinitions := graph.Connected()
usedDefinitions := make(astmodel.TypeDefinitionSet)
for _, def := range defs {
if connectedDefinitions.Contains(def.Name()) {
usedDefinitions.Add(def)
}
}
return usedDefinitions, nil
}