Skip to content

Commit b62a514

Browse files
committed
Use pointers to generators everywhere
Since we changed genall to use pointers to generators to make them comparable, we'll need to actually keep track of the specific instance of a given generator in the Runtime, otherwise things like output rules don't get applied correctly.
1 parent 3038b25 commit b62a514

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

pkg/genall/genall.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ import (
3030
)
3131

3232
// Generators are a list of Generators.
33-
type Generators []Generator
33+
// NB(directxman12): this is a pointer so that we can uniquely identify each
34+
// instance of a generator, even if it's not hashable. Different *instances*
35+
// of a generator are treated differently.
36+
type Generators []*Generator
3437

3538
// RegisterMarkers registers all markers defined by each of the Generators in
3639
// this list into the given registry.
3740
func (g Generators) RegisterMarkers(reg *markers.Registry) error {
3841
for _, gen := range g {
39-
if err := gen.RegisterMarkers(reg); err != nil {
42+
if err := (*gen).RegisterMarkers(reg); err != nil {
4043
return err
4144
}
4245
}
@@ -162,8 +165,7 @@ func (r *Runtime) Run() bool {
162165
return true
163166
}
164167

165-
for i := range r.Generators {
166-
gen := &r.Generators[i] // don't take a reference to the loop variable
168+
for _, gen := range r.Generators {
167169
ctx := r.GenerationContext // make a shallow copy
168170
ctx.OutputRule = r.OutputRules.ForGenerator(gen)
169171
if err := (*gen).Generate(&ctx); err != nil {

pkg/genall/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func protoFromOptions(optionsRegistry *markers.Registry, options []string) (prot
133133

134134
switch val := val.(type) {
135135
case Generator:
136-
gens = append(gens, val)
136+
gens = append(gens, &val)
137137
gensByName[defn.Name] = &val
138138
case OutputRule:
139139
_, genName := splitOutputRuleOption(defn.Name)

pkg/schemapatcher/gen_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ var _ = Describe("CRD Patching From Parsing to Editing", func() {
4141
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
4242

4343
By("loading the generation runtime")
44-
crdSchemaGen := &Generator{
44+
var crdSchemaGen genall.Generator = &Generator{
4545
ManifestsPath: "./manifests",
4646
}
47-
rt, err := genall.Generators{crdSchemaGen}.ForRoots("./...")
47+
rt, err := genall.Generators{&crdSchemaGen}.ForRoots("./...")
4848
Expect(err).NotTo(HaveOccurred())
4949

5050
outputDir, err := ioutil.TempDir("", "controller-tools-test")

0 commit comments

Comments
 (0)