-
Notifications
You must be signed in to change notification settings - Fork 187
/
unroll_recursive_types.go
84 lines (72 loc) · 2.88 KB
/
unroll_recursive_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
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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package pipeline
import (
"context"
"strings"
"github.com/go-logr/logr"
"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/codegen/pipeline/recursivetypefixer"
)
const UnrollRecursiveTypesStageID = "unrollRecursiveTypes"
// UnrollRecursiveTypes finds types that reference themselves and "unrolls" the reference. So a type that looks like:
//
// type Error struct {
// code string
// message string
// errors []Error
// }
//
// gets unrolled to look like:
//
// type Error struct {
// code string
// message string
// errors []Error_Unrolled
// }
//
// where Error_Unrolled looks like:
//
// type Error_Unrolled struct {
// code string
// message string
// }
//
// The recursive references must be removed because
// controller-tools doesn't support generating "references" (JSON $ref) so it can't support recursive types today.
// See https://github.com/kubernetes-sigs/controller-tools/issues/489 for more information.
// Unrolling these types, while required for controller-tools to function, means
// THAT THE TYPES WE GENERATE DON'T EXACTLY CONFORM TO THE PUBLISHED API!
// In practice, we think this is ok for Error types because our observation is that Errors that reference themselves only return
// a depth of 1 in practice.
// If we were to unroll all loops (rather than just types that directly reference themselves like we're doing here) that
// "in practice" observation may no longer hold, so we avoid doing it here (it's also more complicated to do that).
func UnrollRecursiveTypes(log logr.Logger) *Stage {
return NewStage(
UnrollRecursiveTypesStageID,
"Unroll directly recursive types since they are not supported by controller-gen",
func(ctx context.Context, state *State) (*State, error) {
result := make(astmodel.TypeDefinitionSet)
// Find object types that reference themselves
fixer := recursivetypefixer.NewSimpleRecursiveTypeFixer(log)
for _, def := range state.Definitions() {
updatedDef, err := fixer.Fix(def)
if err != nil {
return nil, err
}
// Confirm that all the changed types have Error in their name. If the type doesn't have Error
// we can't be sure it's safe to unroll, so we raise an error. This is fatal because controller-gen
// will error on recursive types when we run it later and we want to fail fast.
if !astmodel.TypeEquals(def.Type(), updatedDef.Type()) && !strings.Contains(def.Name().Name(), "Error") {
return nil, errors.Errorf("%q is directly recursive and cannot be unrolled because it doesn't contain 'Error'", def.Name())
}
result.Add(updatedDef)
}
// Add new unrolled types
result.AddTypes(fixer.Types())
return state.WithDefinitions(result), nil
})
}