Skip to content

Commit

Permalink
changes to generators
Browse files Browse the repository at this point in the history
  • Loading branch information
lavalamp committed Aug 12, 2016
1 parent f1fd638 commit bd7d7cc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ var (
)

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) {
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&TestType{},
&TestTypeList{},
)

scheme.AddKnownTypes(SchemeGroupVersion,
&api.ListOptions{})
&api.ListOptions{},
)
return nil
}

func (obj *TestType) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
)

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) {
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&TestType{},
&TestTypeList{},
Expand All @@ -41,8 +41,10 @@ func addKnownTypes(scheme *runtime.Scheme) {
&v1.ListOptions{},
&v1.DeleteOptions{},
&unversioned.Status{},
&v1.ExportOptions{})
&v1.ExportOptions{},
)
versionedwatch.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}

func (obj *TestType) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta }
Expand Down
24 changes: 14 additions & 10 deletions cmd/libs/go2idl/conversion-gen/generators/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func areTypesAliased(in, out *types.Type) bool {
}

const (
apiPackagePath = "k8s.io/kubernetes/pkg/api"
runtimePackagePath = "k8s.io/kubernetes/pkg/runtime"
conversionPackagePath = "k8s.io/kubernetes/pkg/conversion"
)

Expand Down Expand Up @@ -541,20 +541,24 @@ func (g *genConversion) preexists(inType, outType *types.Type) (*types.Type, boo
}

func (g *genConversion) Init(c *generator.Context, w io.Writer) error {
scheme := c.Universe.Package(apiPackagePath).Variable("Scheme")

sw := generator.NewSnippetWriter(w, c, "$", "$")
sw.Do("func init() {\n", nil)
sw.Do("if err := $.scheme|raw$.AddGeneratedConversionFuncs(\n", generator.Args{
"scheme": scheme,
})
sw.Do("SchemeBuilder.Register(RegisterConversions)\n", nil)
sw.Do("}\n", nil)

scheme := c.Universe.Type(types.Name{Package: runtimePackagePath, Name: "Scheme"})
schemePtr := &types.Type{
Kind: types.Pointer,
Elem: scheme,
}
sw.Do("// RegisterConversions adds conversion functions to the given scheme.\n", nil)
sw.Do("// Public to allow building arbitrary schemes.\n", nil)
sw.Do("func RegisterConversions(scheme $.|raw$) error {\n", schemePtr)
sw.Do("return scheme.AddGeneratedConversionFuncs(\n", nil)
for _, conv := range g.typesForInit {
sw.Do(nameTmpl+",\n", argsFromType(conv.inType, conv.outType))
}
sw.Do("); err != nil {\n", nil)
sw.Do("// if one of the conversion functions is malformed, detect it immediately.\n", nil)
sw.Do("panic(err)\n", nil)
sw.Do("}\n", nil)
sw.Do(")\n", nil)
sw.Do("}\n\n", nil)
return sw.Error()
}
Expand Down
21 changes: 13 additions & 8 deletions cmd/libs/go2idl/deepcopy-gen/generators/deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,21 +375,26 @@ func (g *genDeepCopy) Init(c *generator.Context, w io.Writer) error {
}
glog.V(5).Infof("registering types in pkg %q", g.targetPackage)

scheme := c.Universe.Variable(types.Name{Package: apiPackagePath, Name: "Scheme"})
sw := generator.NewSnippetWriter(w, c, "$", "$")
sw.Do("func init() {\n", nil)
sw.Do("if err := $.scheme|raw$.AddGeneratedDeepCopyFuncs(\n", generator.Args{
"scheme": scheme,
})
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("// 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: $.type|dcFnName$, InType: $.typeof|raw$(&$.type|raw${})},\n", args)
}
sw.Do("); err != nil {\n", nil)
sw.Do("// if one of the deep copy functions is malformed, detect it immediately.\n", nil)
sw.Do("panic(err)\n", nil)
sw.Do("}\n", nil)
sw.Do(")\n", nil)
sw.Do("}\n\n", nil)
return sw.Error()
}
Expand Down

0 comments on commit bd7d7cc

Please sign in to comment.