Skip to content

Commit 6eea484

Browse files
committed
🐛 Handle pointer to alias in DeepCopy
Fixes: #1136 Signed-off-by: Chris Bandy <bandy.chris@gmail.com>
1 parent d1f73b5 commit 6eea484

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

pkg/crd/testdata/cronjob_types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ type CronJobSpec struct {
340340
AliasFromPackage corev1.IPFamilyPolicyType `json:"aliasFromPackage,omitempty"`
341341

342342
// This tests that string alias is handled correctly.
343-
StringAlias StringAlias `json:"stringAlias,omitempty"`
343+
StringAlias StringAlias `json:"stringAlias,omitempty"`
344+
StringAliasPtr *StringAlias `json:"stringAliasPtr,omitempty"`
344345

345346
// This tests that validation on a string alias type is handled correctly.
346347
// +kubebuilder:validation:MinLength=1

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -9012,6 +9012,8 @@ spec:
90129012
maxLength: 255
90139013
minLength: 1
90149014
type: string
9015+
stringAliasPtr:
9016+
type: string
90159017
stringPair:
90169018
description: This tests string slice validation.
90179019
items:

pkg/deepcopy/testdata/cronjob_types.go

+3
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ type SpecificCases struct {
156156

157157
// Case: kubernetes-sigs/controller-tools#813
158158
StringAlias StringAlias `json:"stringAlias,omitempty"`
159+
160+
// Case: kubernetes-sigs/controller-tools#1136
161+
StringAliasPtr *StringAlias `json:"stringAliasPtr,omitempty"`
159162
}
160163

161164
type StringAlias = string

pkg/deepcopy/testdata/zz_generated.deepcopy.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/deepcopy/traverse.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,12 @@ func (n *namingInfo) Syntax(basePkg *loader.Package, imports *importsList) strin
173173

174174
// NB(directxman12): typeInfo.String gets us most of the way there,
175175
// but fails (for us) on named imports, since it uses the full package path.
176+
var typeName *types.TypeName
176177
switch typeInfo := n.typeInfo.(type) {
178+
case *types.Alias:
179+
typeName = typeInfo.Obj()
177180
case *types.Named:
178-
// register that we need an import for this type,
179-
// so we can get the appropriate alias to use.
180-
typeName := typeInfo.Obj()
181-
otherPkg := typeName.Pkg()
182-
if otherPkg == basePkg.Types {
183-
// local import
184-
return typeName.Name()
185-
}
186-
alias := imports.NeedImport(loader.NonVendorPath(otherPkg.Path()))
187-
return alias + "." + typeName.Name()
181+
typeName = typeInfo.Obj()
188182
case *types.Basic:
189183
return typeInfo.String()
190184
case *types.Pointer:
@@ -200,6 +194,16 @@ func (n *namingInfo) Syntax(basePkg *loader.Package, imports *importsList) strin
200194
basePkg.AddError(fmt.Errorf("name requested for invalid type: %s", typeInfo))
201195
return typeInfo.String()
202196
}
197+
198+
// register that we need an import for this type,
199+
// so we can get the appropriate alias to use.
200+
otherPkg := typeName.Pkg()
201+
if otherPkg == basePkg.Types {
202+
// local import
203+
return typeName.Name()
204+
}
205+
alias := imports.NeedImport(loader.NonVendorPath(otherPkg.Path()))
206+
return alias + "." + typeName.Name()
203207
}
204208

205209
// copyMethodMakers makes DeepCopy (and related) methods for Go types,

0 commit comments

Comments
 (0)