mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Clarify copyable vs in-bounds
This make it clearer where "is copyable" is needed and where "is in bounds" is needed. No effect on generated code.
This commit is contained in:
parent
1ba6f5df9e
commit
82b2d2c87b
@ -171,7 +171,7 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
|
|||||||
ttag := extractTag(t.CommentLines)
|
ttag := extractTag(t.CommentLines)
|
||||||
if ttag != nil && ttag.value == "true" {
|
if ttag != nil && ttag.value == "true" {
|
||||||
glog.V(5).Infof(" tag=true")
|
glog.V(5).Infof(" tag=true")
|
||||||
if !copyableWithinPackage(t, boundingDirs) {
|
if !copyableType(t) {
|
||||||
glog.Fatalf("Type %v requests deepcopy generation but is not copyable", t)
|
glog.Fatalf("Type %v requests deepcopy generation but is not copyable", t)
|
||||||
}
|
}
|
||||||
pkgNeedsGeneration = true
|
pkgNeedsGeneration = true
|
||||||
@ -247,15 +247,22 @@ func (g *genDeepCopy) Filter(c *generator.Context, t *types.Type) bool {
|
|||||||
enabled = true
|
enabled = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
copyable := enabled && g.copyableWithinPackage(t)
|
copyable := enabled && copyableType(t)
|
||||||
if copyable {
|
if copyable {
|
||||||
g.typesForInit = append(g.typesForInit, t)
|
g.typesForInit = append(g.typesForInit, t)
|
||||||
}
|
}
|
||||||
return copyable
|
return copyable
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *genDeepCopy) copyableWithinPackage(t *types.Type) bool {
|
func (g *genDeepCopy) copyableAndInBounds(t *types.Type) bool {
|
||||||
return copyableWithinPackage(t, g.boundingDirs)
|
if !copyableType(t) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Only packages within the restricted range can be processed.
|
||||||
|
if !isRootedUnder(t.Name.Package, g.boundingDirs) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// hasDeepCopyMethod returns true if an appropriate DeepCopy() method is
|
// hasDeepCopyMethod returns true if an appropriate DeepCopy() method is
|
||||||
@ -293,16 +300,12 @@ func isRootedUnder(pkg string, roots []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyableWithinPackage(t *types.Type, boundingDirs []string) bool {
|
func copyableType(t *types.Type) bool {
|
||||||
// If the type opts out of copy-generation, stop.
|
// If the type opts out of copy-generation, stop.
|
||||||
ttag := extractTag(t.CommentLines)
|
ttag := extractTag(t.CommentLines)
|
||||||
if ttag != nil && ttag.value == "false" {
|
if ttag != nil && ttag.value == "false" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// Only packages within the restricted range can be processed.
|
|
||||||
if !isRootedUnder(t.Name.Package, boundingDirs) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// TODO: Consider generating functions for other kinds too.
|
// TODO: Consider generating functions for other kinds too.
|
||||||
if t.Kind != types.Struct {
|
if t.Kind != types.Struct {
|
||||||
return false
|
return false
|
||||||
@ -477,7 +480,7 @@ func (g *genDeepCopy) doMap(t *types.Type, sw *generator.SnippetWriter) {
|
|||||||
sw.Do("}\n", nil)
|
sw.Do("}\n", nil)
|
||||||
default:
|
default:
|
||||||
sw.Do("for key, val := range in {\n", nil)
|
sw.Do("for key, val := range in {\n", nil)
|
||||||
if g.copyableWithinPackage(t.Elem) {
|
if g.copyableAndInBounds(t.Elem) {
|
||||||
sw.Do("newVal := new($.|raw$)\n", t.Elem)
|
sw.Do("newVal := new($.|raw$)\n", t.Elem)
|
||||||
funcName := g.funcNameTmpl(t.Elem)
|
funcName := g.funcNameTmpl(t.Elem)
|
||||||
sw.Do(fmt.Sprintf("if err := %s(val, newVal, c); err != nil {\n", funcName), argsFromType(t.Elem))
|
sw.Do(fmt.Sprintf("if err := %s(val, newVal, c); err != nil {\n", funcName), argsFromType(t.Elem))
|
||||||
@ -511,7 +514,7 @@ func (g *genDeepCopy) doSlice(t *types.Type, sw *generator.SnippetWriter) {
|
|||||||
sw.Do("(*out)[i] = in[i].DeepCopy()\n", nil)
|
sw.Do("(*out)[i] = in[i].DeepCopy()\n", nil)
|
||||||
} else if t.Elem.IsAssignable() {
|
} else if t.Elem.IsAssignable() {
|
||||||
sw.Do("(*out)[i] = in[i]\n", nil)
|
sw.Do("(*out)[i] = in[i]\n", nil)
|
||||||
} else if g.copyableWithinPackage(t.Elem) {
|
} else if g.copyableAndInBounds(t.Elem) {
|
||||||
funcName := g.funcNameTmpl(t.Elem)
|
funcName := g.funcNameTmpl(t.Elem)
|
||||||
sw.Do(fmt.Sprintf("if err := %s(in[i], &(*out)[i], c); err != nil {\n", funcName), argsFromType(t.Elem))
|
sw.Do(fmt.Sprintf("if err := %s(in[i], &(*out)[i], c); err != nil {\n", funcName), argsFromType(t.Elem))
|
||||||
sw.Do("return err\n", nil)
|
sw.Do("return err\n", nil)
|
||||||
@ -554,7 +557,7 @@ func (g *genDeepCopy) doStruct(t *types.Type, sw *generator.SnippetWriter) {
|
|||||||
sw.Do("out.$.name$ = in.$.name$.DeepCopy()\n", args)
|
sw.Do("out.$.name$ = in.$.name$.DeepCopy()\n", args)
|
||||||
} else if t.IsAssignable() {
|
} else if t.IsAssignable() {
|
||||||
sw.Do("out.$.name$ = in.$.name$\n", args)
|
sw.Do("out.$.name$ = in.$.name$\n", args)
|
||||||
} else if g.copyableWithinPackage(t) {
|
} else if g.copyableAndInBounds(t) {
|
||||||
funcName := g.funcNameTmpl(t)
|
funcName := g.funcNameTmpl(t)
|
||||||
sw.Do(fmt.Sprintf("if err := %s(in.$.name$, &out.$.name$, c); err != nil {\n", funcName), args)
|
sw.Do(fmt.Sprintf("if err := %s(in.$.name$, &out.$.name$, c); err != nil {\n", funcName), args)
|
||||||
sw.Do("return err\n", nil)
|
sw.Do("return err\n", nil)
|
||||||
@ -589,7 +592,7 @@ func (g *genDeepCopy) doPointer(t *types.Type, sw *generator.SnippetWriter) {
|
|||||||
sw.Do("**out = in.DeepCopy()\n", nil)
|
sw.Do("**out = in.DeepCopy()\n", nil)
|
||||||
} else if t.Elem.IsAssignable() {
|
} else if t.Elem.IsAssignable() {
|
||||||
sw.Do("**out = *in", nil)
|
sw.Do("**out = *in", nil)
|
||||||
} else if g.copyableWithinPackage(t.Elem) {
|
} else if g.copyableAndInBounds(t.Elem) {
|
||||||
funcName := g.funcNameTmpl(t.Elem)
|
funcName := g.funcNameTmpl(t.Elem)
|
||||||
sw.Do(fmt.Sprintf("if err := %s(*in, *out, c); err != nil {\n", funcName), argsFromType(t.Elem))
|
sw.Do(fmt.Sprintf("if err := %s(*in, *out, c); err != nil {\n", funcName), argsFromType(t.Elem))
|
||||||
sw.Do("return err\n", nil)
|
sw.Do("return err\n", nil)
|
||||||
|
Loading…
Reference in New Issue
Block a user