Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prep for not checking in generated, part 1/2 #28578

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 additions & 13 deletions cmd/libs/go2idl/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"k8s.io/kubernetes/cmd/libs/go2idl/namer"
"k8s.io/kubernetes/cmd/libs/go2idl/parser"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
"k8s.io/kubernetes/pkg/util"
utilflag "k8s.io/kubernetes/pkg/util/flag"

"github.com/spf13/pflag"
)
Expand All @@ -52,15 +54,15 @@ type GeneratorArgs struct {
// Which directories to parse.
InputDirs []string

// If true, recurse into all children of InputDirs
Recursive bool

// Source tree to write results to.
OutputBase string

// Package path within the source tree.
OutputPackagePath string

// Output file name.
OutputFileBaseName string

// Where to get copyright header text.
GoHeaderFilePath string

Expand All @@ -81,9 +83,9 @@ func (g *GeneratorArgs) AddFlags(fs *pflag.FlagSet) {
fs.StringSliceVarP(&g.InputDirs, "input-dirs", "i", g.InputDirs, "Comma-separated list of import paths to get input types from.")
fs.StringVarP(&g.OutputBase, "output-base", "o", g.OutputBase, "Output base; defaults to $GOPATH/src/ or ./ if $GOPATH is not set.")
fs.StringVarP(&g.OutputPackagePath, "output-package", "p", g.OutputPackagePath, "Base package path.")
fs.StringVarP(&g.OutputFileBaseName, "output-file-base", "O", g.OutputFileBaseName, "Base name (without .go suffix) for output files.")
fs.StringVarP(&g.GoHeaderFilePath, "go-header-file", "h", g.GoHeaderFilePath, "File containing boilerplate header text. The string YEAR will be replaced with the current 4-digit year.")
fs.BoolVar(&g.VerifyOnly, "verify-only", g.VerifyOnly, "If true, only verify existing output, do not write anything.")
fs.BoolVar(&g.Recursive, "recursive", g.VerifyOnly, "If true, recurse into all children of input directories.")
fs.StringVar(&g.GeneratedBuildTag, "build-tag", g.GeneratedBuildTag, "A Go build tag to use to identify files generated by this command. Should be unique.")
}

Expand All @@ -105,15 +107,14 @@ func (g *GeneratorArgs) NewBuilder() (*parser.Builder, error) {
b.AddBuildTags(g.GeneratedBuildTag)

for _, d := range g.InputDirs {
d = strings.TrimLeft(d, "+-*")
if g.Recursive {
if err := b.AddDirRecursive(d); err != nil {
return nil, fmt.Errorf("unable to add directory %q: %v", d, err)
}
var err error
if strings.HasSuffix(d, "/...") {
err = b.AddDirRecursive(strings.TrimSuffix(d, "/..."))
} else {
if err := b.AddDir(d); err != nil {
return nil, fmt.Errorf("unable to add directory %q: %v", d, err)
}
err = b.AddDir(d)
}
if err != nil {
return nil, fmt.Errorf("unable to add directory %q: %v", d, err)
}
}
return b, nil
Expand Down Expand Up @@ -144,7 +145,8 @@ func DefaultSourceTree() string {
// If you don't need any non-default behavior, use as:
// args.Default().Execute(...)
func (g *GeneratorArgs) Execute(nameSystems namer.NameSystems, defaultSystem string, pkgs func(*generator.Context, *GeneratorArgs) generator.Packages) error {
pflag.Parse()
utilflag.InitFlags()
util.InitLogs()

b, err := g.NewBuilder()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/libs/go2idl/client-gen/generators/client_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func packageForGroup(gv unversioned.GroupVersion, typeList []*types.Type, packag
return generators
},
FilterFunc: func(c *generator.Context, t *types.Type) bool {
return types.ExtractCommentTags("+", t.SecondClosestCommentLines)["genclient"] == "true"
return extractBoolTagOrDie("genclient", t.SecondClosestCommentLines) == true
},
}
}
Expand Down Expand Up @@ -190,7 +190,7 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
} else {
// User has not specified any override for this group version.
// filter out types which dont have genclient=true.
if types.ExtractCommentTags("+", t.SecondClosestCommentLines)["genclient"] != "true" {
if extractBoolTagOrDie("genclient", t.SecondClosestCommentLines) == false {
continue
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"path/filepath"
"strings"

"github.com/golang/glog"

clientgenargs "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/args"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators/normalization"
"k8s.io/kubernetes/cmd/libs/go2idl/generator"
Expand Down Expand Up @@ -75,11 +77,19 @@ func PackageForGroup(gv unversioned.GroupVersion, typeList []*types.Type, packag
return generators
},
FilterFunc: func(c *generator.Context, t *types.Type) bool {
return types.ExtractCommentTags("+", t.SecondClosestCommentLines)["genclient"] == "true"
return extractBoolTagOrDie("genclient", t.SecondClosestCommentLines) == true
},
}
}

func extractBoolTagOrDie(key string, lines []string) bool {
val, err := types.ExtractSingleBoolCommentTag("+", key, false, lines)
if err != nil {
glog.Fatalf(err.Error())
}
return val
}

func PackageForClientset(customArgs clientgenargs.Args, typedClientBasePath string, boilerplate []byte, generatedBy string) generator.Package {
return &generator.DefaultPackage{
// TODO: we'll generate fake clientset for different release in the future.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (g *genFakeForGroup) GenerateType(c *generator.Context, t *types.Type, w io
"Group": namer.IC(g.group),
"realClientPackage": filepath.Base(g.realClientPath),
}
namespaced := !(types.ExtractCommentTags("+", t.SecondClosestCommentLines)["nonNamespaced"] == "true")
namespaced := !extractBoolTagOrDie("nonNamespaced", t.SecondClosestCommentLines)
if namespaced {
sw.Do(getterImplNamespaced, wrapper)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
sw := generator.NewSnippetWriter(w, c, "$", "$")
pkg := filepath.Base(t.Name.Package)
const pkgTestingCore = "k8s.io/kubernetes/pkg/client/testing/core"
namespaced := !(types.ExtractCommentTags("+", t.SecondClosestCommentLines)["nonNamespaced"] == "true")
namespaced := !extractBoolTagOrDie("nonNamespaced", t.SecondClosestCommentLines)
canonicalGroup := g.group
if canonicalGroup == "core" {
canonicalGroup = ""
Expand All @@ -95,11 +95,9 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
}

// allow user to define a group name that's different from the one parsed from the directory.
for _, comment := range c.Universe.Package(g.inputPackage).DocComments {
comment = strings.TrimLeft(comment, "//")
if override, ok := types.ExtractCommentTags("+", comment)["groupName"]; ok {
groupName = override
}
p := c.Universe.Package(g.inputPackage)
if override := types.ExtractCommentTags("+", p.DocComments)["groupName"]; override != nil {
groupName = override[0]
}

m := map[string]interface{}{
Expand Down Expand Up @@ -138,7 +136,7 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
"NewPatchAction": c.Universe.Function(types.Name{Package: pkgTestingCore, Name: "NewPatchAction"}),
}

noMethods := types.ExtractCommentTags("+", t.SecondClosestCommentLines)["noMethods"] == "true"
noMethods := extractBoolTagOrDie("noMethods", t.SecondClosestCommentLines) == true

if namespaced {
sw.Do(structNamespaced, m)
Expand Down
11 changes: 4 additions & 7 deletions cmd/libs/go2idl/client-gen/generators/generator_for_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package generators

import (
"io"
"strings"

"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators/normalization"
"k8s.io/kubernetes/cmd/libs/go2idl/generator"
Expand Down Expand Up @@ -74,11 +73,9 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
groupName = ""
}
// allow user to define a group name that's different from the one parsed from the directory.
for _, comment := range c.Universe.Package(g.inputPacakge).DocComments {
comment = strings.TrimLeft(comment, "//")
if override, ok := types.ExtractCommentTags("+", comment)["groupName"]; ok && override != "" {
groupName = override
}
p := c.Universe.Package(g.inputPacakge)
if override := types.ExtractCommentTags("+", p.DocComments)["groupName"]; override != nil {
groupName = override[0]
}

m := map[string]interface{}{
Expand All @@ -104,7 +101,7 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
"type": t,
"Group": namer.IC(normalization.BeforeFirstDot(g.group)),
}
namespaced := !(types.ExtractCommentTags("+", t.SecondClosestCommentLines)["nonNamespaced"] == "true")
namespaced := !extractBoolTagOrDie("nonNamespaced", t.SecondClosestCommentLines)
if namespaced {
sw.Do(getterImplNamespaced, wrapper)
} else {
Expand Down
4 changes: 2 additions & 2 deletions cmd/libs/go2idl/client-gen/generators/generator_for_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func hasStatus(t *types.Type) bool {
func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
sw := generator.NewSnippetWriter(w, c, "$", "$")
pkg := filepath.Base(t.Name.Package)
namespaced := !(types.ExtractCommentTags("+", t.SecondClosestCommentLines)["nonNamespaced"] == "true")
namespaced := !extractBoolTagOrDie("nonNamespaced", t.SecondClosestCommentLines)
m := map[string]interface{}{
"type": t,
"package": pkg,
Expand All @@ -86,7 +86,7 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
} else {
sw.Do(getterNonNamesapced, m)
}
noMethods := types.ExtractCommentTags("+", t.SecondClosestCommentLines)["noMethods"] == "true"
noMethods := extractBoolTagOrDie("noMethods", t.SecondClosestCommentLines) == true

sw.Do(interfaceTemplate1, m)
if !noMethods {
Expand Down
33 changes: 33 additions & 0 deletions cmd/libs/go2idl/client-gen/generators/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2016 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generators

import (
"github.com/golang/glog"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
)

// extractBoolTagOrDie gets the comment-tags for the key and asserts that, if
// it exists, the value is boolean. If the tag did not exist, it returns
// false.
func extractBoolTagOrDie(key string, lines []string) bool {
val, err := types.ExtractSingleBoolCommentTag("+", key, false, lines)
if err != nil {
glog.Fatalf(err.Error())
}
return val
}
32 changes: 20 additions & 12 deletions cmd/libs/go2idl/conversion-gen/generators/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,8 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
// Only generate conversions for package which explicitly requested it
// byt setting "+genversion=true" in their doc.go file.
filtered := false
for _, comment := range p.DocComments {
comment := strings.Trim(comment, "//")
if types.ExtractCommentTags("+", comment)["genconversion"] == "true" {
filtered = true
}
if extractBoolTagOrDie("genconversion", false, p.DocComments) == true {
filtered = true
}
if !filtered {
continue
Expand Down Expand Up @@ -345,7 +342,7 @@ func isDirectlyConvertible(in, out *types.Type, preexisting conversions) bool {
// "+ genconversion=false"
// comment to ignore this field for conversion.
// TODO: Switch to SecondClosestCommentLines.
if types.ExtractCommentTags("+", inMember.CommentLines)["genconversion"] == "false" {
if extractBoolTagOrDie("genconversion", true, inMember.CommentLines) == false {
continue
}
return false
Expand Down Expand Up @@ -431,7 +428,7 @@ func (g *genConversion) convertibleOnlyWithinPackage(inType, outType *types.Type
if t.Name.Package != g.targetPackage {
return false
}
if types.ExtractCommentTags("+", t.CommentLines)["genconversion"] == "false" {
if extractBoolTagOrDie("genconversion", true, t.CommentLines) == false {
return false
}
// TODO: Consider generating functions for other kinds too.
Expand Down Expand Up @@ -613,9 +610,9 @@ func (g *genConversion) doBuiltin(inType, outType *types.Type, sw *generator.Sni

func (g *genConversion) doMap(inType, outType *types.Type, sw *generator.SnippetWriter) {
sw.Do("*out = make($.|raw$, len(*in))\n", outType)
if outType.Key.IsAssignable() {
if isDirectlyAssignable(inType.Key, outType.Key) {
sw.Do("for key, val := range *in {\n", nil)
if outType.Elem.IsAssignable() {
if isDirectlyAssignable(inType.Elem, outType.Elem) {
if inType.Key == outType.Key {
sw.Do("(*out)[key] = ", nil)
} else {
Expand Down Expand Up @@ -659,7 +656,7 @@ func (g *genConversion) doSlice(inType, outType *types.Type, sw *generator.Snipp
sw.Do("copy(*out, *in)\n", nil)
} else {
sw.Do("for i := range *in {\n", nil)
if outType.Elem.IsAssignable() {
if isDirectlyAssignable(inType.Elem, outType.Elem) {
if inType.Elem == outType.Elem {
sw.Do("(*out)[i] = (*in)[i]\n", nil)
} else {
Expand Down Expand Up @@ -750,7 +747,7 @@ func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.Snip
sw.Do("return err\n", nil)
sw.Do("}\n", nil)
case types.Alias:
if outT.IsAssignable() {
if isDirectlyAssignable(t, outT) {
if t == outT {
sw.Do("out.$.name$ = in.$.name$\n", args)
} else {
Expand Down Expand Up @@ -787,7 +784,7 @@ func (g *genConversion) isDirectlyAssignable(inType, outType *types.Type) bool {

func (g *genConversion) doPointer(inType, outType *types.Type, sw *generator.SnippetWriter) {
sw.Do("*out = new($.Elem|raw$)\n", outType)
if outType.Elem.IsAssignable() {
if isDirectlyAssignable(inType.Elem, outType.Elem) {
if inType.Elem == outType.Elem {
sw.Do("**out = **in\n", nil)
} else {
Expand Down Expand Up @@ -816,3 +813,14 @@ func (g *genConversion) doAlias(inType, outType *types.Type, sw *generator.Snipp
func (g *genConversion) doUnknown(inType, outType *types.Type, sw *generator.SnippetWriter) {
sw.Do("// FIXME: Type $.|raw$ is unsupported.\n", inType)
}

func isDirectlyAssignable(inType, outType *types.Type) bool {
// TODO: This should maybe check for actual assignability between the two
// types, rather than superficial traits that happen to indicate it is
// assignable in the ways we currently use this code.
return inType.IsAssignable() && (inType.IsPrimitive() || isSamePackage(inType, outType))
}

func isSamePackage(inType, outType *types.Type) bool {
return inType.Name.Package == outType.Name.Package
}
33 changes: 33 additions & 0 deletions cmd/libs/go2idl/conversion-gen/generators/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2016 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generators

import (
"github.com/golang/glog"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
)

// extractBoolTagOrDie gets the comment-tags for the key and asserts that, if
// it exists, the value is boolean. If the tag did not exist, it returns
// defaultVal.
func extractBoolTagOrDie(key string, defaultVal bool, lines []string) bool {
val, err := types.ExtractSingleBoolCommentTag("+", key, defaultVal, lines)
if err != nil {
glog.Fatalf(err.Error())
}
return val
}