-
Notifications
You must be signed in to change notification settings - Fork 187
/
improve-property-descriptions.go
86 lines (69 loc) · 2.16 KB
/
improve-property-descriptions.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package pipeline
import (
"context"
"strings"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
)
// ImprovePropertyDescriptionsStageId is the unique identifier for this pipeline stage
const ImprovePropertyDescriptionsStageId = "improvePropertyDescriptions"
// ImprovePropertyDescriptions adds documentation to any properties missing it by copying the description from the
// corresponding type
func ImprovePropertyDescriptions() *Stage {
stage := NewStage(
ImprovePropertyDescriptionsStageId,
"Improve property descriptions by copying from the corresponding type",
func(ctx context.Context, state *State) (*State, error) {
visitor := createPropertyImprovingVisitor(state.Definitions())
result := make(astmodel.TypeDefinitionSet)
for _, def := range state.Definitions() {
newDef, err := visitor.VisitDefinition(def, nil)
if err != nil {
return nil, err
}
result.Add(newDef)
}
return state.WithDefinitions(result), nil
})
return stage
}
func createPropertyImprovingVisitor(defs astmodel.TypeDefinitionSet) astmodel.TypeVisitor[any] {
visitor := astmodel.TypeVisitorBuilder[any]{
VisitObjectType: func(
this *astmodel.TypeVisitor[any],
it *astmodel.ObjectType,
ctx any,
) (astmodel.Type, error) {
result := it
for _, prop := range it.Properties().AsSlice() {
// If the property already has a description, use it
if prop.Description() != "" {
continue
}
// If the property type isn't a typename, we can't do anything
tn, haveTypeName := astmodel.AsInternalTypeName(prop.PropertyType())
if !haveTypeName {
continue
}
// if the type name doesn't reference a type in our definitions, we can't do anything
def, haveDef := defs[tn]
if !haveDef {
continue
}
// If there's no description on the type, there's nothing to do
desc := def.Description()
if len(desc) == 0 {
continue
}
prop = prop.WithDescription(
strings.Join(desc, " "))
result = result.WithProperty(prop)
}
return result, nil
},
}
return visitor.Build()
}