-
Notifications
You must be signed in to change notification settings - Fork 187
/
rename_properties.go
113 lines (93 loc) · 3.06 KB
/
rename_properties.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"
"strings"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"github.com/pkg/errors"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/config"
)
// RenamePropertiesStageID is the unique identifier for this pipeline stage
const RenamePropertiesStageID = "renameProperties"
func RenameProperties(cfg *config.ObjectModelConfiguration) *Stage {
stage := NewStage(
RenamePropertiesStageID,
"Rename properties",
func(_ context.Context, state *State) (*State, error) {
visitor := createPropertyRenamingVisitor(cfg)
var errs []error
definitions := state.Definitions()
modified := make(astmodel.TypeDefinitionSet)
for name, def := range definitions {
_, ok := astmodel.AsObjectType(def.Type())
if !ok {
// Not an object type, so no properties to rename
continue
}
if !cfg.IsTypeConfigured(name) {
// If this definition isn't configured, don't do anything
continue
}
updated, err := visitor.VisitDefinition(def, name)
if err != nil {
errs = append(errs, err)
} else {
modified.Add(updated)
}
}
if len(errs) > 0 {
return nil, kerrors.NewAggregate(errs)
}
if err := cfg.RenamePropertyTo.VerifyConsumed(); err != nil {
return nil, errors.Wrap(err, "verifying property rename configuration")
}
return state.WithOverlaidDefinitions(modified), nil
})
stage.RequiresPostrequisiteStages(
AddStatusConditionsStageID, // Must rename other properties before we try to introduce the `Conditions` property
AddOperatorSpecStageID, // Must rename other properties before we try to introduce the `OperatorSpec` property
)
return stage
}
func createPropertyRenamingVisitor(
cfg *config.ObjectModelConfiguration,
) astmodel.TypeVisitor[astmodel.InternalTypeName] {
builder := astmodel.TypeVisitorBuilder[astmodel.InternalTypeName]{
VisitObjectType: func(
this *astmodel.TypeVisitor[astmodel.InternalTypeName],
it *astmodel.ObjectType,
ctx astmodel.InternalTypeName,
) (astmodel.Type, error) {
return renamePropertiesInObjectType(ctx, it, cfg)
},
}
return builder.Build()
}
func renamePropertiesInObjectType(
typeName astmodel.InternalTypeName,
ot *astmodel.ObjectType,
cfg *config.ObjectModelConfiguration,
) (astmodel.Type, error) {
properties := make([]*astmodel.PropertyDefinition, 0, ot.Properties().Len())
err := ot.Properties().ForEachError(
func(prop *astmodel.PropertyDefinition) error {
name, ok := cfg.RenamePropertyTo.Lookup(typeName, prop.PropertyName())
if !ok {
properties = append(properties, prop)
return nil
}
updated := prop.
WithName(astmodel.PropertyName(name)).
WithJsonName(strings.ToLower(name))
properties = append(properties, updated)
return nil
})
if err != nil {
return nil, errors.Wrapf(err, "renaming properties for %s", typeName)
}
return ot.WithoutProperties().WithProperties(properties...), nil
}