Skip to content

Commit

Permalink
deepcopy-gen: re-add legacy scheme registration
Browse files Browse the repository at this point in the history
  • Loading branch information
sttts committed Jul 5, 2017
1 parent a906434 commit 56b0b49
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion examples/deepcopy-gen/generators/deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,56 @@ func argsFromType(ts ...*types.Type) generator.Args {

func (g *genDeepCopy) Init(c *generator.Context, w io.Writer) error {
glog.V(5).Infof("Registering types in pkg %q", g.targetPackage)
return nil

// the legacy restration will go away when the cloner is removed from Kubernetes, replaced
// with static function calls to the DeepCopy methods.
return g.legacyRegistration(c, w)
}

func (g *genDeepCopy) legacyRegistration(c *generator.Context, w io.Writer) error {
conversionPackagePath := "k8s.io/apimachinery/pkg/conversion"
runtimePackagePath := "k8s.io/apimachinery/pkg/runtime"

cloner := c.Universe.Type(types.Name{Package: conversionPackagePath, Name: "Cloner"})
g.imports.AddType(cloner)
if !g.registerTypes {
sw := generator.NewSnippetWriter(w, c, "$", "$")
sw.Do("// Deprecated: GetGeneratedDeepCopyFuncs returns the generated funcs, since we aren't registering them.\n", nil)
sw.Do("func GetGeneratedDeepCopyFuncs() []conversion.GeneratedDeepCopyFunc{\n", nil)
sw.Do("return []conversion.GeneratedDeepCopyFunc{\n", nil)
for _, t := range g.typesForInit {
args := argsFromType(t).
With("typeof", c.Universe.Package("reflect").Function("TypeOf"))
sw.Do("{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {in.(*$.type|raw$).DeepCopyInto(out.(*$.type|raw$)); return nil}, InType: $.typeof|raw$(&$.type|raw${})},\n", args)
}
sw.Do("}\n", nil)
sw.Do("}\n\n", nil)
return sw.Error()
}

sw := generator.NewSnippetWriter(w, c, "$", "$")
sw.Do("// Deprecated: register deep-copy functions.\n", nil)
sw.Do("func init() {\n", nil)
sw.Do("SchemeBuilder.Register(RegisterDeepCopies)\n", nil)
sw.Do("}\n\n", nil)

scheme := c.Universe.Type(types.Name{Package: runtimePackagePath, Name: "Scheme"})
schemePtr := &types.Type{
Kind: types.Pointer,
Elem: scheme,
}
sw.Do("// Deprecated: RegisterDeepCopies adds deep-copy functions to the given scheme. Public\n", nil)
sw.Do("// to allow building arbitrary schemes.\n", nil)
sw.Do("func RegisterDeepCopies(scheme $.|raw$) error {\n", schemePtr)
sw.Do("return scheme.AddGeneratedDeepCopyFuncs(\n", nil)
for _, t := range g.typesForInit {
args := argsFromType(t).
With("typeof", c.Universe.Package("reflect").Function("TypeOf"))
sw.Do("conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {in.(*$.type|raw$).DeepCopyInto(out.(*$.type|raw$)); return nil}, InType: $.typeof|raw$(&$.type|raw${})},\n", args)
}
sw.Do(")\n", nil)
sw.Do("}\n\n", nil)
return sw.Error()
}

func (g *genDeepCopy) needsGeneration(t *types.Type) bool {
Expand Down

0 comments on commit 56b0b49

Please sign in to comment.