Skip to content

Commit 94bcbe7

Browse files
authored
Merge pull request #1119 from cbandy/alias-deepcopy
🐛 Guard before type assertion
2 parents 6bfd1b4 + 12ea13f commit 94bcbe7

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

pkg/deepcopy/testdata/cronjob_types.go

+5
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,13 @@ type SpecificCases struct {
153153

154154
// Case: kubernetes-sigs/controller-tools#262 part 2 (see type definition)
155155
StringMap MapOfStrings `json:"stringMap"`
156+
157+
// Case: kubernetes-sigs/controller-tools#813
158+
StringAlias StringAlias `json:"stringAlias,omitempty"`
156159
}
157160

161+
type StringAlias = string
162+
158163
// Test aliases to basic types
159164
type TotallyAString string
160165

pkg/deepcopy/traverse.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -618,14 +618,15 @@ func shouldBeCopied(pkg *loader.Package, info *markers.TypeInfo) bool {
618618
return false
619619
}
620620

621-
// according to gengo, everything named is an alias, except for an alias to a pointer,
622-
// which is just a pointer, afaict. Just roll with it.
623-
if asPtr, isPtr := typeInfo.(*types.Named).Underlying().(*types.Pointer); isPtr {
624-
typeInfo = asPtr
625-
}
626-
627621
lastType := typeInfo
628622
if _, isNamed := typeInfo.(*types.Named); isNamed {
623+
// according to gengo, everything named is an alias, except for an alias to a pointer,
624+
// which is just a pointer, afaict. Just roll with it.
625+
if asPtr, isPtr := typeInfo.(*types.Named).Underlying().(*types.Pointer); isPtr {
626+
lastType = asPtr
627+
typeInfo = asPtr
628+
}
629+
629630
// if it has a manual deepcopy or deepcopyinto, we're fine
630631
if hasAnyDeepCopyMethod(pkg, typeInfo) {
631632
return true

0 commit comments

Comments
 (0)