Skip to content

Commit

Permalink
🐛 Handle pointer to alias in DeepCopy
Browse files Browse the repository at this point in the history
Fixes: kubernetes-sigs#1136
Signed-off-by: Chris Bandy <bandy.chris@gmail.com>
  • Loading branch information
cbandy committed Feb 5, 2025
1 parent d1f73b5 commit 6eea484
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ type CronJobSpec struct {
AliasFromPackage corev1.IPFamilyPolicyType `json:"aliasFromPackage,omitempty"`

// This tests that string alias is handled correctly.
StringAlias StringAlias `json:"stringAlias,omitempty"`
StringAlias StringAlias `json:"stringAlias,omitempty"`
StringAliasPtr *StringAlias `json:"stringAliasPtr,omitempty"`

// This tests that validation on a string alias type is handled correctly.
// +kubebuilder:validation:MinLength=1
Expand Down
2 changes: 2 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9012,6 +9012,8 @@ spec:
maxLength: 255
minLength: 1
type: string
stringAliasPtr:
type: string
stringPair:
description: This tests string slice validation.
items:
Expand Down
3 changes: 3 additions & 0 deletions pkg/deepcopy/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ type SpecificCases struct {

// Case: kubernetes-sigs/controller-tools#813
StringAlias StringAlias `json:"stringAlias,omitempty"`

// Case: kubernetes-sigs/controller-tools#1136
StringAliasPtr *StringAlias `json:"stringAliasPtr,omitempty"`
}

type StringAlias = string
Expand Down
5 changes: 5 additions & 0 deletions pkg/deepcopy/testdata/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions pkg/deepcopy/traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,12 @@ func (n *namingInfo) Syntax(basePkg *loader.Package, imports *importsList) strin

// NB(directxman12): typeInfo.String gets us most of the way there,
// but fails (for us) on named imports, since it uses the full package path.
var typeName *types.TypeName
switch typeInfo := n.typeInfo.(type) {
case *types.Alias:
typeName = typeInfo.Obj()
case *types.Named:
// register that we need an import for this type,
// so we can get the appropriate alias to use.
typeName := typeInfo.Obj()
otherPkg := typeName.Pkg()
if otherPkg == basePkg.Types {
// local import
return typeName.Name()
}
alias := imports.NeedImport(loader.NonVendorPath(otherPkg.Path()))
return alias + "." + typeName.Name()
typeName = typeInfo.Obj()
case *types.Basic:
return typeInfo.String()
case *types.Pointer:
Expand All @@ -200,6 +194,16 @@ func (n *namingInfo) Syntax(basePkg *loader.Package, imports *importsList) strin
basePkg.AddError(fmt.Errorf("name requested for invalid type: %s", typeInfo))
return typeInfo.String()
}

// register that we need an import for this type,
// so we can get the appropriate alias to use.
otherPkg := typeName.Pkg()
if otherPkg == basePkg.Types {
// local import
return typeName.Name()
}
alias := imports.NeedImport(loader.NonVendorPath(otherPkg.Path()))
return alias + "." + typeName.Name()
}

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

0 comments on commit 6eea484

Please sign in to comment.